2

What is going on with the snippet below? I'm trying to build an array of strings as dates starting from a day in the past. I'm able to do it for the whole 2019 year for example. But when I start in 2017-01-01 somehow I CANNOT increment the date past 2017-03-26.

I can increment 2017-03-27 to 2017-03-28 without a problem. Why is this happening?

const currentDay = "2017-01-01";
const nextDay = new Date(new Date(currentDay).setDate(new Date(currentDay).getDate() + 1)).toISOString().split("T")[0];

console.log('currentDay: ' + currentDay);
console.log('nextDay: ' + nextDay);

const currentDay2 = "2017-03-26";
const nextDay2 = new Date(new Date(currentDay2).setDate(new Date(currentDay2).getDate() + 1)).toISOString().split("T")[0];

console.log('currentDay2: ' + currentDay2);
console.log('nextDay2: ' + nextDay2 + '<------ DOES NOT INCREMENT');

const currentDay3 = "2017-03-27";
const nextDay3 = new Date(new Date(currentDay3).setDate(new Date(currentDay3).getDate() + 1)).toISOString().split("T")[0];

console.log('currentDay3: ' + currentDay3);
console.log('nextDay3: ' + nextDay3);

I don't think this is an issue with my environment, but anyway, here's what I'm getting:

enter image description here

Are you guys getting something different?

cbdeveloper
  • 27,898
  • 37
  • 155
  • 336
  • 2
    I get `nextDay2: 2017-03-27<------ DOES NOT INCREMENT`. Maybe timezone related? (no idea) – CertainPerformance Dec 16 '19 at 08:29
  • I get this in jsfiddle: `currentDay: 2017-01-01 _display:35:9 nextDay: 2017-01-02 _display:36:9 currentDay2: 2017-03-26 _display:41:9 nextDay2: 2017-03-26<------ DOES NOT INCREMENT _display:42:9 currentDay3: 2017-03-27 _display:47:9 nextDay3: 2017-03-28` – jared Dec 16 '19 at 08:29
  • 1
    My guess - daylight savings. – VLAZ Dec 16 '19 at 08:30
  • It seems to be daylight saving time / timezone related. How can I solve this? – cbdeveloper Dec 16 '19 at 08:30
  • 1
    @jared You can run Stack Snippets directly on Stack Overflow, no need to paste code into JSFiddle that's already runnable here – CertainPerformance Dec 16 '19 at 08:30
  • Any ideas how can I get past this? – cbdeveloper Dec 16 '19 at 08:31
  • this works `const currentDay2 = "2017-03-26"; const nextDay2 = new Date(new Date(currentDay2).setDate(new Date(currentDay2).getDate() + 1));` – jared Dec 16 '19 at 08:33
  • 1
    I'd check out https://momentjs.com/ it's pretty lightweight. – jasper Dec 16 '19 at 08:33
  • The easiest is to just set the time to `12:00:00` - so it doesn't matter if you increment or decrement the hours. – VLAZ Dec 16 '19 at 08:34
  • Also your way of advancing to the next day is WAY more overcomplicated than it needs to be: `date.setDate(date.getDate() + 1)` – VLAZ Dec 16 '19 at 08:36
  • 1
    The fundamental issue is that dates in the format YYYY-MM-DD are parsed as UTC, but you are printing dates using local dates. So if you're in a timezone west of Greenwich, 2017-03-26 UTC is sometime the day before locally. So when you add 1 day and see the local date, it's still the 26th. – RobG Dec 16 '19 at 11:52

2 Answers2

1

It is long code to make comment, so decided to make an answer:

const currentDay2 = "2017-03-26";
let [y,M,d] = currentDay2.split(/[-]/);
const nextDay2 = new Date(y, (+M - 1), ++d).toISOString();
console.log(nextDay2);

It looks like Date.Parse() parsed incorrectly.

StepUp
  • 36,391
  • 15
  • 88
  • 148
-1

Since you are working with UTC format dates, and you want to ignore local timezone changes such as daylight savings time, you should always use getUTCDate() and setUTCDate(). UTC has no daylight savings.

This is a duplicate of the previous question.

It's working properly on my side.

enter image description here

SuperStar518
  • 2,814
  • 2
  • 20
  • 35
  • 4
    If it's a duplicate, then you should flag it as such, instead of posting an answer to point it out. – VLAZ Dec 16 '19 at 08:38
  • @VLAZ maybe... but i'd rather like to care the question. – SuperStar518 Dec 16 '19 at 08:39
  • 2
    The duplicate system is here to *resolve questions*. If there is an answer somewhere, it's more beneficial for everyone to have the answers in a common place, instead of fragmenting the knowledge across different pages. And everyone includes the OP as well as anybody who would stumble upon this after searching. – VLAZ Dec 16 '19 at 09:21
  • UTC is not a format, you might mean ISO 8601 format. – RobG Dec 16 '19 at 11:47