JSON date format (no time) returned from API

Wonder if I should delete the time data that is usually stored. We use postgres and node.js , where DateTime will be returned from our API as:

2013-07-01T00:00:00.000Z

However, since this field should only represent a date, I believe that reformatting before returning would make it clearer that time does not matter:

2013-07-01

Thoughts?

+6
source share
2 answers

If you represent a calendar date, there is no time or time zone. A short presentation makes more sense.

Unfortunately, the new Date (string) in many javascript implementations may do bad things for you.

 new Date('2015-09-23') Tue Sep 22 2015 20:00:00 GMT-0400 (Eastern Daylight Time) 

The easiest way out of the problem is not to use javascript Date - this type is the same as DateTimeOffset in other languages. This is a poor way to represent calendar date values.

But you're probably going to use javascript Date anyway. The next simple β€œfix” is to avoid standard views (since standard views are interpreted as DateTimeOffset with UTC). Here are two possibilities:

Use "/" instead of "-".

 new Date('2015/09/23') Wed Sep 23 2015 00:00:00 GMT-0400 (Eastern Daylight Time) 

Use a three-digit month - leading zeros will be discarded.

 new Date('2015-009-23') Wed Sep 23 2015 00:00:00 GMT-0400 (Eastern Daylight Time) 

If you have client and server side javascript, you're done. If you have something else on the server side, you should think about what the server language will do if it sees non-standard date formats.

+4
source

As an API user, I would rather get a long date form.

For several reasons:

  • Time Zone : The long format has an integrated time zone. It is important.
  • Parsing : most languages ​​will be able to read this long format as a native Date object. In some languages, such as C # and Java, this short date will need to be forced to use the correct time zone. You also avoid the confusion of the Month / Day with a long format.
  • Comparisons . If the user goes through a short date, will your API work correctly? A good API should look the same as exiting.
+3
source

Source: https://habr.com/ru/post/949260/


All Articles