JQuery Datepicker:如果选择的日期是今天的日期 [英] JQuery Datepicker: If selected date is today's date
问题描述
我可以通过使用以下方式确定所选日期是否过去:
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屋!