简体   繁体   English

直接将天数添加到当前日期javascript

[英]Straight forward add days to current date javascript

I apologize as I'm sure this question has been answered already but there's so many different solutions from different years that my head is just spinning from viewing all the different solutions. 很抱歉,我确定这个问题已经得到回答,但我感到很抱歉,但是不同年份有许多不同的解决方案,以至于我只是查看所有不同的解决方案而已。 But can someone please help with providing a straight forward javascript solution to display (x) days after current date? 但是有人可以帮忙提供一个直接的JavaScript解决方案,以显示当前日期之后的(x)天吗?

All I'm trying to accomplish is having a notice on a page - "We are currently accepting bookings for March XX, 2017 or after" at the top. 我要做的就是在页面上发出通知-顶部显示“我们目前正在接受2017年3月XX日或之后的预订”。

OR is there anything I can to the code below to make it add (X) days as I'm using this to show current date. 或者我可以在下面的代码中添加任何内容以使其增加(X)天,因为我正在使用它来显示当前日期。

var MONTH_NAME = ['January', 'Febuary', 'March', 'April', 'May', 'June',
              'July', 'August', 'September', 'October', 'November', 'December'];
function showTime() {
function twoDigit(n) {
    return ('0' + n).slice(-2);
}
function iso8601(date) {
    return date.getFullYear() +
           '-' + twoDigit(1 + date.getMonth()) +
           '-' + twoDigit(date.getDate()) +
           'T' + twoDigit(date.getHours()) +
           ':' + twoDigit(date.getMinutes());
}
function en_US(date) {
    var h = date.getHours() % 12;
    return MONTH_NAME[date.getMonth()] +
           ' '  + date.getDate() +
           ', ' + date.getFullYear();
}

var timeEl = document.getElementById('time');
if (timeEl !== null) {
    var now = new Date();
    timeEl.innerHTML = en_US(now);
    timeDiv.setAttribute('datetime', iso8601(now));
}
};
setInterval(showTime, 1000);

Please and thank you. 谢谢,麻烦您了。

You can use setDate and getDate methods to solve your problem. 您可以使用setDategetDate方法来解决您的问题。 Take a look at addDays method I have written. 看看我编写的addDays方法。 Hope this helps. 希望这可以帮助。

 var MONTH_NAME = ['January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]; function showTime() { function twoDigit(n) { return ('0' + n).slice(-2); } function iso8601(date) { return date.getFullYear() + '-' + twoDigit(1 + date.getMonth()) + '-' + twoDigit(date.getDate()) + 'T' + twoDigit(date.getHours()) + ':' + twoDigit(date.getMinutes()); } function en_US(date) { var h = date.getHours() % 12; return MONTH_NAME[date.getMonth()] + ' ' + date.getDate() + ', ' + date.getFullYear(); } function addDays(date, days){ var newDate = new Date(date.getTime()); newDate.setDate(newDate.getDate() + days); return newDate; } var timeEl = document.getElementById('time'); if (timeEl !== null) { var now = new Date(); timeEl.innerHTML = en_US(now) + ' to ' + en_US(addDays(now, 15)); timeEl.setAttribute('datetime', iso8601(now)); } }; setInterval(showTime, 1000); 
 <div id="time"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM