How to get unix timestamp in nanoseconds with JavaScript? - javascript

I'm using moment and I have a datetime like 2020-11-04 21:01:54.434 in a moment object. I would like to obtain the unix timestamp in nanoseconds. I'm trying things like moment.valueOf() but they don't seem to be working up to nanosecond precision. Given that the moment object has information about the nanoseconds, is there anyway I can extract it as the number of seconds since unix epoch?

Math.round(moment.getTime() * 1000)
Because moment.getTime will return you time in milliseconds, so you have to convert it in nanoseconds

Related

Weird data format from firestore db

My data from Firestore looks this way:
Timestamp(seconds=1566840930, nanoseconds=491000000)
Since it's not a valid unix timestamp i have no idea on how to display it in a readable name, like dd.mm.yy.
I'm fetching data in a component:
#Component({
firestore() {
return {
linksArray: db.collection('links')
}
}
})
Then using v-for i'm displaying a list of links.
What you have there is a Firestore Timestamp object, which is defined as:
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.
So (like UNIX timestamps) it represents a point in time relative to the UTC Epoch time, but (unlike UNIX timestamps) it represents this time with nanosecond resolution.
If you want to get the millisecond resolution that UNIX timestamps are in, call toMillis() on the Timestamp.

Moment.js unix timestamp is not matched with epoch timestamp

I am using the unix() function of moment.js in node.js and it returns the timestamp and when i matched this timestamp with epoch timestamp (https://www.epochconverter.com/) then there is a difference of 18-20 sec.
moment().unix()
so anyone can help on this.
Please compare the result of moment('2017-10-23 14:31:03').unix() to the epoch converter for the same date and time; if they are equal, it means that your local machine (e.g., laptop) is not set to the right time.

How to convert 12 hours datetime to timestamp with javascript

I have a datetime in following format 28/11/2015 09:41 PM and I want to convert it to epoch timestamp. How can I do that with javascript?
Additionally: I want to add and substracts 7200 seconds from that timestamp and convert it back to the original format. How can I do that? Is it necessary to convert datetime to timestamp first?
There is an awesome library available for that!
http://momentjs.com/
A great place to find information on JavaScript's built-in objects is the MDN, which has this article (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) on the datetime (or really just Date) object.
The way to get an epoch timestamp from a Date object is to use the getTime() function. It returns the number of milliseconds since 1 January, 1970.
var dateNow = Date.now();
var epochNow = dateNow.getTime();
You can then just add the seconds to it:
epochNow += (7200 * 1000); // * 1000 because it's in milliseconds
And then convert it back:
dateNow.setTime(epochNow);
Good luck!
NOTE
Beware inconsistencies in JavaScript Date implementations, especially in earlier versions of Internet Explorer. As some have noted, a good library like moment.js (http://momentjs.com/ ) is very helpful to prevent problems. However, if you are only using fully modern browsers or node, you shouldn't have as many problems.

Converting specific date/time to timestamp with JavaScript

I am given a spesific task to obtain the current time in a given format, and then convert said time to a Unix timestamp, but I'm feeling a little in over my head here.
After some research and creative googling, I came up with:
this.$el.find("#settime").val(moment((new Date).getTime()).format("DD.MM.YYYY HH:mm"));
Here the:
(moment((new Date).getTime()).format("DD.MM.YYYY HH:mm"))
...part being the central one.
Now, once the user sees the date in this format, he/she needs to be able to edit it, and feed it back to my application.
At this point, I need it converted.
Assume that a user who's using my application wants 04.08.2015 12:00, but their local time reads 04.08.2015 13:00, so they lower it by an hour and throw it back to my application.
How would I go about converting the updated time back to a timestamp? Note, that the time will always come in at that format (DD.MM.YYYY HH:mm).
You can get the timestamp with the following code
Math.floor(new Date().getTime() / 1000)
if you want to convert a datetime string to timestamp, then pass that string to Date() and call the getTime() method on it and divide the result by 1000.
so if the user provides August 4th 2015 13:13 then
Math.floor(new Date("08.04.2015 13:13").getTime() / 1000)
will give the timestamp corresponding to the given date 1438674180,
You will have to convert the modified date into MM.DD.YYYY format for this to work.
Refer this link to know more about Dates

What is the timestamp reference after deducting from getTimezoneOffset()?

For example I have this:
d = new Date(2013,04,20,14,56,10)
Mon May 20 2013 14:56:10 GMT+0800 (SGT)
dt = d.getTime() /1000
1369032970
Now, timezoneOffset value is
d.getTimezoneOffset()*60
-28800
So if I reduce it, I get
dt -= d.getTimezoneOffset()*60
1369061770
My question is, is 1369032970 my local timestamp, and 1369061770 UTC timestamp?
Can I safely say that any current timestamp reduced by the timezoneOffset is the UTC timestamp?
The result from getTime is in milliseconds since 1/1/1970 UTC. The local time zone plays no part in it. So if your question was how to get the UTC timestamp, simply use the result from getTime without any modification.
The idea of a "local timestamp" isn't very useful. One might apply an offset to the UTC timestamp before rendering it to a human-readable date string - but in Javascript, that's already done for you behind the scenes. You really don't want to pass a numeric timestamp to anyone else unless it is strictly UTC, because the meaning of what is "local" would be lost.
Also, when you call getTimezoneOffset, you are getting back the specific offset at the moment represented by the date - in minutes. Also, the sign is the opposite of what we normally see for time zone offsets. For example, I live in Arizona where the offset is UTC-07:00 year-round. But a call to getTimezoneOffset returns a positive value of 420. If you were to apply it a timestamp, you would do the following:
dt -= dt.getTimezoneOffset() * 60 * 1000;
You almost had it, but forgot to convert from seconds to milliseconds. But like I said, this value is meaningless. If you created a new Date object from it, it would display with the offset applied twice - once by your own code, and again by the Javascript internals.

Categories