如何检查输入日期是否等于今天的日期?

我有一个 id 为‘ date _ trans’的表单输入。该日期输入的格式(经过验证的服务器端)可以是:

  • Dd/mm/yyyy
  • 好吧
  • 呀呀呀
  • Yyyy/mm/dd

但是,在发布表单之前,我想检查 date _ trans 字段的日期是否等于今天的日期。如果采取的日期是客户端的日期(即它使用 js) ,这没有问题,因为我也在服务器上运行双重检查。

我完全不知道如何在 jQuery 或者普通的旧 javascript 中进行日期比较。如果有帮助的话,我正在使用 Jquery 数据采集器

196638 次浏览
function sameDay( d1, d2 ){
return d1.getUTCFullYear() == d2.getUTCFullYear() &&
d1.getUTCMonth() == d2.getUTCMonth() &&
d1.getUTCDate() == d2.getUTCDate();
}


if (sameDay( new Date(userString), new Date)){
// ...
}

Using the UTC* methods ensures that two equivalent days in different timezones matching the same global day are the same. (Not necessary if you're parsing both dates directly, but a good thing to think about.)

Date.js is a handy library for manipulating and formatting dates. It can help in this situation.

A simple date comparison in pure JS should be sufficient:

// Create date from input value
var inputDate = new Date("11/21/2011");


// Get today's date
var todaysDate = new Date();


// call setHours to take the time out of the comparison
if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
// Date equals today's date
}

Here's a working JSFiddle.

Just use the following code in your javaScript:

if(new Date(hireDate).getTime() > new Date().getTime())
{
//Date greater than today's date
}

Change the condition according to your requirement.Here is one link for comparision compare in java script

Try using moment.js

moment('dd/mm/yyyy').isSame(Date.now(), 'day');

You can replace 'day' string with 'year, month, minute' if you want.

for completeness, taken from this solution:

You could use toDateString:

var today = new Date();
var isToday = (today.toDateString() == otherDate.toDateString());

no library dependencies, and looking cleaner than the 'setHours()' approach shown in a previous answer, imho

TodayDate = new Date();
if (TodayDate > AnotherDate) {} else{}

< = also works, Although with =, it might have to match the milliseconds.

The following solution compares the timestamp integer divided by the values of hours, minutes, seconds, millis.

var reducedToDay = function(date){return ~~(date.getTime()/(1000*60*60*24));};
return reducedToDay(date1) == reducedToDay(date2)

The tilde truncs the division result (see this article about integer division)

There is a simpler solution

if (inputDate.getDate() === todayDate.getDate()) {
// do stuff
}

like that you don't loose the time attached to inputDate if any

The Best way and recommended way of comparing date in typescript is:

var today = new Date().getTime();
var reqDateVar = new Date(somedate).getTime();


if(today === reqDateVar){
// NOW
} else {
// Some other time
}

Try this

// method to check date is less than today date
isLessDate(schedule_date : any){
var _schedule_date = new Date(schedule_date);
var date = new Date();
var transformDate = this.datePipe.transform(date, 'yyyy-MM-dd');
var _today_date = new Date(''+transformDate);
if(_schedule_date < _today_date){
return 'small'
}
else if(_schedule_date > _today_date){
return 'big'
}
else {
return 'same'
}
}