获取指定日期之前的天数Javascript [复制]

时间:2022-04-26 18:25:41

This question already has an answer here:

这个问题在这里已有答案:

I'm reading the value of the date input and need to workout if there is 42 days or more between the specified date and today's date. This is what I've tried so far:

我正在读取日期输入的值,如果在指定日期和今天的日期之间有42天或更长时间,则需要锻炼。这是我到目前为止所尝试的:

var sdate = new Date($("#holiday-editor input[name=StartDate]").val()); //This is returing date in the string format
var priorDate = new Date().setDate(sdate - 42).toString(); // This is returning some abstract int value
var dateNow = new Date().getDate().toString(); // this is returning 5 even though I'd like to get today's date in the string format
if (dateNow > priorDate) {
    $("#HolidayBookedLate").show();
}

1 个解决方案

#1


0  

If you manipulate dates a lot in your app I'd suggest to use moment.js. It's only 15kb but it has lots of useful features to work with dates.

如果您在应用程序中操作日期很多,我建议使用moment.js。它只有15kb,但它有很多有用的功能来处理日期。

In your case you can use diff function to get amount of days between two dates.

在您的情况下,您可以使用diff函数来获取两个日期之间的天数。

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1

#1


0  

If you manipulate dates a lot in your app I'd suggest to use moment.js. It's only 15kb but it has lots of useful features to work with dates.

如果您在应用程序中操作日期很多,我建议使用moment.js。它只有15kb,但它有很多有用的功能来处理日期。

In your case you can use diff function to get amount of days between two dates.

在您的情况下,您可以使用diff函数来获取两个日期之间的天数。

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1