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

后端 未结 15 1604
情歌与酒
情歌与酒 2020-11-29 02:31

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)



        
15条回答
  •  天涯浪人
    2020-11-29 02:49

    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("" + day + "/" + month + "/" + year + "")
    

    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.

提交回复
热议问题