Can't convert unix epoch / timestamp to proper time - javascript

I am getting timestamps for estimated bus arrival times from an API as a timestamp / epoch: 1536589019000. If I go to a website like this I get the appropriate format:
Monday, September 10, 2018 7:16:59 AM
But if I attempt to convert the date in javascript, for example, with momentjs, I get some date far into the future: 50662-08-08 08:03
moment.unix(estimatedArrivalTime).format("YYYY-MM-DD HH:mm")
How do I convert a unix timestamp properly?

divide the time by thousand , moment.unix() expects time to be in seconds
moment.unix(1536589019).format("YYYY-MM-DD HH:mm")

You're getting a timestamp in millesconds instead of seconds. Just passing it like so should work: moment(1536589019000).format("YYYY-MM-DD HH:mm")

Related

Convert Unix Date and Time to Local Time with JavaScript (React)

I'm trying to convert unix timestamp to local date in a react project. Below is the function that I'm using:
function convertDate(unixDate) {
const d = new Date(unixDate * 1000);
const day = d.toLocaleString(d.getDate())
return(day);
}
The date I'm getting is not acurate. For example is we run convertDate(1657745369979.82) we should get the date '07/13/2022 8:49:29 PM'. However the date that I'm actually getting is '11/10/54501, 8:59:39 AM'
Why are you multiplying by 1000 ?
This works just fine
new Date(1657745369979.82).toLocaleString() => '14/07/2022, 02:19:29'
which is the same as
Convert epoch to human-readable date and vice versa
1657745369979.82
Timestamp to Human date [batch convert]
Supports Unix timestamps in seconds, milliseconds, microseconds and nanoseconds.
Assuming that this timestamp is in milliseconds:
GMT: Wednesday, July 13, 2022 8:49:29.979 PM
Your time zone: Thursday, July 14, 2022 2:19:29.979 AM GMT+05:30
Relative: A day ago
from https://www.epochconverter.com/
If there's more that you want, this old thread should also help
Convert a Unix timestamp to time in JavaScript
It looks like you do not need to multiply the timestamp by 1000.
Unix timestamps are often of this length: 1657831769, which would need to be multiplied by 1000 (https://www.epochconverter.com/ is useful for testing conversions).
const unixDate = 1657745369979.82;
const d = new Date(unixDate);
console.log(d.toISOString());
Output: 2022-07-13T20:49:29.979Z

Convert N hours UTC format to Custom time zone using Moment JS

I have saved attribute in UTC time as N hours (example time: 15), and now i want to convert it back to custom time zone.
I want to use moment js and convert this 15 H from UTC time zone to custom time zone (example: Europe/Berlin)
First i convert from Europe/Berlin to UTC:
hours = 15
moment.tz(hours, "HH", "Europe/Berlin").utc().format("HH")
This is saved as 13 in database.
Now i want to convert back 13 hours from UTC to Europe/Berlin.
Custom time zone(Europe/Berlin) is dynamic.
Any help?
You can parse the hours again as UTC, and then set the timezone explicitly to Europe/Berlin before formatting:
console.log(moment.tz('13', "HH", 'GMT').tz('Europe/Berlin').format("HH"));
<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.17/moment-timezone-with-data.min.js"></script>

Which date format is this? 1370131200000

1370131200000
I was using hichart to plot timeline data. in the example the date field in x axis is like this. But my timestamp values are like 1502582400.
It's UNIX time stamp (Wikipedia)
1502582400 is 08/13/2017 # 12:00am (UTC)
You can find how to convert to other formats here.
JavaScript dates are numbers which specify the number of milliseconds since January 1, 1970, 00:00:00.

moment.js does not convert timestamps with specific timezones to unix timestamps

I am using moment.js to convert a bunch of timestamps in it's specific timezone to a unix timestamp like this:
var timestamp = "2015-12-29T09:35:00.000-08:00";
console.log(moment("2015-12-29T09:35:00.000-08:00").unix();
console.log(moment("2015-12-29T09:35:00.000-08:00").tz("America/Los_Angeles").unix();
The console log of both the above statements is for some reason, the same - 1451361900. This unix timestamp which it is logging is in my local timezone and not the one I asked for: "America/Los_Angeles". What am I missing?
A unix timestamp, or Posix, should always be in the UTC (Coordinated Universal Time) format.
Moment is just doing something like
function unix () {
return Math.floor(+this / 1000);
}
Where it converts the date object to an integer and then converts from milliseconds to seconds.
The starting point is a regular javascript Date object, and the ECMA standard says
Date objects are based on a time value that is the number of
milliseconds since 1 January, 1970 UTC.
so date objects are always UTC when converted to the number of milliseconds since 1. January 1970 (epoch), i.e. you can't set another timezone on a Unix timestamp, both your dates are the same.
The proper way is to use moment-Timezone is this.
console.log(moment("2015-12-29T09:35:00").unix());
console.log(moment.tz("2015-12-29T09:35" , "America/Los_Angeles").unix());
In above your are providing time zone as a string too which is this last part ".000-08:00" and then you are providing another zone, which is incorrect.
As you are trying to find out the unix timestamp for the date "2015-12-29T09:35:00.000-08:00". In this date format timezone value is already present which is "-08:00", hence you get the same unix timestamp.
For getting the unix timestamp desired solution, remove the timezone value and use moment-timezone as :
console.log(moment.tz("2013-12-01", "America/Los_Angeles").unix());
For more details check moment-timezone

How to convert Unix Timestamp to date with GMT (IST) format using moment in AngularJs

test:{"A":"\/Date(1442773800000+0530)\/","B":"423"}
Hi i am getting big trouble,Above is my data (JSON format), here i am using momentjs for convert Unix time stamp to date and time without using + 530 code, Sometime date not showing correct format, its automatically convert different UTC/GMT (time Zone),
moment(test.A).format('DD-MM-YYYY HH:mm')
how to use including the GMT(+530) in moment script
utcOffset() is the preferred method as of Moment 2.9.0. This function uses the real offset from UTC, not the reverse offset.
moment(1369266934311).utcOffset(+530).format('YYYY-MM-DD HH:mm')
And if you are using Moment version less then 2.9. You can go with this :
moment(1369266934311).zone(+530).format('YYYY-MM-DD HH:mm')

Categories

Resources