I try to run this js
return function(milliSeconds, timeZoneId) {
if (milliSeconds == 0) {
return \"\";
}
var now = moment();
return now.
I'm working on similar topic and got answer to your original question. Hope will help other people if this is too late to you.
First of all, just like @VincenzoC explained: 1. Have to change the Unix timestamp to 10-digit for Moment.JS. 2. The format for year should be "YYYY" instead of "yyyy".
Per the explanation here enter link description here POSIX compatibility requires that the offsets are inverted. Therefore, Etc/GMT-X will have an offset of +X and Etc/GMT+X will have an offset of -X. This is a result of IANA's Time Zone Database and not an arbitrary choice by Moment.js.
You need to change set as: var timezoneId = "Etc/GMT+08"; Then run moment.unix(1524578400).tz("Etc/GMT+8").format('MMM d, YYYY H:mm:ss Z');
The return is: "Apr 2, 2018 6:00:00 -08:00" Please note what you expect is 5am but it's Day light saving time, so the return is 6am which is correct.
As moment.tz(..., String) docs states:
The moment.tz constructor takes all the same arguments as the
moment
constructor, but uses the last argument as a time zone identifier.
A time zone identifier is something like 'America/New_York'
or 'Europe/Rome'
, "GMT-08:00"
is not a valid input.
In your case you can use moment(Number) and then:
var milliSeconds = "1524578400000";
var timezoneId = "GMT-08:00";
var timeInt = parseInt(milliSeconds, 10)
var m1 = moment(timeInt).tz('America/Los_Angeles');
console.log(m1.format('MMM D, YYYY H:mm:ss'));
var offset = timezoneId.substring(3);
var m2 = moment(timeInt).utcOffset(offset);
console.log(m2.format('MMM D, YYYY H:mm:ss'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.14/moment-timezone-with-data-2012-2022.min.js"></script>
Please note that:
YYYY
for years and D
for day of the month (d
is day of the week)1524578400000
is 2018-04-24 14:00:00
UTC, I'm missing why you are expecting 5am
as output.