0

I am trying to find the difference between two dates. The dates are got with jquery and I am using datejs too. When using datejs it picks up my date as US thinking it is MM/DD/YYYY instead of dd-mm-yyyy. My result for difference is NaN. How do I work this out. Am I miles out or anywhere near close?

var msMinute = 60*1000, 
    msDay = 60*60*24*1000;

start = $('#reconcile_start_date').val();   // 10-12-2014 | dd-mm-yyyy
end = $('#reconcile_end_date').val();           // 15-12-2014 | dd-mm-yyyy

start = new Date(start);
end   = new Date(end);

console.log(Math.floor((end - start) / msDay) + ' full days between ' + end + ' and ' + start);
difference = Math.floor((end - start) / msDay);

if(difference > 30){}
Pierce McGeough
  • 3,016
  • 8
  • 43
  • 65
  • 2
    This library can be pretty helpful: http://momentjs.com/ – emerson.marini Dec 10 '14 at 11:47
  • I think its more to do with how I am using datejs. It doesnt like new Date('10-12-2014) for some reason. Is there a way to format that? I can't see (or maybe I missed it) in the documentation – Pierce McGeough Dec 10 '14 at 11:51
  • 1
    If you pass the parameters to the `Date` constructor one by one (year, month, day) you'll get what you're expecting. Don't pass it as a string. – emerson.marini Dec 10 '14 at 11:55
  • Using [moment.js](http://momentjs.com/docs/#/displaying/difference/) is probably the easiest way to do it – u.k Dec 10 '14 at 12:10

3 Answers3

1

try this:

$(document).ready(function(){

    var msMinute = 60*1000; 
    var msDay = 60*60*24*1000;

    var start = '10-12-2014'; // October 12
    var statarr=start.split('-');

    var end = '12-15-2014'; // December 15
    var endarr=end.split('-');

    var dstart = new Date(statarr[0]+'/'+statarr[1]+'/'+statarr[2]).getTime();
    var dend   = new Date(endarr[0]+'/'+endarr[1]+'/'+endarr[2]).getTime();

    var diff = parseInt(dend-dstart);

    console.log(Math.floor(diff / msDay) + ' full days between ' + end + ' and ' + start);
    difference = Math.floor((end - start) / msDay);

    if(difference > 30){
    }
    });

// for UK formate use this:

var start = '12-10-2014'; // October 12
    var statarr=start.split('-');

    var end = '15-12-2014'; // December 15
    var endarr=end.split('-');

    var dstart = new Date(statarr[1]+'/'+statarr[0]+'/'+statarr[2]).getTime();
    var dend   = new Date(endarr[1]+'/'+endarr[0]+'/'+endarr[2]).getTime();

and rest is same.

Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
0

The issue is around the parsing of the dates, by default JS wont parse the date in the format.

Some more examples on how to convert the date format from UK format can be found (Why does Date.parse give incorrect results?)

More information of the dateString param, formats and browser behaviour - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

See this sample. http://jsbin.com/fayerihipu/2/

$(document).ready(function(){

var msMinute = 60*1000, 
var msDay = 60*60*24*1000;

var start = '10-12-2014'; // October 12
var end = '12-15-2014'; // December 15

var dstart = new Date(start).getTime();
var dend   = new Date(end).getTime();

var diff = parseInt(dend-dstart);

console.log(Math.floor(diff / msDay) + ' full days between ' + end + ' and ' + start);
difference = Math.floor((end - start) / msDay);

if(difference > 30){
}
});
Community
  • 1
  • 1
LiamB
  • 18,243
  • 19
  • 75
  • 116
0

Officially, the only date format supported by JavaScript is a simplified version of ISO-8601: yyyy-mm-dd, and almost all browsers also support yyyy/mm/dd as well.

So you need to do something like this to parse your dates:

var parts = start.split('-');
start = new Date(parseInt(parts[2], 10),
              parseInt(parts[1], 10) - 1,
              parseInt(parts[0], 10));
//Date uses zero-based month numbers, and so we have to subtract one from the month number

Take a look here for more details.

Community
  • 1
  • 1
Victor
  • 5,043
  • 3
  • 41
  • 55