[英]Ext JS: Display dates in UTC regardless of browser timezone
What is the best way of forcing dates to display in UTC (and ignore the browser local time) in an Ext JS grid? 强制日期在Ext JS网格中以UTC显示(并忽略浏览器本地时间)的最佳方法是什么?
My model receives dates in UTC: 我的模型以UTC接收日期:
"2014-06-24T00:00:00+00:00"
My grid has a datecolumn: 我的网格有一个日期列:
Ext.define('MyApp.view.MyGrid', {
store: MyApp.store.MyStore,
columns: [
{
xtype: 'datecolumn',
text: 'Date',
dataIndex: 'date',
format: 'Y-m-d H:i:sO'
},
]
});
The dates are displayed in browser local time, eg: 日期以浏览器本地时间显示,例如:
2014-06-24 01:00:00+0100
But I want to display them in UTC. 但我想以UTC显示它们。
So far, the best solution I have found is to import moment.js and use it thus: 到目前为止,我发现的最佳解决方案是导入moment.js并以此方式使用它:
{
xtype: 'datecolumn',
text: 'Date',
dataIndex: 'date',
renderer: function (value) {
return moment.utc(value).format('YYYY-MM-DD HH:mm:ssZZ');
}
}
Which has the desired result: 哪个具有预期的结果:
2014-06-24 00:00:00+0000
Surely there is a cleaner way? 当然有更清洁的方法了吗?
I don't see UTC support built-in, so you can for example extend datecolumn
and use moment
to render date. 我没有看到内置的UTC支持,因此您可以例如扩展datecolumn
并使用moment
来呈现日期。 Example: 例:
Ext.define('Ext.grid.column.UtcDate', {
extend: 'Ext.grid.column.Date',
alias: ['widget.utcdatecolumn'],
alternateClassName: 'Ext.grid.UtcDateColumn',
defaultRenderer: function(value){
return moment.utc(value).format(this.format);
}
});
Then just use utcdatecolumn
instead datecolumn
: 然后只需使用utcdatecolumn
代替datecolumn
:
Ext.define('MyApp.view.MyGrid', {
store: MyApp.store.MyStore,
columns: [
{
xtype: 'utcdatecolumn',
text: 'Date',
dataIndex: 'date',
format: 'YYYY-MM-DD HH:mm:ssZZ'
}
]
});
Its there in latest ExtJs. 它在最新的ExtJs中。 Make use of toUTCstring()
or other toUTC xxxx() methods available. 利用toUTCstring()
或其他可用的toUTC xxxx()方法。
alternatively, can do below: 或者,可以执行以下操作:
// convert to msec
// add local time zone offset
// get UTC time in msec
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.