James Radford
James Radford

Reputation: 1855

timezone issue with javascript date

got a timezone issue with a javascript date which displays the day before when changing the timezone from GMT to EST. I've imported momentjs to help with this although my attempt is failing with the following code. any suggestions?

original code is

thisDate = new Date(myVar);  // original

New attempt to correct the timezone difference.

thisDate = moment(thisDate).utc().format();

Many thanks,

UPDATE

var myDate = new Date(myVar);
var displayDate = moment(myDate).zone('+0100').format('YYYY-MM-DD HH:mm');

Upvotes: 0

Views: 1500

Answers (1)

Padmanathan J
Padmanathan J

Reputation: 4620

MomentJS is parsing the date as a locale date-time. If no hour is given, it is assuming midnight.

Then, you convert it to UTC, so it is shifted, according to your local time, forward or backwards. If your are in UTC+N, then you will get the previous date.

Check this formats

moment(new Date('07-18-2013')).utc().format("YYYY-MM-DD HH:mm").toString()
"2013-07-17 21:00"

moment(new Date('07-18-2013 12:00')).utc().format("YYYY-MM-DD HH:mm").toString()
"2013-07-18 09:00"

Date()
"Thu Jul 25 2013 14:28:45 GMT+0300 (Jerusalem Daylight Time)"


moment(new Date('07-18-2013 UTC')).utc().format("YYYY-MM-DD HH:mm").toString()
"2013-07-18 00:00"


// always "2013-05-23 00:55"
moment(1369266934311).zone('+0100').format('YYYY-MM-DD HH:mm')

Refer to this documentation.

Update :

moment().format("dddd, MMMM Do YYYY, h:mm:ss a");

// "Sunday, February 14th 2010, 3:25:50 pm"

Upvotes: 1

Related Questions