-3

I'm at West Coast, verified 7 hours behind UTC/GMT. Thought the followings are equivalent

const d =  new Date('2023-01-01'); 
const d2 = new Date('2023-01-01 0:0:0.000'); 

Result:

Sat Dec 31 2022 16:00:00 GMT-0800 (Pacific Standard Time)

Sun Jan 01 2023 00:00:00 GMT-0800 (Pacific Standard Time)

Why are Zero hours, minutes, seconds and milliseconds are ahead? And why it's not 7 hours diff?

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
Jeb50
  • 6,272
  • 6
  • 49
  • 87
  • `'2023-01-01 0:0:0.000'` isn't a standard string value and your JavaScript runtime environment is free to choose a value. `'2023-01-01'` is an argument for an UTC timestamp with time `00:00:00`. ["`dateString` A string value representing a date, in a format recognized by the `Date.parse()` method. (The ECMA262 spec specifies a simplified version of ISO 8601, but other formats can be implementation-defined, which commonly include IETF-compliant RFC 2822 timestamps.)"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date) – Thomas Sablik Mar 18 '23 at 16:20
  • @ThomasSablik Thank you very much for your effort and helping out! Following is for SO policy committee: Everyone is special and different. Not all coders are talented. Use the suggested answer as example, do we expect an ordinary person to study all 35 answers some dated back to 12 years ago? Here is [computer history of 2011](https://www.computerhope.com/history/2011.htm) When we encourage our kids to ask questions at school, we the adults penalize whoever ask questions not to the like of some others. "There are no stupid questions." – Jeb50 Mar 18 '23 at 21:20
  • @Jeb50—you should start with the accepted answer, then read some of the higher voted answers. The ECMAScript Date object hasn't changed **at all** since 1999. There have been some minor changes to parsing and formatting. Which is why an answer from 2011, updated in 2017, is still worth using. Another duplicate has been added that more precisely matches your question (from 2010, updated 2016). If you want "the future", see the proposed [ECMAScript Temporal object](https://tc39.es/proposal-temporal/docs/). – RobG Mar 20 '23 at 05:05
  • Something is wrong with your timezone, because yesterday 'the west coast' (assuming USA given that Americans typically omit their country) was not in 'Pacific Standard Time', it was observing 'Pacific Daylight Time'. The former is -0800 GMT, the latter -0700, so I assume that whatever your computer is using to pick _your_ timezone, it's incorrect. – Evert Mar 20 '23 at 05:09
  • Also confirming from timeandate.com that there are no places _currently_ observing Pacific Standard Time: https://www.timeanddate.com/time/zones/pst – Evert Mar 20 '23 at 05:12

1 Answers1

1

The Date constructor, when called with a string argument with no time component, assumes 00:00:00 UTC. When given a time conponent it interprets it as local time. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date

Date-only strings (e.g. "1970-01-01") are treated as UTC, while date-time strings (e.g. "1970-01-01T12:00") are treated as local.

The offset is eight hours because that's the Pacific Standard Time offset. Your local time is one hour later (-07:00) because your timezone is currently in daylight saving time.

As for why your code thinks your timezone is PST I can only guess. Maybe your computer's date is not set correctly, and is set to a date outside of DST. Or maybe your operating system's timezone database is out of date. Or maybe there's some other anomaly in your setup.

Ciaran Keating
  • 2,793
  • 21
  • 19
  • Like your answer. As so JS doesn't produce a correct local time. – Jeb50 Mar 18 '23 at 20:58
  • Your Javascript code is saying that your computer's timezone is set to Pacific Standard Time, whereas in real life your in Pacific Daylight Time. What does your computer's clock say? – Ciaran Keating Mar 19 '23 at 22:33
  • **"I'm at West Coast, verified 7 hours behind UTC/GMT."** It was 10 am PST, and 5 pm UTC. – Jeb50 Mar 20 '23 at 04:05
  • Sorry, obviously my question wasn't clear. When you say "I'm at West Coast..." that could mean that you know where you are, and you know your current time and offset from UTC. But is your computer set to PST or PDT? Maybe you've selected the "West Coast" timezone but haven't checked the "Automatically adjust for daylight saving time" checkbox...? – Ciaran Keating Mar 20 '23 at 04:27
  • The first sentence is incorrect. For date-only timestamps, UTC is only assumed where the string exactly matches YYYY-MM-DD format **and** has valid values. Otherwise, it is treated as either invalid (if one or more values are invalid) or is parsed using implementation specific heuristics (say if in YYYY-M-D format, i.e. leading zeros for single digit months and days are omitted). – RobG Mar 20 '23 at 05:12
  • @RobG Good clarification. – Jeb50 Mar 20 '23 at 15:11