lundi 2 février 2015

NaN javascript error when calculating between dates with timestamp


I would like to begin by saying i looked at multiple threads in this forum before posting. Wasnt able to find my solution :(


Issue: getting a NaN error when trying to find the difference between two dates with a timestamp from two textboxes.


The date format i'm using is DDMMYYYY HH:MM - 27/01/2015 00:00 code below.


thank you in advance for this super helpful forum :)



function stringToDate(s) {
var dateParts = s.split(' ')[0].split('-');
var timeParts = s.split(' ')[1].split(':');
var d = new Date(dateParts[0], --dateParts[1], dateParts[2]);
d.setHours(timeParts[0], timeParts[1], timeParts[2]);

return d;
}

function test() {
var a = textbox_1.value;
var b = textbox_2.value;
alert(stringToDate(a) - stringToDate(b));
}




Aucun commentaire:

Enregistrer un commentaire