如何使用jQuery检查日期与当前日期

编程入门 行业动态 更新时间:2024-10-24 16:27:47
本文介绍了如何使用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检查日期与当前日期

本文发布于:2023-10-31 07:21:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545418.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   如何使用   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!