Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Milliseconds to a date

How would I go about to get the amount of milliseconds and a specific data. For Instance, i know how to create a date:

var d = new Date(2014,02,22,12,0,0,0);

and to get today's date:

(new Date()).getTime() + 10*24*60*60*1000;      

But now how would I go about to get the total milliseconds until this date occurs?

like image 726
Corne Avatar asked Oct 22 '25 05:10

Corne


2 Answers

var miliseconds = Math.abs(d - new Date());
var today = new Date() // empty constructor returns the "now" time.

Returns the number of miliseconds between the two dates. Using Math.abs ensures you always get a positive result.

like image 191
flavian Avatar answered Oct 23 '25 17:10

flavian


Well its a matter of subtracting.

var d = new Date(2014,02,22,12,0,0,0);
var today = new Date();

var ms = d-today;
document.write(ms);

Output:

28015951707
like image 24
Meoiswa Avatar answered Oct 23 '25 19:10

Meoiswa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!