如果我有标签:
<span class="utctime">2010-01-01 11:30 PM</span>
我想要一个jquery脚本或插件将每个utctime
类转换为当前用户的浏览器本地时间。在写一篇文章之前我更愿意找到它。
答案 0 :(得分:17)
好的,所以我创建了一个这样做:
/*
Note: this requires that the JQuery-DateFormat plugin (available here) be loaded first
http://plugins.jquery.com/project/jquery-dateFormat
*/
(function ($) {
$.fn.localTimeFromUTC = function (format) {
return this.each(function () {
// get time offset from browser
var currentDate = new Date();
var offset = -(currentDate.getTimezoneOffset() / 60);
// get provided date
var tagText = $(this).html();
var givenDate = new Date(tagText);
// apply offset
var hours = givenDate.getHours();
hours += offset;
givenDate.setHours(hours);
// format the date
var localDateString = $.format.date(givenDate, format);
$(this).html(localDateString);
});
};
})(jQuery);
用法:
<span class="utcdate">2/5/2010 10:30 PM</span>
$('.utcdate').localTimeFromUTC('MM/dd/yyyy hh:mm a');
答案 1 :(得分:7)
使用输入日期查找时区偏移量。对DST更改很重要。
(function ($) {
$.fn.localTimeFromUTC = function (format) {
return this.each(function () {
// get provided date
var tagText = $(this).html();
var givenDate = new Date(tagText);
if(givenDate == 'NaN') return;
// get time offset from browser
var offset = -(givenDate.getTimezoneOffset() / 60);
// apply offset
var hours = givenDate.getHours();
hours += offset;
givenDate.setHours(hours);
// format the date
var localDateString = $.format.date(givenDate, format);
$(this).html(localDateString);
});
};
})(jQuery);
像......一样使用它。
function ConvertDatesToLocalTime() {
$('.ConvertUtcToLocal').localTimeFromUTC('MM/dd/yyyy hh:mm:ss a');
}
$(document).ready(function () {
ConvertDatesToLocalTime();
});
将“ConvertUtcToLocal”类分配给需要转换的所有元素。
答案 2 :(得分:1)
$(".localdatetime").each(function () {
var datestr = $(this).text();
//alert(datestr);
if (datestr.trim() != '') {
var dateOb = (new Date(Date.parse(datestr, 'MM-dd-yyyy HH:mm'))).setTimezone("GMT").toString('dd MMM yyyy hh:mm tt');
//alert(dateOb);
$(this).text(dateOb);
}
})
这也可以与Date.js库一起使用,以在用户时区显示时间
答案 3 :(得分:0)
CodeGrue非常感谢与社区分享。
对于那些被迫使用其他时区而不是UTC的人...你可以通过添加这样的时差来改变这个功能:
原始摘录:
var offset = -(currentDate.getTimezoneOffset() / 60);
更改了片段以使用CEST时区(时区偏移:UTC + 2小时):
var offset = -(currentDate.getTimezoneOffset() / 60 + 2);
等等。
答案 4 :(得分:0)
当我使用它时,我不得不改变行
var hours = givenDate.getHours();
到
var hours = givenDate.getUTCHours();
通过此调试时,行var givenDate = new Date(tagText)
最终创建一个UTC格式的Date对象(如果您以RFC1123格式给它一个日期,例如ddd, dd MMM yyyy HH:mm:ss GMT
),但是当您调用getHours时你得到当地时区的时间。因此,除非你调用getUTCHours,否则它不起作用。
所以完整的事情是
/*
Note: this requires that the JQuery-DateFormat plugin be loaded first
http://plugins.jquery.com/project/jquery-dateFormat
*/
(function ($) {
$.fn.localTimeFromUTC = function (format) {
return this.each(function () {
// get time offset from browser
var currentDate = new Date();
var offset = -(currentDate.getTimezoneOffset() / 60);
// get provided date
var tagText = $(this).html();
var givenDate = new Date(tagText);
// apply offset
var hours = givenDate.getUTCHours();
hours += offset;
givenDate.setHours(hours);
// format the date
var localDateString = $.format.date(givenDate, format);
$(this).html(localDateString);
});
};
})(jQuery);
有关我如何将其与timeago插件结合使用,请参阅this other question。