Adding a Date in Javascript -


i'm trying use pattern described here: how add number of days today's date?

my goal start animation @ date 1 week before event, in case launch of sputnik. code is:

        var sputniklaunchdate = new date(1957, 9, 4); //the first event of insterest in simulation.         var earliestanimationdate = new date();         earliestanimationdate.setdate(sputniklaunchdate.getdate() - 7); //start 1 week before 

when in firefox debugger, variable sputniklauchdate correct (1957-10-04t05:00:00.000z). however, earliestanimationdate ends being 2015-05-28t18:49:54.313z , have no idea why. can explain me i'm doing wrong?

the problem setdate sets number of days relative current time, when constructed earliestanimationdate didn't give information, set today's date. pass in sputniklaunchdate.gettime() copy time earliestanimationdate (or else put new date(1957, 9, 4) again, of course).

var sputniklaunchdate = new date(1957, 9, 4); var earliestanimationdate = new date(sputniklaunchdate.gettime()); earliestanimationdate.setdate(sputniklaunchdate.getdate() - 7); 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -