


You want the UTC date 5 days ago from NOW (UTC) var newDate = Date.now() + -5*24*3600*1000 // date 5 days ago in milliseconds UTC The JavaScript Date setDate () Method is used to set the date of a month into a date object which is created using the date () constructor. However, it works if you're doing your math all in UTC, egĪ. Using the method setDate(), programmers can add days, months, or years to the returned day and. toString() which returns the local date and therefore is incompatible with the Date.now() base date in UTC. Method Description setDate() For setting the day as a number. The getDate() method gives the current day of the month. assume (wrongly) that Date.now() gives you the current LOCAL now time, or.To expand on that, the answer above will have daylightsaving inaccuracies in the case where you want to calculate the LOCAL day 5 days ago in a timezone with daylightsaving changes and you JavaScript setDate() Method Set the day of the month: Date() d.setDate(15) Set the day of the month to be the last day of the previous month: Date() d. Date objects are created with the new Date () constructor.

You can display a timer on the webpage by the help of JavaScript date object. As s/he says "Some day in year have 23 hours, and some 25 due to timezone rules". The getDate() method gets the day from the given date, and the setDate() method sets the day of the month to the next day by. The JavaScript date object can be used to get year, month and day. NOTE: Date.now() doesn't work in older browsers (eg IE8 I think). So use Date.now() var newDate = Date.now() + -5*24*3600*1000 // date 5 days ago in millisecondsĪnd if you like it formatted new Date(newDate).toString() // or.
