JQuery Datepicker:如果选择的日期是今天的日期 [英] JQuery Datepicker: If selected date is today's date

查看:145
本文介绍了JQuery Datepicker:如果选择的日期是今天的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过使用以下方式确定所选日期是否过去:

  var due_date = $('# due_date')。val(); 
if(new Date(due_date).getTime()< new Date()。getTime())
{
// do stuff
}

^这工作很好



我正在使用以下内容来确定选择的日期是今天的日期:

  var due_date = $('#due_date')。 
var today = new Date()。getTime();
if(new Date(due_date).getTime()== today)
{
alert('ok');
}

但是没有达到该警报。上述陈述有什么问题?

解决方案

字符串30/03/2012用于创建日期时一个Date对象,表示2012年3月30日午夜。当您调用新的Date()时,它将创建一个表示当前时间(包括秒和毫秒)的Date对象。 / p>

您需要将Date对象的小时,分​​钟,秒和毫秒属性设置为0,以便它们代表完全相同的时间,使用 setHours() setMinutes()等功能。



有关Date对象的信息,请查看 MDN条目


I am able to determine if the selected date is in the past by using:

var due_date = $('#due_date').val();
if(new Date(due_date).getTime() <  new Date().getTime())
{
  //do stuff
}

^This works fine

I am using the following to determine if a selected date is today's date:

var due_date = $('#due_date').val();
var today = new Date().getTime();
if(new Date(due_date).getTime() == today)
{
    alert('ok');
}

But it's not hitting that alert. What is wrong with the above statement?

解决方案

The string "30/03/2012" when used to create a date results in a Date object that represents midnight on the 30 March 2012. When you call new Date() it creates a Date object that represents the current time (including seconds and milliseconds).

You'll need to set the hour, minute, second and millisecond properties of your Date object to 0 so that they represent the exact same time, using the setHours(), setMinutes(), etc functions.

For more information about the Date object take a look at the MDN entry.

这篇关于JQuery Datepicker:如果选择的日期是今天的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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