2

I am using momentjs in alloy framework in Appcelerator. My api returns the date as - 2017-09-06T12:03:00.000Z I am using below code to format this date into readable form -

var dt =  moment(record.createddate);
$.dateValue.text = moment(dt).format('lll');

But the output I get is - Sep 6, 2017 5:33 PM, which is not correct as the date saved in db and returned from api is EST and the date getting displayed is GMT+0530. How should i format this date so that I get the correct date value?

Jeet
  • 5,569
  • 8
  • 43
  • 75
  • Shouldn't `moment('2017-09-06T12:03:00.000Z').format('lll')` return `Sep 6, 2017 2:03 PM` instead of `Sep 6, 2017 5:33 PM` ? – Ilshidur Sep 11 '17 at 08:15
  • Exactly it should and that is what I am expecting, but it returns `Sep 6, 2017 5:33 PM`. :( – Jeet Sep 11 '17 at 08:17

1 Answers1

2

I guess, somewhere in your code, the moment's default timezone is set to GMT+0530. Something like moment.tz.setDefault('Asia/Colombo') could do this.

You can define in what timezone you want to display your date. This should work for you :

moment('2017-09-06T12:03:00.000Z').tz("Etc/GMT").format('lll')

Or if you want the value I suggested in the comments :

moment('2017-09-06T12:03:00.000Z').tz("Etc/GMT-2").format('lll')

For more informations about moment.js timezones, you can check the moment.js timezone docs.

Hope this helps !

Ilshidur
  • 1,461
  • 14
  • 11
  • Yes that could have been an easier fix but the problem is alloy framework doesn't have MomentTimezone, but only Moment. I mean this part `.tz("Etc/GMT").format('lll')` is not possible in Alloy. – Jeet Sep 11 '17 at 08:49