Javascript 验证日期是否在当前日期之前
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11344324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate if date is before date of current date
提问by John
Using this function, I'm getting a 7 days difference; how can I test whether a date is before the current date?
使用此功能,我得到了 7 天的差异;如何测试日期是否在当前日期之前?
function validateDate() {
pickedDate = Date.parse("05-Jul-2012".replace(/-/g, " "));
todaysDate = new Date();
todaysDate.setHours(0, 0, 0, 0);
dateDifference = Math.abs(Number(todaysDate) - pickedDate);
//7 Days=604800000ms
if (dateDifference > 604800000) {
return false;
} else {
return true;
}
}
回答by Hemant Metalia
You can directly compare both dates as
您可以直接将两个日期比较为
return pickedDate <= todaysDate
For exact date comparison considering milliseconds you can use JavaScript getMilliseconds() Method
对于考虑毫秒的确切日期比较,您可以使用 JavaScript getMilliseconds() 方法
You can parse date as you have done:
你可以像你所做的那样解析日期:
pickedDatestr = "09-Apr-2010"
var pickedDate = new Date(Date.parse(pickedDatestr.replace(/-/g, " ")))
回答by Christoph Bühler
For date comparison (without time):
对于日期比较(没有时间):
function isDateBeforeToday(date) {
return new Date(date.toDateString()) < new Date(new Date().toDateString());
}
isDateBeforeToday(new Date(2016, 11, 16));
Test cases:
测试用例:
// yesterday
isDateBeforeToday(new Date(2018, 12, 20)); // => true
// today
isDateBeforeToday(new Date(2018, 12, 21)); // => false
// tomorrow
isDateBeforeToday(new Date(2018, 12, 22)); // => false
回答by Ashwini Agarwal
Try this function
试试这个功能
function checkDate(day, month, year)
{
var regd = new RegExp("^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$");
var date = month + "/" + day + "/" + year;
var date = new Date(date);
var today = new Date();
var vdob = regd.test(date);
var err_txt = "" ;
if(date.getDate() != day || (date.getTime()>today.getTime()))
{
err_txt+=("Please select a valid Date.\n")
}
return (err_txt);
}
回答by Luke
The following will check whether a date occurs before today:
以下将检查日期是否发生在今天之前:
function isBeforeToday(){
var today = new Date((new Date()).toString().substring(0,15));
return date < today;
}
This works by creating a new date object after stripping all time information from its corresponding date string:
这是通过在从其相应的日期字符串中去除所有时间信息后创建一个新的日期对象来实现的:
Tue Mar 06 2018 16:33:15 GMT-0500 (EST)
-> Tue Mar 06 2018
-> Tue Mar 06 2018 00:00:00 GMT-0500 (EST)
Tue Mar 06 2018 16:33:15 GMT-0500 (EST)
-> Tue Mar 06 2018
->Tue Mar 06 2018 00:00:00 GMT-0500 (EST)
回答by Avittan E
if(this.dateString1.getFullYear() <= this.dateString2.getFullYear() )//check the year
{
// console.log("date1+date2"+this.dateString1.getFullYear()+this.dateString2.getFullYear())
if(this.dateString1.getMonth() <= this.dateString2.getMonth())//check the month
{
// console.log("date1+date2"+this.dateString1.getMonth()+this.dateString2.getMonth())
if(this.dateString1.getDate() < this.dateString2.getDate())//check the date
this.toastr.error("Project Start Date cannot be Previous Date");
return;
}
}
回答by Filip Savic
I came to this question because I was trying to check if my Java.util.Date that I was getting from my back-end was after today's date.
我来到这个问题是因为我试图检查我从后端获取的 Java.util.Date 是否在今天的日期之后。
In the end I found a really simple solution - by comparing milliseconds, like this:
最后我找到了一个非常简单的解决方案 - 通过比较毫秒,如下所示:
isAfterToday(date) {
return new Date(date).valueOf() > new Date().valueOf();
}
The function valueOf()
is documented here.
该函数valueOf()
记录在此处。
回答by Tuan
You can directly compare the 2 dates using '<, '>', etc.
您可以使用 '<、'>' 等直接比较 2 个日期。
function validateDate(date) {
//get start of day using moment.js
const now = Moment().startOf('day').toDate();
if (date < now) {
return false; //date is before today's date
} else {
return true; //date is today or some day forward
}