JavaScript Why does the same date have different hours? what does same date mean

Maybe the answer is obvious, but I don't get it. Why are the Dates in the Code Snippet different? Does the format say something about the hours as well?

console.log("2017-1-9 -->")
console.log(new Date("2017-1-9"))
console.log("2017-1-09 -->")
console.log(new Date("2017-1-09"))
console.log("2017-01-9 -->")
console.log(new Date("2017-01-9"))
console.log("2017-01-09 -->")
console.log(new Date("2017-01-09"))
console.log("2017-01-11 -->")
console.log(new Date("2017-01-11"))
.as-console-wrapper { 
  min-height: 100%;
}
Answer:1

TL;DR: Because the language specification says that date strings not conforming to the specified format can be parsed according to "any implementation-specific heuristics or implementation-specific date formats," and YYYY-M-D is just such a string.

Let's dive into the spec. Here's what the ECMAScript 5.1 spec says about the Date constructor (I'm quoting it instead of the current, ES2016 spec just because it's simpler, but the latter works basically the same in this case):

15.9.3.2 new Date (value)

...

The [[PrimitiveValue]] internal property of the newly constructed object is set as follows:

  1. Let v be ToPrimitive(value).
  2. If Type(v) is String, then

    • Parse v as a date, in exactly the same manner as for the parse method (15.9.4.2); let V be the time value for this date.
  3. ...

And here's the spec for parse (emphasis mine):

15.9.4.2 Date.parse (string)

The parse function applies the ToString operator to its argument and interprets the resulting String as a date and time... The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. ...

Date Time String Format, in a nutshell, is YYYY-MM-DDTHH:mm:ss.sssZ and its subsets. Since YYYY-M-D doesn't conform to that format, the interpreter is (unfortunately) free to do whatever it wants. If you want to know why Chrome does it in this particular way, you'll have to dig around in the V8 source.

Answer:2

I am learning javascript and at the same time trying to create a simple script that allows you to type a foreign language on a web browser with your keyboard. So, when you type a for example, there ...

I am learning javascript and at the same time trying to create a simple script that allows you to type a foreign language on a web browser with your keyboard. So, when you type a for example, there ...

i have a code that list below option in . when i select other, then combo box should convert into text. i tried with some script using onchange fucntion function chargeother(ele){ if(ele....

i have a code that list below option in . when i select other, then combo box should convert into text. i tried with some script using onchange fucntion function chargeother(ele){ if(ele....

  1. change combo box text size
  2. how to change text in combobox
  3. change combobox text color
  4. xaml change combobox text color
  5. jquery change text combobox

I am new to JavaScript and trying to find the difference between two JSON Objects. The structure of the JSON object and its data is shown below. I got a code online which works for a normal JSON ...

I am new to JavaScript and trying to find the difference between two JSON Objects. The structure of the JSON object and its data is shown below. I got a code online which works for a normal JSON ...

  1. compare json objects java
  2. compare json objects javascript
  3. compare json objects online
  4. compare json objects python
  5. compare json objects c#
  6. compare json objects nodejs
  7. compare json objects powershell
  8. compare json objects php
  9. compare json objects jackson
  10. compare json objects jquery
  11. compare json objects
  12. compare two json objects java
  13. compare two json objects javascript
  14. compare two json objects
  15. compare two json objects online
  16. compare 2 json objects java
  17. compare 2 json objects
  18. compare 2 json objects javascript
  19. compare two json objects python

I'm learning Angular 2. I'm using a LocationService with an Observable that hands me the coordinates after some time. This is my code. location.service.ts public getLocation(): Observable<any> ...

I'm learning Angular 2. I'm using a LocationService with an Observable that hands me the coordinates after some time. This is my code. location.service.ts public getLocation(): Observable<any> ...

  1. angular subscribe when to unsubscribe
  2. angular subscribe when done
  3. angular subscribe when
  4. angular subscribe when all
  5. angular subscribe when complete
  6. angular when subscribe finished