Get tomorrow's date with getDay Javascript

前端 未结 2 1634
南笙
南笙 2021-01-05 11:03

What I am making is a weather forecast website, and what I need is the days of the week (ie. \"Sunday\", \"Monday\", etc.). To get tomorrow\'s date I am just putting \"+ 1\"

相关标签:
2条回答
  • 2021-01-05 11:29

    To add a day to a javascript Date object you would do :

    var date =new Date();
    //use the constructor to create by milliseconds
    var tomorrow = new Date(date.getTime() + 24 * 60 * 60 * 1000);
    

    Note, getDate.

    Date.getDay returns a number from 0-6 based on what day of the week it is.

    So you would do:

    var date =new Date();
    var tomorrow = new Date(date.getTime() + 24 * 60 * 60 * 1000);
    var twoDays = new Date(date.getTime() + 2 * 24 * 60 * 60 * 1000);
    var threeDays  = new Date(date.getTime() + 3 * 24 * 60 * 60 * 1000);
    
    document.getElementById('tomorrow').innerHTML = weekday[tomorrow.getDay()];
    document.getElementById('twodays').innerHTML = weekday[twoDays.getDay()];
    document.getElementById('threedays').innerHTML = weekday[threeDays.getDay()];
    

    Edit: Fixing typo

    0 讨论(0)
  • 2021-01-05 11:36

    Use (day.getDay() + i) % 7. That will only return results between 0-6.

    0 讨论(0)
提交回复
热议问题