# 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?
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));
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Senior Software EngineerCommented:
Hi,

May be the issue with timestamp of hosted server.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.