Moment js format date with timezone

2019-07-26 06:47发布

问题:

Hi I am using Ember+moment.js to format date in my ember helpers.

I am getting the following date from the service

Tue Aug 23 2016 09:43:53 GMT+0200 (W. Europe Daylight Time)

In my ember helper class i am able to format date using following code :

var formattedDate = moment(date).format('DD/MM/YYYY h:mm a');

I am getting the following output :

23/08/2016 9:43 am

Expected Output : 23/08/2016 9:43 am GMT

How can i specify the timezone flag in the format function?

Any help should be appreciated.

回答1:

Install ember-moment - ember install ember-moment
Install ember-cli-moment-shim - ember install ember-cli-moment-shim
stop and start ember server

To enable moment timezone, need to include moment:{ includeTimezone: 'all' } in config\environment.js

/* jshint node: true */

module.exports = function(environment) {
  var ENV = {
    modulePrefix: 'App-Name',
    environment: environment,
    baseURL: '/',
    locationType: 'auto',
    moment: {
      // Options:
      // 'all' - all years, all timezones
      // '2010-2020' - 2010-2020, all timezones
      // 'none' - no data, just timezone API
      includeTimezone: 'all'
    },
    EmberENV: {
      FEATURES: {
        // Here you can enable experimental features on an ember canary build
        // e.g. 'with-controller': true
      }
    },

    APP: {
      // Here you can pass flags/options to your application instance
      // when it is created
    }
  };
  return ENV;
};

and then you can start use tz function and to get abbreviated time zone name you can include z flag in the format.

moment().tz('Asia/Calcutta').format('DD/MM/YYYY h:mm a z')