assembler
assembler

Reputation: 3302

conversion for unix timestamp to moment js not working properly in chrome

This code:

let obj = moment.unix(1459382400).utc();

Works fine in Firefox 54, but not in Chrome 59.0.3071.109. In firefox it returns Date 2016-03-31T00:00:00.000Z which is the correct date, but in Chrome it returns Wed Mar 30 2016 20:00:00 GMT-0400 (CDT) which is a day before the correct date.

I've been searching for a solution unsuccessfully. The 1459382400 unix timestamp has no time, but in chrome it does.

What am I missing.

Upvotes: 1

Views: 608

Answers (1)

Shubham
Shubham

Reputation: 1288

function setTimezone(moment, timezone) {

var a = moment.toArray(); // year,month,date,hours,minutes,seconds as an array

moment = moment.tz(timezone);

moment.year(a[0])
    .month(a[1])
    .date(a[2])
    .hours(a[3])
    .minutes(a[4])
    .seconds(a[5])
    .milliseconds(a[6]);

return moment; // for chaining
};

var m = setTimezone(moment('2014-03-10T10:00'), "America/New_York");
console.log(m.format())

Use this function

Before using, consider updating moment. latest version has this issue fixed

Upvotes: 2

Related Questions