Question

i had a departureDate = date string(16-Apr-2012), time String(05:00), and TimeZone(Asia/Dubai) and arraivalDate = date string(16-Apr-2012), time String(02:00),and TimeZone(America/New_York)

now i need to compare the above dates ie.,

if (departureDate > arraivalDate) {
    // do something
}

Purpose of this comparision is to check arraival time is before departure time.. is there any function in javascript to do this? Please provide the whole function..

Was it helpful?

Solution

Look at the Date object documentation.

There is the getTime method that allows you to get a numeric value corresponding to the time for the specified date according to universal time. Which means that the value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC.

Which means you can do the following:

if (departureDate.getTime() > arrivalDate.getTime()) {
    // Do something
}

Provided you declared your dates the following way:

var departureDate = new Date(Date.UTC(year, month, day, hour, minute, second))
    arrivalDate = new Date(Date.UTC(year, month, day, hour, minute, second))

So that it respects the timezones. You need to specify UTC dates though. Javascript has no built-in way to handle timezones better than this. As my first sentence states it, look at the documentation about the Date object.

Edit: After talking with you in the chat, here is what I think you want:

According that you have the following:

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;

You can do this to compare both dates:

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
}

OTHER TIPS

Just as stated already, you'll still need to use getTime to extract actual seconds to compare. If you have numerical offsets you can simply add them to your dates before comparison.

// Assuming departure is in +4 TZ (Asia/Duabai) and arrival is in -4 TZ (America/New_York)
var departureMsec = departureDate.getTime() + (+4 * 3600 * 1000)
var arrivalMsec = arrivalDate.getTime() + (-4 * 3600 * 1000)
if (departureMsec > arrivalMsec) {
    // Do something
}

Things get more complicated if you don't have offsets as numbers or time zones in your input are not limited to some common subset. I'm not aware if there is a tzdata version with handling code for JavaScript, so I'll give you pointers how to do it by hand. If you work with limited amount of zones and none of them have daylight savings, then you can simply put offsets in hash:

var offsets={
    "Asia/Dubai": +4,
    "America/New_York": -4,
}

And use data from there. If you need arbitrary amount of zones, your best bet would be parsing tzdata to such a hash and implementing manual handling of correct daylight adjustments for every possible date, which gets harder and harder with each year, because with change of laws you'll have to support old conversions for old dates and new for new dates. I can add more details if you explain more about your needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top