如何将以前的DateTime与JavaScript中的当前DateTime进行比较

时间:2015-05-14 13:52:30

标签: javascript datetime

我正在创建预订系统,我想确保用户不要错误地选择以前的日期。我希望以下伪代码在JavaScript中:

if(StartDateTime < Now && EndDateTime < Now)
{    
    alert("Bookings cannot be created for a previous date");
} 

1 个答案:

答案 0 :(得分:1)

var startDate = '5/5/2015'; // mm/dd/yyyy formate
var StartDateTime = new Date(startDate).getTime();
var endDate = '5/10/2015';
var EndDateTime = new Date(endDate).getTime();
var Now = new Date().getTime();
if(StartDateTime < Now && EndDateTime < Now){    
    alert("Bookings cannot be created for a previous date");
}