时间js格式日期与时区 [英] Moment js format date with timezone

查看:135
本文介绍了时间js格式日期与时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用Ember + moment.js格式化日期。
$ b

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



在我的ember帮助类中,我可以使用以下代码格式化日期:

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

我收到以下输出:



23/08/2016 9:43 am



预期输出: 23/08/2016 9:43 am GMT



如何在格式化函数中指定时区标志?



任何帮助都不胜感激。

解决方案

安装ember-moment - ember install ember-moment >
安装ember-cli-moment-shim - ember安装ember-cli-moment-shim

停止并启动 ember server



要启用时区,需要包含 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 :{
//选项:
//'all' - 所有年份,所有时区
//'2010-2020' - 2010-2020,all timezones
//'没有数据,只是时区API
includeTimezone:'all'
},
EmberENV:{
FEATURES:{
//这里可以启用实验功能在ember金丝雀上建立
// eg 'with-controller':true
}
},

APP:{
//这里可以将标志/选项传递给应用程序实例
//创建时
}
};
返回ENV;
};

然后您可以开始使用tz函数并获取缩写时区名称,您可以包括 z 标记格式。



moment()。tz('亚洲/加尔各答')格式('DD / MM / YYYY h:mm a z')


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.

解决方案

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')

这篇关于时间js格式日期与时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆