Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.getTime() Alternative without milliseconds

Tags:

javascript

Is there a way to use the Date().getTime() function without getting the time in milliseconds? If not is there a substitute for .getTime() which will only give me the precision in minutes?

Also I'm not sure how to just strip the milliseconds out of the date object.

var time = new Date().getTime()

Output: 1426515375925
like image 399
DomX23 Avatar asked Mar 16 '15 14:03

DomX23


People also ask

Is new Date () getTime () UTC?

Use the getTime() method to get a UTC timestamp, e.g. new Date(). getTime() . The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation. Calling the method from any time zone returns the same UTC timestamp.

What is new Date () getTime ()?

The date. getTime() method is used to return the number of milliseconds since 1 January 1970. when a new Date object is created it stores the date and time data when it is created. When the getTime() method is called on this date object it returns the number of milliseconds since 1 January 1970 (Unix Epoch).

Can I use date now ()?

now() method is used to return the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC. Since now() is a static method of Date, it will always be used as Date.

How do I get time stamps?

Getting the Current Time Stamp If you instead want to get the current time stamp, you can create a new Date object and use the getTime() method. const currentDate = new Date(); const timestamp = currentDate. getTime(); In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.


1 Answers

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
like image 141
James Donnelly Avatar answered Sep 19 '22 16:09

James Donnelly