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

后端 未结 15 1537
情歌与酒
情歌与酒 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:45

    The JavaScript Date class handles this for you

    var d = new Date("2012-02-29")
    console.log(d)
    // Wed Feb 29 2012 11:00:00 GMT+1100 (EST)
    
    d.setDate(d.getDate() + 1)
    console.log(d)
    // Thu Mar 01 2012 11:00:00 GMT+1100 (EST)
    
    console.log(d.getDate())
    // 1
    
    0 讨论(0)
  • 2020-11-29 02:45
    var curDate = new Date().toLocaleString().split(',')[0];
    

    Simply! in dd.mm.yyyy format.

    0 讨论(0)
  • 2020-11-29 02:46

    Its really simple:

    1: Create date object with today' date and time. 2: Use date object methods to retrieve day, month and full year and concatenate them using the + operator.

    Visit http://www.thesstech.com/javascript/date-time JavaScript for detailed tutorial on date and time.

    Sample Code:

      var my_date = new Date();  
      var tomorrow_date =       (my_date .getDate()+1)  + "-" + (my_date .getMonth()+1) + "-" + my_date .getFullYear();
      document.write(tomorrow_date);
    
    0 讨论(0)
  • 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("<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.

    0 讨论(0)
  • 2020-11-29 02:51

    Method Date.prototype.setDate() accepts even arguments outside the standard range and changes the date accordingly.

    function getTomorrow() {
        const tomorrow = new Date();
        tomorrow.setDate(tomorrow.getDate() + 1); // even 32 is acceptable
        return `${tomorrow.getFullYear()}/${tomorrow.getMonth() + 1}/${tomorrow.getDate()}`;
    }
    
    0 讨论(0)
  • 2020-11-29 02:51
    function getMonday(d)
    {
       // var day = d.getDay();
       var day = @Config.WeekStartOn
       diff = d.getDate() - day + (day == 0 ? -6 : 0);
       return new Date(d.setDate(diff));
    }
    
    0 讨论(0)
提交回复
热议问题