Home >Web Front-end >JS Tutorial >What's the Best Way to Represent Dates in JSON?
The JSON standard doesn't dictate a specific format for representing dates. However, JavaScript offers guidance through the Date object's toJSON method.
Recommended Format
According to JavaScript, the ideal JSON date format is:
2012-04-23T18:25:43.511Z
This format combines human readability with conciseness. It supports correct sorting, including fractional seconds for chronology restoration, and adheres to ISO 8601. Moreover, ISO 8601 enjoys widespread international acceptance and endorsement from various organizations.
Other Acceptable Formats
While the JavaScript format is recommended, other formats like "milliseconds since 1970" are universally understood by date libraries. This makes them viable for portability.
Conclusion
Ultimately, the right JSON date format depends on the specific needs and preferences of the application or framework being used. However, for consistency, readability, and ISO compliance, the recommended JavaScript format remains the ideal choice.
The above is the detailed content of What's the Best Way to Represent Dates in JSON?. For more information, please follow other related articles on the PHP Chinese website!