[英]Moment.js & Moment Timezone with data-attribute timezones
I've got a server-generated list of locations in country/city
format, ie 我有一个服务器生成的country/city
格式的位置列表,即
<span class="location" data-timeZoneID="China/Beijing">Beijing</span>
<span class="location" data-timeZoneID="England/London">London</span>
I'd like to use JS/Jquery and Moment Timezone do this: 我想使用JS / Jquery和Moment Timezone来做到这一点:
data-timezone-
attribute from each item 从每个项目中提取data-timezone-
属性 moment.tz()
, 将其提供给moment.tz()
的实例, EDIT : I'm successfully pulling the data-attribute values with jQuery, but when I try to pass them to moment().tz()
; 编辑 :我已经成功地用jQuery拉数据属性值,但是当我尝试将它们传递给moment().tz()
; the time is not converted and my local client time is displayed instead. 时间不会转换,而是显示我的本地客户时间。 When I manually enter an IANA time zone ID, the conversion happens (although it's still inaccurate). 当我手动输入IANA时区ID时,会发生转换(尽管仍然不准确)。 Any thoughts on why the value supplied from the data-attribute
wouldn't be working? 有什么想法为什么不能从data-attribute
提供的值起作用?
This (sortof) works: 这(排序)的工作原理:
$(function(){
$('.location').each(function() {
var timeZone = $(this).data('timezoneid');
var now = moment().tz('Africa/Tripoli').format("MM/DD/YYYY hh:mm");
$(this).append( now );
});
});
This doesn't (local time is displayed): 不会(显示本地时间):
HTML 的HTML
<span class="location" data-timeZoneID="Africa/Tripoli">Tripoli: </span><br />
<span class="location" data-timeZoneID="England/London">London: </span>
JS JS
$(function(){
$('.location').each(function() {
var timeZone = $(this).data('timezoneid');
var now = moment().tz("'" + timeZone + "'").format("MM/DD/YYYY hh:mm");
$(this).append( now );
});
});
jsFiddle jsFiddle
It was a little hard to understand what your problem it but considering your "step by step"-list I would have done the following: 很难理解您的问题所在,但考虑到“逐步”列表,我将执行以下操作:
$(function(){
$('.location').each(function() {
var timeZone = $(this).data('timeZoneID');
var now = moment().tz(timeZone).format('HH:MM');
$(this).append( now );
});
});
Here's a JSFiddle that works 这是一个有效的JSFiddle
http://jsfiddle.net/93pEd/1/ http://jsfiddle.net/93pEd/1/
Notice the 2 changes that I made: 注意我所做的2个更改:
- London is Europe/London instead of England/London
- the format is HH:mm instead of HH:MM
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.