用JS将指定时间转化成用户当地时区的时间

公司的项目是面向海外用户的,但是最初的设计没考虑到时差问题,存入数据库的时间都是东八区的时间,导致现在补救有点坑爹......

有一个需求是,产品详细页需要注明此款产品的开售时间,当海外的用户来访问这个页面时,不应该显示的是东八区的时间,而应该显示当地时区对应的时间。

.net的类库里没有获取客户端当地时区的方法的,比如ToLocalTime(),获取的是服务器的时间,TimeZoneInfo里的一些方法,都是正向转化,必须提供已知的时区编号,时区名称等条件,但这些不是已知时,都是白搭。

查询了资料,发现只能从JS入手,看到有两种解决方案,一种是用ajax传到服务端进行处理,第二种是设置cookie在服务端获取再处理,如果不是大批量的数据呈现,感觉有点麻烦,之后自己写了一个方法,具体方案就是从后台读取的时间,先从前台的JS里转化,再呈现到页面上,具体看业务需求。

直接提供JS方法吧,如有什么不对的地方可以提出来:

/*
* 根据日期写入时所在的时区号,传化为客户端所在时区的时间
*  东:负数,西:正数
*  Create Date:2016-01-27 By Harry  
*/
Date.prototype.ToLocalTimeByZoneNum = function (zoneNum) {
    if (isNaN(zoneNum))
        zoneNum = 0;
    this.setHours(this.getHours() + zoneNum);//转化时间为UTC时间
    var clientTime = new Date();//客户端当前时间
    var offset = Math.floor(clientTime.getTimezoneOffset() * 60000)//客户端时间与UTC时间的偏移量(毫秒)
    this.setTime(this.getTime() - offset);//根据偏移量计算传入时间在客户端所在时区的对应时间
    return this;
}
//默认传入东八区
Date.prototype.ToLocalTime = function () {
    this.ToLocalTimeByZoneNum(-8)
    return this;
}
/*
*  转化成国外常用显示格式
*  1:Mon Feb 01 2016 12:00:00
*  2:Mon Feb 01 2016
*  3:Feb 01 2016
*  4:Feb 01,2016
*  Create Date:2016-01-28 By Harry 
*/
Date.prototype.ToGlobalTime = function (type) {
    var _date = this.ToLocalTime().toString();
    switch (type) {
        case 1:
            return _date.substring(0, 25); //_date.indexOf('G')
            break;
        case 2:
            return _date.substring(0, 16)
            break;
        case 3:
            return _date.substring(3, 16)
            break;
        case 4:
            return _date.substring(3, 16).replace(/(.{7})/, "$1
,");
            break;
        default:
            return this;
            break;
    }
}

附加两个拓展方法,网上搜来的
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // Example: (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), // "H+": this.getHours(), //小时 "m+": this.getMinutes(), // "s+": this.getSeconds(), // "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } /** * 更改日期 * y年, m月, d日, h小时, n分钟,s秒 * Example: new Date().Add("d",-1) */ Date.prototype.Add = function (part, value) { value *= 1; if (isNaN(value)) { value = 0; } switch (part) { case "y": this.setFullYear(this.getFullYear() + value); break; case "m": this.setMonth(this.getMonth() + value); break; case "d": this.setDate(this.getDate() + value); break; case "h": this.setHours(this.getHours() + value); break; case "n": this.setMinutes(this.getMinutes() + value); break; case "s": this.setSeconds(this.getSeconds() + value); break; default: } }
作者:Harry

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.
原文地址:https://www.cnblogs.com/simendancer/p/5165023.html