# Returning the wrong number of days.

For some reason the number of days the code is returning is 2 should be 1.  Here is my code below

``````Date.prototype.todaydiff = function(tstamp){
this.timestamp=tstamp;
var one_day=1000*60*60*24;

var today = new Date();
var dateA = new Date(new Date().getUnixTime());
/*Calculation for timezone offset */
var dateB = new Date(this.timestamp*1000 + today.getTimezoneOffset() * 60000);

// Convert both dates to milliseconds
var date1_ms = dateA.getTime();
var date2_ms = dateB.getTime();

// Calculate the difference in milliseconds
var difference_ms = date1_ms-date2_ms;

return Math.round(difference_ms/one_day);
};

var myDate = new Date()
console.log(myDate.todaydiff(1436227200));
``````
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
It seems close. You don't need to add the time zone offset. Also what is "getUnixTime()"? That's not a standard method.

Try this:
``````Date.prototype.todaydiff = function(tstamp){
this.timestamp=tstamp;
var one_day=1000*60*60*24;

var dateA = new Date();
var dateB = new Date(this.timestamp*1000);

// Convert both dates to milliseconds
var date1_ms = dateA.getTime();
var date2_ms = dateB.getTime();

// Calculate the difference in milliseconds
var difference_ms = date1_ms-date2_ms;

return Math.round(difference_ms/one_day);
};

var myDate = new Date()
//console.log(myDate.todaydiff(1436227200));
var myDate2 = new Date(myDate.getFullYear(),myDate.getMonth(),myDate.getDate()-1)
console.log(myDate.todaydiff(myDate2.getTime()/1000));
``````

Experts Exchange Solution brought to you by