What is the difference between getTime() vs Date.now() [duplicate] - javascript

This question already has answers here:
Date.getTime() v.s. Date.now()
(3 answers)
What is the difference between Date.now and Date.getTime in Javascript and how timezone affects them?
(2 answers)
Closed 2 years ago.
I did not realize the difference between getTime() vs Date.now() in Javascript, both gives the same output ? Is there any difference ?

No there should be not difference between the OUTPUT of
new Date().getTime() (without a date string)
and Date.now()
Date.now() The static Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC. (Unix Epoch)
Date.getTime()
The getTime() method returns the number of milliseconds since the Unix Epoch.
JavaScript uses milliseconds as the unit of measurement, whereas Unix Time is in seconds.
getTime() always uses UTC for time representation. For example, a client browser in one timezone, getTime() will be the same as a client browser in any other timezone.
console.log(new Date().getTime(),Date.now())
If you add a string or (yyyy,mm-1,dd,hh,min,ss,ms) to the call the new Date() then you will get the number of milliseconds since Epoch to the date you specify:
const d = new Date(2021,1,10,0,0,0,0)
console.log(d.getTime())
const diff = Math.abs(Date.now()-d.getTime()); // difference since 10th of Jan 2021
console.log(new Date(diff).toLocaleTimeString()); // diff WITH timezone offset
For a discussion vs object prototypes and static methods see Date.getTime() v.s. Date.now()

Related

Did new Date().getTime() returns utc milliseconds? [duplicate]

This question already has answers here:
Does new Date().getTime() in Browser Javascript always yield UTC?
(2 answers)
Closed last year.
new Date().getTime() // returns same utc milliseconds for all system time zones
So, is new Date().getTime() returns utc milliseconds?
There is no such things as "UTC milliseconds". A millisecond is an SI unit that is one thousandth of a second.
ECMAScript Date instances hold a time value that is an offset in milliseconds from 1970-01-01T00:00:00Z (the ECMAScript epoch). As a consequence, Dates are considered to be UTC, but that's just a concept based on the underlying time value.

Creating a JavaScript Date object from days since January 1st, 1970 [duplicate]

This question already has answers here:
Is the Javascript date object always one day off?
(29 answers)
Closed 2 years ago.
I have found many examples on the internet of converting from seconds or milliseconds to a JavaScript Date, but only a few for converting from days to a Date object. When I tried these examples in my own code (with my own values), I was unable to replicate their results.
My API returns an integer value representing days since UNIX epoch. I need to find a way to convert this to a JavaScript date object, so that I can display it in a human readable format.
For example:
new Date(18521 * 86400 * 1000)
// multiply by 86400 (seconds in a day), then by 1000 to convert to milliseconds.
At the time of writing, the date is 9/17/2020 MMDDYYYY, and 18522 days have passed since UNIX epoch. However, when I try to retrieve yesterday's UNIX date 18521 using the date constructor with some math (mentioned above), I get the incorrect date: Sep 15 2020. I would expect to get (Sep 16 2020) since I have only subtracted one day, but for some reason that is not the case.
Is there anything I am doing incorrectly here? What should I change to make my code work?
The way you're doing it is correct. There are always 8.64e7 ms in an ECMAScript UTC day and ECMASCript and Java have the same epoch. The way you're doing it sets the UTC date to the correct value (which is the right way to do it), not the local date. The default toString shows the local date so if the host is set to a negative offset, it will show one day earlier.
So get the UTC date instead:
new Date(18521 * 8.64e7).toISOString() //2020-09-16T00:00:00.000Z.
If you want to do this with a local date, then create a date for Jan 1970 and set the "date" parameter to the day count plus 1 (because January starts on 1 not 0):
console.log('UTC : ' + new Date(18521 * 8.64e7).toISOString() +
'\nLocal: ' + new Date(1970, 0, 18521 + 1).toDateString());

gave same time on converting Date.now() [duplicate]

This question already has answers here:
Convert a Unix timestamp to time in JavaScript
(34 answers)
Closed 2 years ago.
I'm looking for something to convert exact time
var arr = [1585287883,1585287876,1585287736,1585287730,1585287725,1585287720];
arr.forEach(val=>{
console.log(Date(val).toString())
})
There are 2 issues with what you posted:
Date -> new Date
The timestamps in javascript should represent milliseconds, currently they represent seconds. You can multiply the timestamps by 1000 when building the date object, to convert seconds to milliseconds.
var arr = [1585287883,1585287876,1585287736,1585287730,1585287725,1585287720];
arr.forEach(val=>{
console.log(new Date(val*1000).toString())
})
The fact is that you're using the wrong input format. Instead of seconds since 1970-01-01, you should use milliseconds.
A JavaScript date is fundamentally specified as the number of
milliseconds that have elapsed since midnight on January 1, 1970, UTC.
This date and time is the same as the UNIX epoch, which is the
predominant base value for computer-recorded date and time values.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Simply, multiply you're input by 1000 (and use the keyword new ;) )
var arr = [1585287883,1585287876,1585287736,1585287730,1585287725,1585287720];
arr.forEach(val=>{
console.log(new Date(val * 1000).toString());
})

Calculate new Date() as seconds since midnight, January 1, 1970 UTC [duplicate]

This question already has answers here:
How can I get seconds since epoch in Javascript?
(10 answers)
Closed 2 years ago.
I am attempting to use the Google Maps timezone API to get the datetime for a specific location (e.g. New York).
According to the API documentation:
Required Parameters:
timestamp specifies the desired time as seconds since midnight, January 1, 1970 UTC.
The issue I am having is calculating the difference in seconds from two Date() objects:
let currentDate = new Date(); // local user time
let epochDate = new Date('January 1, 1970'); // epoch date
// calculate seconds since (currentDate - epochDate)
Use the getTime() function on a Date instance:
let secondsSinceEpoch = new Date().getTime() / 1000;
No need to set Epoch (January 1, 1970), all dates are relative to that date.

Get a UTC timestamp [duplicate]

This question already has answers here:
How do I get a UTC Timestamp in JavaScript?
(16 answers)
Closed 10 years ago.
How can I get the current UTC timestamp in JavaScript? I want to do this so I can send timestamps from the client-side that are independent of their timezone.
new Date().getTime();
For more information, see #James McMahon's answer.
As wizzard pointed out, the correct method is,
new Date().getTime();
or under Javascript 1.5, just
Date.now();
From the documentation,
The value returned by the getTime method is the number of milliseconds
since 1 January 1970 00:00:00 UTC.
If you wanted to make a time stamp without milliseconds you can use,
Math.floor(Date.now() / 1000);
I wanted to make this an answer so the correct method is more visible.
You can compare ExpExc's and Narendra Yadala's results to the method above at http://jsfiddle.net/JamesFM/bxEJd/, and verify with http://www.unixtimestamp.com/ or by running date +%s on a Unix terminal.
You can use Date.UTC method to get the time stamp at the UTC timezone.
Usage:
var now = new Date;
var utc_timestamp = Date.UTC(now.getUTCFullYear(),now.getUTCMonth(), now.getUTCDate() ,
now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());
Live demo here http://jsfiddle.net/naryad/uU7FH/1/
"... that are independent of their timezone"
var timezone = d.getTimezoneOffset() // difference in minutes from GMT

Categories