Welcome to WebmasterWorld Guest from 54.92.153.90

Forum Moderators: open

Message Too Old, No Replies

IE and Date Difference Javascript

     
5:12 pm on Jan 7, 2009 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 4, 2005
posts:621
votes: 0


Hi there,

The code below works fine in Firefox. In IE it gives me a NAN value when I alert newcheckin or newcheckout.

Anybody know why?

Thanks for your help!

function dayDiff()
{
var checkin = document.getElementById('CHECK_IN').value;

var checkout = document.getElementById('CHECK_OUT').value;

var arrival = checkin.split("-");

var departure = checkout.split("-");

var newcheckin = new Date(arrival[2]+","+arrival+","+arrival[0]);

var newcheckout = new Date(departure[2]+","+departure[1]+","+departure[0]);

var one_day=1000*60*60*24;

if(newcheckin !="NaN" && newcheckout !="NaN")

//Assigning the Difference in days to the control

var diff = Math.ceil((newcheckin-newcheckout)/(one_day)) * -1;

return diff;

}

[1][edited by: Pico_Train at 5:23 pm (utc) on Jan. 7, 2009]

5:23 pm on Jan 7, 2009 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 4, 2005
posts:621
votes: 0


Function above isn't displaying correctly...use this instead

function dayDiff()
{
var checkin = document.getElementById('CHECK_IN').value;

var checkout = document.getElementById('CHECK_OUT').value;

var arrival = checkin.split("-");

var departure = checkout.split("-");

var newcheckin = new Date(arrival[2]+","+arrival[1]+","+arrival[0]);

var newcheckout = new Date(departure[2]+","+departure[1]+","+departure[0]);

var one_day=1000*60*60*24;

if(newcheckin !="NaN" && newcheckout !="NaN")

//Assigning the Difference in days to the control

var diff = Math.ceil((newcheckin-newcheckout)/(one_day)) * -1;

return diff;

}

7:17 pm on Jan 7, 2009 (gmt 0)

Preferred Member

10+ Year Member

joined:July 5, 2005
posts:352
votes: 0


In the following lines;
var newcheckin = new Date(arrival[2]+","+arrival[1]+","+arrival[0]);

var newcheckout = new Date(departure[2]+","+departure[1]+","+departure[0]);

You don't need to concatenate the comas;

var newcheckin = new Date(arrival[2],arrival[1],arrival[0]);

var newcheckout = new Date(departure[2],departure[1],departure[0]);

That should work fine assuming the values in CHECK_IN and CHECK_OUT are correct.

7:41 pm on Jan 7, 2009 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 4, 2005
posts:621
votes: 0


Perfect! Thanks for the advice!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members