JavaScript creating a Date x object?

Let's say I have a number x that can be anything (within reason). How can I create a new Date object that equals x number of seconds ago? I do not know how to approach this.

+4
source share
3 answers
var seconds = 5; var dateNow = new Date(); var date5SecondsAgo = new Date(dateNow.getTime() - seconds*1000); 
+5
source
 var now = new Date(); var seconds = 15; var before = new Date(now.getTime() - seconds*1000); 
+2
source

You can use the valueOf / getTime property to get the number of milliseconds since January 1, 1970, and then after milliseconds per second

 var milliSecondPerSecond = 1000; var myStartDate = new Date(myEndDateTime - numberOfSeconds * milliSecondPerSecond ); 
+1
source

Source: https://habr.com/ru/post/1401302/


All Articles