Skip to content Skip to sidebar Skip to footer

Date Is Considering 31 Days Of Month

While working with date difference, when I am using below code somehow function is assuming that all the months have 31 days. For ex. if I am subtracting 01-March with 28-February

Solution 1:

This will give you the difference between two dates, in milliseconds

var diff = Math.abs(date1 - date2);

example, it'd be

var diff = Math.abs(newDate() - compareDate);

You need to make sure that compareDate is a valid Date object.

Something like this will probably work for you

var diff = Math.abs(newDate() - newDate(dateStr.replace(/-/g,'/')));

i.e. turning "2011-02-07 15:13:06" into new Date('2011/02/07 15:13:06'), which is a format the Date constructor can comprehend.

U can subtract like this--

var d1 = newDate(); //"now"var d2 = newDate("2011/02/01")  // some datevar diff = Math.abs(d1-d2);  // difference in millisecondsvar days = diff*24*60*60*1000;

Solution 2:

You code is actually subtracting March 1 from April 1, as the months in JavaScript dates are 0-based.

Solution 3:

var sysdt = "02/28/2013";
var date1 = newDate(sysdt);

var userdt = "03/01/2013"var date2 = newDate(userdt);

var days = (date2-date1)/(1000*24*60*60);

or subtract 1 from month in your code

varsysdt="02/28/2013";
varyear= sysdt.substring(6,10);
varmon= sysdt.substring(0,2)-1; // months are from 0 to 11vardate= sysdt.substring(3,5);
varn= Date.UTC(year,mon,date);



varuserdt="03/01/2013"varyr= userdt.substring(6,10);
varmn= userdt.substring(0,2)-1; // months are from 0 to 11vardd= userdt.substring(3,5);
varn1= Date.UTC(yr,mn,dd);

vardays= (n1-n)/(1000*24*60*60);

Post a Comment for "Date Is Considering 31 Days Of Month"