I was trying to calculate the days left until a specific date. I know that there are a million different approaches and tutorials about it, but I wanted to write a code by myself. The problem is that the output of the function is "NaN". I am very thankful for your help.
This is my code:
var daysLeft = function(input) {
var num = '';
var date = [];
var x = 0;
for (i = 0; i < input.length; i++) {
if (!isNaN(input.charAt(i))) {
num += input.charAt(i);
}
else {
date[x] = parseInt(num, 10);
x++;
}
}
var inputDate = new Date(date[2], date[1], date[0]);
var today = new Date();
var timeDiff = Math.abs(inputDate.getTime() - today.getTime());
return Math.ceil(timeDiff / (1000*3600*24));
};
daysLeft("11.12.2014");
BTW: I wrote this code, because the Date() function works with the American format of Dates (MM/dd/YYYY) and not with UTC dates. I am also aware that there is the Date.UTC() function, but anyway. I just wanted to turn around months and days myself.