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...
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...
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.
date.setDate(date.getDate() + 1);
? –
Billbillabong 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. :)
new Date(+new Date('05-10-1983'.replace(/-/g,'/')) + 24*60*60*1000)
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));
}
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 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;
}
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];
You can use framework called php.js. Google for it. This includes the advanced date functions and more
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 );
}
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Parameters
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())
© 2022 - 2024 — McMap. All rights reserved.