3

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Peter
  • 1,110
  • 10
  • 25

3 Answers3

1

When you parse num to set date[x], you need to reset num to ''.

...
else {
    date[x] = parseInt(num, 10);
    x++;
    num = '';
}

You might consider using String.split() to separate your input at the periods.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

My Solution is:

function isNumeric(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

var daysLeft = function(input) {
    var num = '';
    var date = [];
    var x = 0;
    for (i = 0; i < input.length; i++) {
        if (!isNaN(input.charAt(i)) && isNumeric(input.charAt(i))) {
            num += input.charAt(i);
        }
        else {
            date[x] = parseInt(num, 10);
            x++;
            num = '';
        }
    }
    date[x] = parseInt(num, 10);

    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));
};

But a better would be:

function parseDate(input) {
  var parts = input.split('-');
  // new Date(year, month [, day [, hours[, minutes[, seconds[, ms]]]]])
  return new Date(parts[0], parts[1]-1, parts[2]); // Note: months are 0-based
}

var daysLeft = function(input) {    
    var inputDate = parseDate(input);
    var today = new Date();
    var timeDiff = Math.abs(inputDate.getTime() - today.getTime());
    return Math.ceil(timeDiff / (1000*3600*24));
};
schnawel007
  • 3,982
  • 3
  • 19
  • 27
0

You should use something like this:

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++;
            num='';
        }
    }
    date[x] = parseInt(num, 10);
    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");