1

Seems like a simple question, but all the timezone ins and outs in JS are causing me a bunch of headaches.

Basically, if I have a date like the following:

2018-04-06

I want to be able to get the next day's date as such:

2018-04-07

I found the following snippet on SO for doing this (kind of):

var date = new Date('2018-04-06');
date.setDate(date + 1);

The problem is that I'm getting the date back with the adjusted timezone, and because I'm in the US ET timezone, it's giving me that date minus five hours, which is actually the same day as where I started.

I've been through countless SO posts trying to find an answer to this seemingly simple question, but for any given date, regardless of the timezone the user is in, how do I get the next day's date in YYYY-MM-DD format? Thank you.

HartleySan
  • 7,404
  • 14
  • 66
  • 119

4 Answers4

4

Strings in the format YYYY-MM-DD are parsed as UTC so in this case, do everything in UTC (see Why does Date.parse give incorrect results? and How can I add 1 day to current date?).

The toISOString method will return the string in the required format, just trim the redundant time part, e.g.

let s = '2018-04-06';
let d = new Date(s);
d.setUTCDate(d.getUTCDate() + 1);
console.log(d.toISOString().substr(0,10));
RobG
  • 142,382
  • 31
  • 172
  • 209
0

Did you try with the UTC date?

var date = new Date('2018-04-06');
console.log(date.toUTCString());

date.setDate(date.getDate() + 1);
console.log(date.toUTCString());
chrisbyte
  • 1,408
  • 3
  • 11
  • 18
  • Thanks for the quick response. That does seem to solve the timezone issue, but I get the string back in the following format: `Sat, 07 Apr 2018 00:00:00 GMT`. How do I then convert that to YYYY-MM-DD format? Thank you. – HartleySan Feb 18 '20 at 21:18
  • You can use the [Date methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) to get the different pieces then reformat the date the way you like. – displacedtexan Feb 18 '20 at 21:30
  • displacedtexan, I've tried doing that several different ways now, and I'm not seeing how. `toUTCString()` produces a string, which you can't use the date methods on to get the various pieces, and feeding the UTC date back into a `Date` function (e.g., `new Date(date.toUTCString())`) just creates the timezone issue again. Could you please explain in more detail what you mean? Thank you. – HartleySan Feb 18 '20 at 21:44
  • You need to add 1 to the UTC date, otherwise there will be a small period each year during daylight saving transition when the date will not increment. Also, *toISOString* gives the required format. – RobG Feb 18 '20 at 22:10
0

As it was suggested by @chrisbyte, have your tried to use toUTCString method instead of toString() method ?

As a reminder , toString is the default used when you display the date object withim the console for example

I think the "problem" you're assuming is just an incomplete understanding how Date.toString() method behaves: this method seems to to return string representing a Date object but seems to use timezone as mentionned here (on the comment in 1st example)

Here my snippet to understand more:

  const originalDate = new Date('2018-04-06');
    // retrieving the original timestamp
    const originalTimestamp = originalDate.valueOf()
    
    // displaying the original date (non UTC / UTC)
    console.log(`original date (timezone dependent): ${originalDate.toString()}`)
        console.log(`original date (timezone independent): ${originalDate.toUTCString()}`)

    // we add one more day
    originalDate.setDate(originalDate.getDate() +1)
    const dayAfterTimestamp = originalDate.valueOf()
    
   // displaying the original date (non UTC / UTC)
    console.log(`updated date (timezone dependent): ${originalDate.toString()}`)
        console.log(`updated date (timezone independent): ${originalDate.toUTCString()}`)

    // check the differences (in milliseconds)
    console.log(`difference: ${(dayAfterTimestamp-originalTimestamp)}`)
    
    
       // displaying the original format (timezone independent)

At last if you want to return the date string as a YYYY-MM-DD format you may have to implement it yourself :-/ , or use toLocaleFormat method but it isn't standardized.

-1

The logic would be to add 24 hours in milliseconds to the current time. As an example:

var myDate = new Date();
var oneMoreDay = new Date();
oneMoreDay.setTime(myDate.getTime() + 86400000);
console.log(myDate.getDate());
console.log(oneMoreDay.getDate());

An additional day has been added to the oneMoreDay variable. In your specific example you just wanted to add one more day to the ORIGINAL variable, so i'd do something such as:

date.setTime(date.getTime() + 86400000);
Simeon Ikudabo
  • 2,152
  • 1
  • 10
  • 27
  • What if the user's somewhere in Asia though, where they have a positive timezone offset from UTC? Wouldn't that give a date two days from the provided date? – HartleySan Feb 18 '20 at 21:19
  • Not all days are 24 hours long where daylight saving is observed, see [*How can I add 1 day to current date?*](https://stackoverflow.com/questions/9989382/how-can-i-add-1-day-to-current-date?r=SearchResults&s=2|140.0549) – RobG Feb 18 '20 at 22:11