Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate ms since midnight in Javascript

What is the best way to calculate the time passed since (last) midnight in ms?

like image 745
Olga Avatar asked Jun 08 '12 07:06

Olga


People also ask

What does getTime() do?

The getTime() method returns the number of milliseconds since the ECMAScript epoch. You can use this method to help assign a date and time to another Date object. This method is functionally equivalent to the valueOf() method.

Is JavaScript time in milliseconds?

In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.

What is timestamp in JavaScript?

A Timestamp represents a point in time independent of any time zone or calendar, represented as seconds and fractions of seconds at nanosecond resolution in UTC Epoch time. It is encoded using the Proleptic Gregorian Calendar which extends the Gregorian calendar backwards to year one.


1 Answers

Create a new date using the current day/month/year, and get the difference.

var now = new Date(),     then = new Date(         now.getFullYear(),         now.getMonth(),         now.getDate(),         0,0,0),     diff = now.getTime() - then.getTime(); // difference in milliseconds 
like image 172
Niet the Dark Absol Avatar answered Sep 25 '22 06:09

Niet the Dark Absol