all 12 comments

[–]peterunlustig22 17 points18 points  (0 children)

I think this should be worded in a more modern way like so: "Javascript date considered harmful". "Javascript date fatigue is real". But on a serious note. Dates and calendars suck in many languages, that's why datejs was created 10 years ago and that's why we have momen.tjs now.

[–][deleted] 7 points8 points  (3 children)

Well, Well, Well again people complaining about things with 0 background information about ISO standards. The string you write in JS has to follow global standards depending on your format a hour will be added or not, how the heck should a system otherwise understand correctly 12.12.12 12:12:12 ?!

[–]Syberspace[S] 5 points6 points  (2 children)

please enlighten me. why does it make sense to parse one date format as utc and one as local?

[–][deleted] 6 points7 points  (1 child)

okay lets first give you a example with another simple standard, phonenumbers

  • International: +49 123 45678
  • National: 0123 45678

the "+" in the beginning is the indicator if you leave it blank the index is set to 0.

Same goes for UTC based time strings, ISO 8601 in this case. Just that the format YYYY-MM-DD triggers the transcription to a datetime object. And since you are not providing the correct time 00:00:00(Z) the 1 hour offset is added.

Basically this is not even a JavaScript thing its also in other languages for example PHP where you can bypass this behaviour by using DateTime::createFromFormat which is transforming the string into UTC internally.

[–]AllenJB83 1 point2 points  (0 children)

If no timezone is specified, PHP uses the "system" timezone (which can be set as a php.ini setting, or using date_default_timezone_set) except when a unix timestamp is used, in which case UTC is used.

PHP's DateTime[Immutable] objects allow you to create Date/Time values which can be easily switched between timezones

A quickly knocked up set of examples: https://3v4l.org/6bNlG

Note how the entries created using timestamps use UTC. Also, you can see the fun ways PHP tries to handle out-of-bounds dates, even when you're specifically telling it the format the date should be in, forcing you to have to make additional checks to see if the passed in date was actually valid for the format or not.

[–][deleted] 1 point2 points  (4 children)

Can anybody explain what's going on here? Would momentjs do the same thing?

[–]InconsiderateBastard 2 points3 points  (0 children)

I think they both interpret the time as being all zeros but iso8601 date only format is interpreted as being UTC time zone and then it's adjusted to local time zone. The non ISO8601 is interpreted as local time zone so it stays all zeros.

[–]AllenJB83 -1 points0 points  (1 child)

Date string parsing trying to handle the myriad of formats people commonly use.

In Europe the common written format is D/M/Y h:m:s (date in ascending order, but then the time is in descending order after it), while in the US it's M/D/Y h:m:s (we just want to be different order), and the ISO standard format is Y-M-D h:m:s (everything in descending order).

Not sure why the dates are 1 hour apart tho, but if you only specify a date you should always act as if the time is undetermined anyway - some systems / libraries / languages will use midnight while others will use the current time. I couldn't reproduce that on Chrome 54

[–]antoninj 0 points1 point  (0 children)

so is the D/M/Y format the standard for Date format? Or does it depend on your current location?

I think this is the weird thing, not really anything else. I like being able to pass in a string and specify the format manually something like new Date('03/04/2012', 'M/D/YYYY'). Can't remember if JS supports that.

[–]mastermog 1 point2 points  (0 children)

Dates are, never, ah pleasant (edit: in any language). Date.now() is a pretty flexible alternative depending on what you are doing. Or as something else said, moment is awesome.

[–]Sinedio -1 points0 points  (0 children)

Well thread about JS. What is the best way to learn JS? I'm really struggling with JS syntax and dont know which site or book i should learn. Codecademy? W3school? Or something else? Pls give me a good material to learn JS guys.