是否有将 UTC 日期时间转换为本地用户时区的 JQuery 插件? [英] Is there a JQuery plugin to convert UTC datetimes to local user timezone?
本文介绍了是否有将 UTC 日期时间转换为本地用户时区的 JQuery 插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有标签:
<span class="utctime">2010-01-01 11:30 PM</span>
我想要一个 jquery 脚本或插件来将每个 utctime
类转换为当前用户的浏览器本地时间.我宁愿在写之前先找到它.
I would like a jquery script or plug in to convert every utctime
class to the current user's browser local time. I would prefer to find this before writing one.
推荐答案
好的,所以我创建了一个解决方案:
Ok, so I created one that does it:
/*
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');
这篇关于是否有将 UTC 日期时间转换为本地用户时区的 JQuery 插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文