1

I am confused by the result of the following script and I don't understand why it is what it is:

enddate = '01-02-2020'; //euro format dd-mm-yyyy
datesplit = enddate.split("-"); 

console.log("datesplit: ", datesplit); //[ '01', '02', '2020' ]
console.log(datesplit[2]); // 2020
console.log(datesplit[1]); // 02
console.log(datesplit[0]); // 01

enddate1 = new Date(datesplit[2],datesplit[1],datesplit[0]);

console.log("enddate 1", enddate1); //output: 2020-03-01T05:00:00.000Z , but I'm expecting 2020-02-01T00:00:00.000Z

That last console log output is what I can't understand. I would appreciate an explanation of why the result is what it is.

mo_maat
  • 2,110
  • 12
  • 44
  • 72
  • Sounds like the work of time zones. What time zone are you in? – evolutionxbox Feb 02 '20 at 02:33
  • eastern timezone – mo_maat Feb 02 '20 at 02:35
  • It's the work of Date parsing not liking the invalid format, and so falling back to browser specific parsing rules which are weird - it's probably defaulting to GMT timezone or local time – Shiny Feb 02 '20 at 02:37
  • @Shiny local time most likely – evolutionxbox Feb 02 '20 at 02:37
  • 1
    But how can it not like the format if I'm explicitly setting the year, month and day? That's the whole reason why I did the split. – mo_maat Feb 02 '20 at 02:39
  • 1
    Oh - Months are zero based,it treats it like an Array. 02 month = 3rd month – Shiny Feb 02 '20 at 02:45
  • 1
    Right!! Thanks! That gets it closer. I'm still getting `2020-02-26T05:00:00.000Z` not `2020-02-25T23:00:00.000Z`. I am wanting to get `2020-02-26T00:00:00.000Z` – mo_maat Feb 02 '20 at 02:53
  • @mo_maat—put extra information **in the question**, not in comments. To set the UTC date, use `new Date(Date.UTC(datesplit[2],datesplit[1],datesplit[0]))` so that the values are parsed as UTC, not local. – RobG Feb 02 '20 at 22:30

3 Answers3

5

JavaScript treats the month as zero-based. So you'll have to -1 your month value to get the right result. As @RobG said, you should use new Date(Date.UTC(...)) to get your date in UTC

let endDate = '01-02-2020' // dd-mm-yyyy;
let [day, month, year] = endDate.split('-');

// Months are zero-based, so -1 to get the right month
month = month - 1;

console.log(day);  // '01'
console.log(month);// 1
console.log(year); // '2020'

let newDate = new Date(Date.UTC(year, month, day));

console.log(newDate) // "2020-02-01T00:00:00.000Z"
Shiny
  • 4,945
  • 3
  • 17
  • 33
  • 1
    I've accepted this answer but I still have what seems to be the timezone issue. My date is not coming out as `2020-03-01T00:00:00.000Z`. I still get `2020-03-01T00:05:00.000Z`. – mo_maat Feb 02 '20 at 03:10
  • I've amended my answer, and removed the second section - Could I ask what's wrong with that second style? – Shiny Feb 02 '20 at 22:52
  • 1
    In regard to the removed second method, use of the built–in parser is strongly discouraged, especially for non–standard timestamps either as `new Date(string)` or `Date.parse(string)`. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Feb 03 '20 at 22:46
0

You can check Mozilla Documentation

You will see that January is 0, February is 1, and so on. So that's how date works in JavaScript.

You need to convert your month value to Number and then make it "-1". So something like this:

new Date(datesplit[2], (parseInt(datesplit[1], 10) - 1), datesplit[0])
miken32
  • 42,008
  • 16
  • 111
  • 154
pacanga
  • 416
  • 6
  • 17
  • 1
    Also, there is no need to "*convert your month value to Number*", the minus operator will coerce the value to Number anyway. ;-) – RobG Feb 02 '20 at 22:49
0

Given that the other posts seem to have helped you get the right month, have you tried using .toISOString method on the Date object to get the right UTC offset?

The docs on MDN state that the timezone is always zero UTC offset.

Sherman Hui
  • 938
  • 3
  • 10
  • 22