一键导入
react-native-protobuf-dates
How to correctly handle and format protobuf Timestamp dates sent by the API Gateway to prevent "Invalid Date" issues.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
How to correctly handle and format protobuf Timestamp dates sent by the API Gateway to prevent "Invalid Date" issues.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Mandatory rules and components for handling keyboard avoidance smoothly in the React Native app.
Senior-level React Native architecture and patterns for Expo 54 apps.
Strict coding standards, naming conventions, file size limits, and typing rules.
Guidelines for managing server state, queries, and API clients in React Native.
Best practices for implementing forms with React Hook Form and Zod.
Mandatory rule to never use deprecated APIs or ignore deprecation warnings.
| name | React Native Protobuf Dates |
| description | How to correctly handle and format protobuf Timestamp dates sent by the API Gateway to prevent "Invalid Date" issues. |
When working with events or any API endpoints returning Protobuf Timestamp objects via the API Gateway, you MUST be extremely careful with date parsing.
By default, Protobuf serializes Timestamp in JSON using various formats depending on the backend configuration, ts-proto settings, and integer limits (Long.js).
When a Timestamp is received in React Native, the seconds property is often NOT a simple number! It can be:
"2026-05-31T20:00:00Z")171231231000)seconds ({ seconds: 171231231 })seconds ({ seconds: "171231231" })Long representation because 64-bit integers exceed standard JS safe integer limits ({ seconds: { low: 171231231, high: 0, unsigned: false } }){ _seconds: 171231231 } or { toDate: () => Date })If you naively parse new Date(timestamp.seconds * 1000) and seconds is a Long object like {low, high}, it will result in NaN and display "Invalid Date" in the UI.
You MUST NEVER write custom date parsing logic for API timestamps.
Always use the shared, bulletproof formatDate utility provided in @/shared/lib/format-date.utils.ts. This utility handles all possible Protobuf serialization quirks, including Long parsing and string fallbacks.
import { formatDate } from '@/shared/lib/format-date.utils';
// Default formatting: 'DD/MM/YYYY HH:MM'
const displayDate = formatDate(event.startAt);
// Custom formatting options
const shortDate = formatDate(event.startAt, {
day: '2-digit',
month: '2-digit',
year: '2-digit',
});
new Date() directly on properties derived from a Protobuf Timestamp.formatDate from @/shared/lib/format-date.utils to display dates securely and consistently across the app.