Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Date format (without time) returning from an API

Wondering if I should remove the time data normally stored. We're using postgres and node.js where a DateTime would get returned from our API as:

2013-07-01T00:00:00.000Z

Yet since this field should only represent a date, I feel reformatting before return like this would make it more clear that time is not relevant:

2013-07-01

Thoughts?

like image 524
Matt Avatar asked Jul 11 '13 18:07

Matt


People also ask

How do I set date format in JSON?

There is no date format in JSON, there's only strings a de-/serializer decides to map to date values. However, JavaScript built-in JSON object and ISO8601 contains all the information to be understand by human and computer and does not relies on the beginning of the computer era (1970-1-1).

Does JSON support date format?

JSON does not directly support the date format and it stores it as String. However, as you have learned by now that mongo shell is a JavaScript interpreter and so in order to represent dates in JavaScript, JSON uses a specific string format ISODate to encode dates as string.

Does JSON Stringify convert date to UTC?

stringify converts DateTimeOffset to UTC format #1710.

How can pass date in JSON postman?

environment. set('currentdate', moment(). format(("YYYY-MM-DD"))); {{$timestamp}} -> predefined variable gets the current timestamp, Since you need date the above one should work.


2 Answers

If you are representing a calendar date, there is no time or timezone. The short representation makes more sense.

Unfortunately, new Date(string) in many javascript implementations can do bad things to you.

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

The simplest way out of the problem is not to use javascript's Date - this type matches up with DateTimeOffset in other languages. It is a bad way to represent calendar date values.

But, you're probably going to use javascript's Date anyway. The next simplest "fix" is to avoid standard representations (since standard representations get interpretted 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 3 digit month - the leading zeroes will be discarded.

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

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

like image 190
Amy B Avatar answered Sep 24 '22 21:09

Amy B


As an API user, I would much rather receive the long form of a date.

For a few reasons:

  1. Time Zone: the long format actually has a time-zone built in. That's important.
  2. Parsing: most languages will be able to read that long format as a native "Date" object. In some languages like C# & Java, that short date will need to be coerced into using the correct time zone. You also avoid Month/Day confusion with the long format.
  3. Comparisons: if a user passes in a short date, is your API going to handle that correctly? A good API needs to look the same going in and out.
like image 43
Gates VP Avatar answered Sep 23 '22 21:09

Gates VP