Calculate days left before a specific date using JavaScript

I was trying to calculate the days remaining until a certain date. I know there are a million different approaches and tutorials out there, but I wanted to write the code myself. The problem is that the output of the function is "NaN". I am very grateful 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 American date format (MM / dd / YYYY) and not UTC dates. I also know there is a Date.UTC () function, but anyway. I just wanted to turn around for months and days.

+3


source to share


3 answers


When you parse num

for installation date[x]

, you need to reset num

- ''

.

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

      



You can use String.split()

to split input into periods.

+1


source


My decision:

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 it would be better:

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

      

+1


source


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

      

0


source







All Articles