Moment.js unix timestamp is not matched with epoch timestamp - javascript

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.

Related

How to get unix timestamp in nanoseconds with 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

Convert timestamp from timezone to UTC timestamp?

I am receiving a timestamp from a third-party API, that looks like: 1540388730994. However, I have been informed that this timestamp is in 'Europe/Amsterdam' timezone.
I need to convert this timestamp to UTC, as we store all our dates in UTC.
How is this possible in JavaScript?
So far I have tried:
const timestamp = 1540388730994
const timestampInUTC = moment.tz(timestamp, 'Europe/Amsterdam').utc().valueOf()
console.log(timestamp, timestampInUTC)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.21/moment-timezone-with-data-2012-2022.min.js"></script>
However, you can clearly see that the two output timestamps are identical, whereas I would have expected the conversion to subtract 1-2 hours, as Amsterdam timezone is GMT+2.
What am I doing wrong here?
Timestamps in numeric form are always in UTC. If they've for some reason manually added/subtracted it by a time zone offset, they are doing it wrong. You don't see any change from moment, because a UTC timestamp is the same moment in time regardless of what time zone you represent the local time equivalent in. If you expected the timestamp to change, that would be representing an entirely different moment in time.

How to compare dates with moment

I'm trying to compare dates from database (firebase) with NOW. I'm using moment.js and it's not working. I think it has something to do with the time zone (or UTC+01:00)...
Example.
date1: "2016-11-20T14:00:00"
NOW: "2016-11-20T14:49:20+01:00"
I get NOW with moment() and compare like this:
var date1 = moment("2016-11-20T14:00:00");
moment(date1).isSameOrAfter( moment() ) // returns true
The comparison is precisely one hour off... How can I fix this? AND: is there any best practice in storing and comparing dates across time zones.
Dates should always be saved in UTC. If you use string type the UTC date time value in ISO format.
So while saving convert from client time zone into UTC and while retrieving convert from UTC into client time zone.
Comparison should be done between two dates in the same time zone.
If the database value is in UTC and then you have to parse as .utc() while creating a moment object.
moment.utc("2016-11-20T14:00:00");
Change the NOW value to UTC before comparing.
moment.(date1).utc()
Alternatively you can change the database value from UTC to local.
moment.utc("2016-11-20T14:00:00").local()
Keep the NOW value as is before comparing.
You cannot compare the date you stored in Firebase with now() as it lacks the information about your timezone.
You can store dates using Firebase.ServerValue.TIMESTAMP
For Example:
date1: Firebase.ServerValue.TIMESTAMP
This will save your time in Unix Milliseconds (time since the Unix epoch, in milliseconds) by the Firebase Database servers.
Then you can compare with new Date().getTime() this will give your device time in Unix Milliseconds.
For Example:
if(new Date().getTime() >= date1) {
//do something
}
Hope it helps :)

Given a date/time and timezone, how do you convert that into a UTC timestamp?

I am calling an API that returns a date/time string such as "2014-04-30 15:32:01". On top of this, I have a known timezone that this date/time exists in. I can see from the javascript Date() class has a .UTC() call for this, but that does not seem to accept a timezone as far as I can tell.
Given the date/time string + timezone, how can I convert those into a UTC timestamp?
I'd recommend using Moment.js and Moment Timezone.
You can create a timestamp using Date.parse from your local time (RFC2822 date format to include the timezone), create a date from that and use Date.toUTCString to get the UTC time. Not sure if it'll work with day light savings though.
Example: http://jsfiddle.net/3vXV6/ (will alert the date)
References:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString

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