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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8215556/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:09:47  来源:igfitidea点击:

How to check if input date is equal to today's date?

javascriptjquery

提问by JonoB

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
  • yyyy-mm-dd
  • yyyy/mm/dd
  • 日/月/年
  • dd-mm-yyyy
  • 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

回答by James Hill

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

回答by schellmax

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()”方法更干净,恕我直言

回答by Cengkaruk

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'。

回答by Phrogz

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* 方法可确保与同一全球日匹配的不同时区中的两个等效日相同。(如果您直接解析两个日期,则没有必要,但值得考虑。)

回答by Ravi Kant

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脚本中比较比较的一个链接

回答by L-Ray

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)

波浪号截断除法结果(请参阅有关整数除法的文章

回答by Diodeus - James MacFarlane

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

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

回答by Adwich

There is a simpler solution

有一个更简单的解决方案

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

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

这样你就不会失去附加的时间(inputDate如果有的话)

回答by Ronk

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

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

< = 也有效,虽然使用 =,但它可能必须匹配毫秒。