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

时间:2022-09-18 13:00:11

I have a form input with an id of 'date_trans'. The format for that date input (which is validated server side) can be any of:

我有一个id为'date_trans'的表单输入。该日期输入的格式(有效的服务器端)可以是以下任何一种:

  • dd/mm/yyyy
  • 日/月/年
  • dd-mm-yyyy
  • DD-MM-YYYY
  • yyyy-mm-dd
  • YYYY-MM-DD
  • yyyy/mm/dd
  • YYYY / MM / DD

However, before posting the form, I'd like to check if the date_trans field has a date that is equal to today's date. Its ok if the date taken is the client's date (i.e. it uses js), since I run a double check on the server as well.

但是,在发布表单之前,我想检查date_trans字段的日期是否等于今天的日期。如果所用的日期是客户的日期(即它使用js),那就好了,因为我也在服务器上运行了一次双重检查。

I'm totally lost on how to do the date comparrison in jQuery or just plain old javascript. If it helps, I am using the jquery datepicker

我完全迷失了如何在jQuery或简单的旧javascript中进行日期比较。如果它有帮助,我使用jquery datepicker

10 个解决方案

#1


-6  

Have you looked at datejs? (using the appropriate globalization pack to parse dd/mm/yyyy correctly)

你看过datejs吗? (使用适当的全球化包正确解析dd / mm / yyyy)

Just pass the date in to its parse() method and compare with today's date.

只需将日期传递给它的parse()方法,并与今天的日期进行比较。

#2


143  

A simple date comparison in pure JS should be sufficient:

纯JS中的简单日期比较应该足够了:

// 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.

这是一个有效的JSFiddle。

#3


57  

for completeness, taken from this solution:

为了完整性,取自此解决方案:

You could use toDateString:

你可以使用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

没有库依赖关系,并且看起来比之前的答案中显示的'setHours()'方法更清晰,imho

#4


19  

Try using moment.js

尝试使用moment.js

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

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

如果需要,您可以将'day'字符串替换为'year,month,minute'。

#5


7  

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.)

使用UTC *方法可确保在同一全球日匹配的不同时区中的两个等效天数相同。 (如果您直接解析这两个日期,则没有必要,但是要考虑好事。)

#6


4  

Just use the following code in your javaScript:

只需在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

根据您的要求更改条件。这是java脚本中比较比较的一个链接

#7


1  

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

就像你没有失去附加到inputDate的时间(如果有的话)

#8


0  

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

Date.js是一个方便的库,用于操作和格式化日期。它可以帮助在这种情况下。

#9


0  

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)

tilde截断除法结果(参见这篇关于整数除法的文章)

#10


-1  

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

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

<=也可以,虽然有=,它可能必须匹配毫秒。

#1


-6  

Have you looked at datejs? (using the appropriate globalization pack to parse dd/mm/yyyy correctly)

你看过datejs吗? (使用适当的全球化包正确解析dd / mm / yyyy)

Just pass the date in to its parse() method and compare with today's date.

只需将日期传递给它的parse()方法,并与今天的日期进行比较。

#2


143  

A simple date comparison in pure JS should be sufficient:

纯JS中的简单日期比较应该足够了:

// 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.

这是一个有效的JSFiddle。

#3


57  

for completeness, taken from this solution:

为了完整性,取自此解决方案:

You could use toDateString:

你可以使用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

没有库依赖关系,并且看起来比之前的答案中显示的'setHours()'方法更清晰,imho

#4


19  

Try using moment.js

尝试使用moment.js

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

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

如果需要,您可以将'day'字符串替换为'year,month,minute'。

#5


7  

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.)

使用UTC *方法可确保在同一全球日匹配的不同时区中的两个等效天数相同。 (如果您直接解析这两个日期,则没有必要,但是要考虑好事。)

#6


4  

Just use the following code in your javaScript:

只需在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

根据您的要求更改条件。这是java脚本中比较比较的一个链接

#7


1  

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

就像你没有失去附加到inputDate的时间(如果有的话)

#8


0  

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

Date.js是一个方便的库,用于操作和格式化日期。它可以帮助在这种情况下。

#9


0  

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)

tilde截断除法结果(参见这篇关于整数除法的文章)

#10


-1  

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

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

<=也可以,虽然有=,它可能必须匹配毫秒。