简体   繁体   English

javascript如何获取下周的日期,并比较日期

[英]javascript how to get next week's date, and compare dates

I am new to javascript. 我是javascript新手。 I wonder if there's any built-in methods that I could use. 我想知道是否可以使用任何内置方法。

For example, given today is May 8 2014, I want my cell to show 'May 8 2014 - May 15 2014' In same month, I can just add the date by 7, but what about 'May 28 2014 - June 4 2014'? 例如,假设今天是2014年5月8日,我希望我的单元格显示“ 2014年5月8日-2014年5月15日”。在同一个月,我可以将日期加7,但是“ 2014年5月28日-2014年6月4日”呢? ? And how do I compare two dates? 以及如何比较两个日期? And how do I tell if a date is within that week period? 我该如何判断日期是否在该周内?

Thanks for any ideas. 感谢您的任何想法。

If you use the setDate function, it will add the number of days that you want, and you will not have to worry about changing month or year, it will done automatically. 如果使用setDate函数,它将添加所需的天数,而您不必担心更改月份或年份,它将自动完成。 To compare dates, you can simply use the > and < operators as you would do with any number (actually under the hood, a Date in Javascript is a number). 要比较日期,您可以像使用任何数字一样简单地使用><运算符(实际上,在Javascript中,Date是数字)。

Example: 例:

var now = new Date();

var nextWeek = new Date(now);
nextWeek.setDate(nextWeek.getDate() + 7);

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

if (tomorrow > now && tomorrow < nextWeek)
    alert('All good!');

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

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