在jquery中检查当前日期 [英] check date with current date in jquery

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

问题描述

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

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

请在下面找到代码<如果(Date.parse(new Date())> Date.parse($(#EndDate)。val() )){

//条件满足今天的日期。

}

所以我的结束日期是今天date.but仍然是当前日期大于结束日期。为什么我如何检查并验证这一点。我明白一些时间值大于结束日期。但是我只想检查日期/月/年不是时间。

解决方案

如果:

  $(#EndDate)。val(); 

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

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

要创建可比较的日期,请执行以下操作:

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

所以现在你可以做:

  if(parseDate($(#EndDate)。val())> today){
//日期大于今天
}

或者您真的必须:

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

请注意,当您执行以下操作:

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

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



Date.parse 需要一个看起来像日期和时间的字符串,所以如果你传递一个像1399903200000这样的数字时间值,那么这个实现将回到一些实现启发式将其转换为日期或 NaN



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


i want to check my date is greater than current date

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

Please find below code

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.

解决方案

If:

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

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

So now you can do:

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

or if you really must:

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

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

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天全站免登陆