How to return an object by calculating dates?

Asked

Viewed 152 times

8

I am calculating the difference between dates in a period. From today until x previous days:

hoje = new Date();
periodo = new Date().setDate(hoje.getDate() - 5);

That way the output is as follows:

Fri Sep 19 2014 17:05:11 GMT-0300 (Local Standard Time)
1409861111749 

I want the calculated date to also return as object:

Fri Sep 14 2014 17:05:11 GMT-0300 (Local Standard Time) 

1 answer

8


You can create a new date from the timestamp returned by the setDate operation:

periodo = new Date(new Date().setDate(hoje.getDate() - 5))

According to the language specification, setDate always returns a Timeclip, which represents the number of milliseconds since 1st. January 1970 (UTC) until the date in question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.