javascript - get next day of a string
Asked Answered
A

9

8

I've a var example = "05-10-1983"

How I can get the "next day" of the string example?

I've try to use Date object...but nothing...

Aged answered 21/7, 2009 at 18:19 Comment(1)
Is the string always going to be in MM-DD-YYY format?Chiron
G
12

This would do it for simple scenarios like the one you have:

var example = '05-10-1983';
var date = new Date();
var parts = example.split('-');
date.setFullYear(parts[2], parts[0]-1, parts[1]); // year, month (0-based), day
date.setTime(date.getTime() + 86400000);
alert(date);

Essentially, we create an empty Date object and set the year, month, and date with the setFullYear() function. We then grab the timestamp from that date using getTime() and add 1 day (86400000 milliseconds) to it and set it back to the date using the setTime() function.

If you need something more complicated than this, like support for different formats and stuff like that, you should take a look at the datejs library which does quite a bit of work for you.

Greathouse answered 21/7, 2009 at 18:25 Comment(2)
Is there a leap year/second problem with using date.setDate(date.getDate() + 1);?Billbillabong
Is your code available with the example date in DD-MM-YYYY or MM-DD-YYYY format? I think it is MM-DD-YYYY format but I also want to confirm with you.Whoremaster
P
5

You can do the following:

var nextDay;
var example = "05-10-1983";

nextDay = new Date(example);
nextDay.setDate(nextDay.getDate() + 1);

#getDate/#setDate gets/sets the current day of the month (1-31).

After the above is run, nextDay will be set to whatever tomorrow's date is. This will also rollover to the next month / year if it's the end of the month, and even handle leap years. :)

Phonolite answered 10/2, 2013 at 23:29 Comment(0)
H
4
new Date(+new Date('05-10-1983'.replace(/-/g,'/')) + 24*60*60*1000)
Hark answered 21/7, 2009 at 18:32 Comment(0)
S
3

The problem with the +86400000 approach is the potential for error when crossing a daylight savings time barrier. For example, I'm on EST. If I do this:

var d = new Date("11/04/2012 00:00:00");
var e = new Date(d.getTime() + 86400000);

e is going to be 11/4/2012 23:00:00 If you then extract just the date portion, you get the wrong value. I recently hit upon this issue while writing a calendar control.

this will do it better (and with a flexible offset which will let you do more than 1 day in the future):

function getTomorrow(d,offset) {
    if (!offset) { offset = 1 }
    return new Date(new Date().setDate(d.getDate() + offset));
}
Stagecoach answered 13/10, 2011 at 20:10 Comment(1)
Your solution would roll back to the beginning of the month if d+offset crosses month boundaries, but Date()+offset does not. You need to anchor the new base date to which you add days to your original date. So instead of: new Date(new Date().setDate(d.getDate() + offset)); it should be: new Date(new Date(d.getTime()).setDate(d.getDate() + offset)); Otherwise, you do the arithmetic with relation to the month of the current Date() + offset, not the d Date given in the function argument + offset.Quest
R
2

So

var d = new Date("11/04/2012 00:00:00");
var e = new Date(d.getTime() + 86400000);

doesn't work because of daylight saving barriers. I ran into the same problem. I ended up doing something like this:

function next_day(date) {
  var e = new Date(date.getTime() + 24 * 60 * 60 * 1000);
  if e.getHours() != date.getHours() {
    e = new Date(e.getTime() + (e.getHours() - date.getHours()) * 60 * 60 * 1000)
  }
  return e;
}
Rectifier answered 25/10, 2012 at 11:57 Comment(1)
Haven't tested it, but it's an interesting solution for the DST problem that I didn't think of. I can't think of any other scenarios where this won't work.Melina
G
1

You can find out day index by getDay() function and create an array of days strings in following manner-

day = new Date(YourDate);

var dayArray = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];

day = dayArray[day.getDay()+1];
Grapher answered 11/7, 2013 at 9:42 Comment(0)
H
0

You can use framework called php.js. Google for it. This includes the advanced date functions and more

Hereabouts answered 21/7, 2009 at 18:23 Comment(1)
This is pretty straightforward to do with navtive js, I wouldn't go straight to another "framework."Sixtieth
M
0

There are leap seconds, leap days, DST, etc., so this can be a tricky problem to solve in all cases.

In my opinion, the best way to address this (without a date library) is to take advantage of the Date constructor overflow feature[1]:

main();

function main() {
  var date = uniqueDateParse( '05-10-1983' );
  var newDate = nextDay( date );

  print( date );
  print( newDate );
}

function uniqueDateParse( string ) {
  var stringArray = string.split( '-', 3 );
  var month = stringArray[ 0 ],
      day   = stringArray[ 1 ],
      year  = stringArray[ 2 ];
  // Per ISO 8601[2], using Pacific Daylight Time[3].
  var dateString = year + '-' + month + '-' + day + 'T00:00:00-07:00';
  return new Date( dateString );
} 

function nextDay( date ) {
  return new Date( date.getFullYear()
                 , date.getMonth()
                 , date.getDate() + 1 );
}

function print( object ) {
  console.log( object );
}

Links

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Parameters

[2] http://www.w3.org/TR/NOTE-datetime

[3] http://www.timeanddate.com/time/zones/pdt

Mandola answered 27/7, 2015 at 22:54 Comment(0)
C
0
const today = new Date('2024-01-09') // get today's date
const tomorrow = new Date(today)
tomorrow.setDate(today.getDate() + 1) // Add 1 to today's date and set it to tomorrow
console.log("Tomorrow is", tomorrow.toDateString()) 
Criticaster answered 12/1 at 5:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.