.getTime() Alternative without milliseconds

Here a quick way to remove the milisecont from the getTime

var milli = new Date().getTime()
var timeWithoutMilli = Math.floor(milli/1000);

That will return the number of seconds


Simple arithmetic. If you want the value in seconds, divide the milliseconds result by 1000:

var seconds = new Date().getTime() / 1000;

You might want to call Math.floor() on that to remove any decimals though:

var seconds = Math.floor(new Date().getTime() / 1000);

Is there a different function I can use that will give me only the minutes since 1/1/1970, I just dont want the number to be as precise.

Certainly, divide the seconds by 60, or divide the milliseconds by 60000:

var minutes = Math.floor(new Date().getTime() / 60000);

var milliseconds = 1426515375925,
    seconds = Math.floor(milliseconds / 1000),  // 1426515375
    minutes = Math.floor(milliseconds / 60000); // 23775256

In my case (date without miliseconds), I wanted miliseconds to always be zero, so it would be like:

hh:mm:ss:000

Here is how I achived it:

var time = new Date().getTime();
// Make miliseconds = 0 (precision full seconds)
time -= time % 1000;

Maybe it will be useful for someone


The simplest way to remove the milliseconds:

Long date = new Date().getTime() / 1000 * 1000

1582302824091L becomes 1582302824000L
2020-02-21 17:33:44.091 becomes 2020-02-21 17:33:44.0

Tags:

Javascript