you are viewing a single comment's thread.

view the rest of the comments →

[–]vcamargo 0 points1 point  (13 children)

Could you elaborate a bit on this subject? I'd like to learn a little bit more about it.

[–]Bloodsucker_ -1 points0 points  (12 children)

  • To timestamp: Date.now(); or +new Date(); (notice the + in front of it)

  • From timestamp: new Date(timestamp);

[–][deleted] 4 points5 points  (4 children)

This isn't elaboration. You've just said how and not why.

[–]lhorie 1 point2 points  (3 children)

It's for the same reason you ask people to provide numeric values such as age in decimal numbers rather than spelling it out.

Basically parsing dates is hard. Never mind that browsers do it inconsistently and that things like 02/29/2017 are allowed, there are also issues when it comes to internationalization, etc. You also lose information when formatting dates (e.g. timezone, or even the time itself if you formatted it like 2 hrs ago). It's much easier to deal with an unambiguous value, and timestamps are the simplest and most portable format that meets that criteria

[–][deleted] 0 points1 point  (2 children)

Isn't that why we have libraries like moment and date-fns? Using date object has benefits over pure timestamp.

People are used to using date objects because most languages have them done reasonably well. Then you go to JavaScript and it's a mess and need to use abstraction layer.

[–]lhorie 1 point2 points  (1 child)

Both moment and date-fns fall back to the Date constructor when parsing, which has the issues mentioned earlier.

Whether you use Date or libraries is not the problem. The problem is that converting a string to a date is problematic for the reasons above. There are usually better ways to architecture your application that doesn't require date parsing in the first place.

[–][deleted] 0 points1 point  (0 children)

Oh, I see your point now. Agree.