Как сравнить только часть даты со значением даты
У меня есть две переменные:
date1 = Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
date2 = Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
Когда я сравниваю две даты, я получаю, что date2
больше, что мне нужно, правильно. Но я не хочу проверять временную часть двух дат, которые у меня есть. Как я мог получить только часть даты из этих двух дат и сравнить ее?
var today = new Date(); //Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
d = new Date(my_value); //Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
if(d>=today){ //I need to check the date parts alone.
alert(d is greater than or equal to current date);
}
Ответы
Ответ 1
Попробуйте очистить время, используя
Date.setHours(hour,min,sec,millisec)
код:
var today = new Date();
today.setHours(0, 0, 0, 0);
d = new Date(my_value);
d.setHours(0, 0, 0, 0);
if(d >= today){
alert(d is greater than or equal to current date);
}
Ответ 2
Лучший способ - изменить принятый ответ if
, как показано ниже.
if(d.setHours(0,0,0,0) >= today.setHours(0,0,0,0))
Таким образом, вы также можете легко проверить равенство, потому что тип возврата для setHours()
является целым числом.
Ответ 3
Попробуйте:
var today = new Date(); //Mon Nov 25 2013 14:13:55 GMT+0530 (IST)
var d = new Date(my_value); //Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
var todayDateOnly = new Date(today.getFullYear(),today.getMonth(),today.getDate()); //This will write a Date with time set to 00:00:00 so you kind of have date only
var dDateOnly = new Date(d.getFullYear(),d.getMonth(),d.getDate());
if(dDateOnly>=todayDateOnly){
alert(d is greater than or equal to current date);
}
Ответ 4
var StartDate = $("#StartDate").val();
var EndDate = $("#EndDate").val();
if ((( EndDate - StartDate)/ (86400000*7))<0)
{
alert("Start Date Must Be Earlier Than End Date"); $("#StartDate").focus();
error = true;
return false;
}