查看Date 对象文档。
有一种getTime
方法可以让您根据通用时间获取与指定日期的时间相对应的数值。这意味着该getTime
方法返回的值是自 1970 年 1 月 1 日 00:00:00 UTC 以来的毫秒数。
这意味着您可以执行以下操作:
if (departureDate.getTime() > arrivalDate.getTime()) {
// Do something
}
前提是您通过以下方式声明日期:
var departureDate = new Date(Date.UTC(year, month, day, hour, minute, second))
arrivalDate = new Date(Date.UTC(year, month, day, hour, minute, second))
这样它就尊重时区。不过,您需要指定 UTC 日期。Javascript 没有比这更好的处理时区的内置方法。正如我的第一句话所述,请查看有关 Date 对象的文档。
编辑:在聊天中与您交谈后,这是我认为您想要的:
根据你有以下几点:
var orgti = document.getElementById('orginTimeZone').value;
var desct = document.getElementById('descTimeZone').value;
var selfwDepDate = document.forms[0].fwDepartureDate.value;
var selfwDepTime = document.forms[0].fwDepartureTime.value;
var selfwArrDate = document.forms[0].fwArrivalDate.value;
var selfwArrTime = document.forms[0].fwArrivalTime.value;
您可以这样做来比较两个日期:
var departureDate = new Date( Date.UTC( selfwDepDate + selfwDepTime ) ) // This creates a date with what you got
var arrivalDate = new Date( Date.UTC( selfwArrDate + selfwArrTime ) ) // This too
// Now the tricky part: you substract the timezone, so that you get the same referential for both dates
departureDate = departureDate.getTime() - ( orgit * 3600000 ) // Multiplying by 3600000 to transform hours into milliseconds since "getTime" returns milliseconds
arrivalDate = arrivalDate.getTime() - ( desct * 3600000 ) // Same
// Now, you've got two timestamps based on dates with the same referential. You can use:
if ( departureDate > arrivalDate ) {
// Do something
}