daza166 daza166 - 1 month ago 8
Javascript Question

JavaScript how to get tomorrows date in format dd-mm-yy

I am trying to get JavaScript to display tomorrows date in format (dd-mm-yyyy)

I have got this script which displays todays date in format (dd-mm-yyyy)

var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
document.write("<b>" + day + "/" + month + "/" + year + "</b>")

Displays: 25/2/2012 (todays date of this post)


But how do I get it to display tomorrows date in the same format i.e.
26/2/2012


I tried this:

var day = currentDate.getDate() + 1


However I could keep
+1
and go over 31 obviously there are not >32 days in a month

Been searching for hours but seems to be no answer or solution around this?

Answer

This should fix it up real nice for you.

If you pass the Date constructor a time it will do the rest of the work.

24 hours 60 minutes 60 seconds 1000 milliseconds

var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
document.write("<b>" + day + "/" + month + "/" + year + "</b>")

One thing to keep in mind is that this method will return the date exactly 24 hours from now, which can be inaccurate around daylight savings time.

Phil's answer work's anytime:

var currentDate = new Date();
currentDate.setDate(currentDate.getDate() + 1);

The reason I edited my post is because I myself created a bug which came to light during DST using my old method.