如何使用jQuery检查日期与当前日期 [英] How to check date with current date using jQuery

查看:400
本文介绍了如何使用jQuery检查日期与当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我的日期是否大于当前日期

I want to check my date is greater than current date

$("#EndDate").val() ="5/13/2014" ->M/d/y

请在下面找到代码

if (Date.parse(new Date()) > Date.parse($("#EndDate").val())) { 

 //condition satisfied for today date too.

}

所以我的结束日期是今天的日期。但仍然是当前日期大于结束日期。为什么?我该如何检查和验证这一点。我知道某些时间值大于结束日期。但我想只检查日期/月/年而不是时间。

so my end date is today date.but still current date greater than end date. why ? how can i check and validate this. i understood some time value is greater than end date. but i want to check only date/month/year not time.

推荐答案

如果:

$("#EndDate").val();

以m / d / y格式返回一个字符串,您可以使用以下命令将其转换为日期对象:

returns a string in m/d/y format, you can turn that into a date object using:

function parseDate(s) {
  var b = s.split(/\D/);
  return new Date(b[2], --b[0], b[1]);
}

要创建可比日期,请执行您已经在做的事情:

To create a comparable date, do what you are already doing:

var today = new Date();
today.setHours(0,0,0,0);

现在你可以这样做:

if (parseDate($("#EndDate").val()) > today) {
  // date is greater than today
}

或者如果你真的必须:

if (+parseDate($("#EndDate").val()) > new Date().setHours(0,0,0,0)) ...

请注意,当你这样做时:

Please note that when you do:

Date.parse(new Date().setHours(0, 0, 0, 0))



<首先,从 new Date()创建一个新日期。调用 setHours()设置时间值,但调用的返回值是Date对象的UTC时间值。

firstly a new date is created from new Date(). Calling setHours() sets the time value, but the return value from the call is the UTC time value of the Date object.

Date.parse 期望一个看起来像日期和时间的字符串,所以如果你传递一个像1399903200000这样的数字时间值,那么实现将会回到某些实现启发式把它变成日期或 NaN

Date.parse expects a string that looks something like a date and time, so if you pass it a number time value something like 1399903200000, the implementation will fall back to some implementation heuristics to either turn it into a Date or NaN.

所以请不要这样做。使用 Date.parse 解析任何字符串是依赖于实现的(甚至是ECMA5中指定的字符串),并且将在不同的浏览器中返回不同的结果。所以请不要这样做。

So please don't do that. Parsing any string with Date.parse is implementation dependent (even the strings specified in ECMA5) and will return different results in different browsers. So please don't do that either.

这篇关于如何使用jQuery检查日期与当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆