zoukankan      html  css  js  c++  java
  • Json 的日期格式转化(时区标准化)

    参考: 1、http://www.techmango.com/Blog/article/AjaxTech/Javascript_Date_Time_UTC_Convert.htm

        2、 http://www.cnblogs.com/dzone/archive/2011/03/31/2001616.html

        3、http://blog.csdn.net/hellotianma/archive/2010/07/12/5728723.aspx

          4、http://msdn.microsoft.com/zh-cn/library/bb412170.aspx  独立 JSON 序列化

    Microsoft dot .NET WebService输出json格式Date日期类型的格式非常特别,例如:/Date(1242357713797+0800)/, 它的含义是1970-1-1至今总的毫秒数,尾部的+0800指的是时区,这块内容是服务器端加上的(服务器将其转化为服务器当地时间了).如果我们在客户端正确显示原始存储的数值,就必须对时间进行各种计算才能将当地时间转换为目的时间。本文将解释如何进行这些计算。

    第一步:

    事情的第一步是获得当地时间。在JavaScript中,这无疑可以通过初始化一个Data()对象来轻松完成。

    // create Date object for current location

    d = new Date();

    通过调用Data()对象的getTime()方法,即可显示1970年1月1日后到此当时时间之间的毫秒数。

    // convert to msec since Jan 1 1970

    localTime = d.getTime();

    第二步:

    下一步,通过Data()对象的getTimezoneOffset()方法来找出当地时间偏移值。在缺省情况下,此方法以分钟显示时区偏移值结 果,因此在早先的计算中要将此值转换成毫秒。

    // obtain local UTC offset and convert to msec

    localOffset = d.getTimezoneOffset() * 60000;

    注意,getTimezoneOffset()方法的负返回值表示当地时间在全球标准时间(UTC)之前,而正返回值则表示当地时间在全球标准时间 (UTC)之后。

    注意:万一你想知道我是如何得到60000这个倍增因数的,记住1000毫秒等于一秒,而一分钟等于60秒。因此 ,将分钟转换成毫秒,要用60乘以1000等于60000。

    第三步

    将本地时间与本地时区偏移值相加得到当前国际标准时间(UTC)。

    // obtain UTC time in msec

    utc = localTime + localOffset;

    这里,变量utc包含当前国际标准时间(UTC)。但是,此时间以1970年1月1日到现在所含有的毫秒数来表示。暂时让它这样表示,因为还要进行 一些计算。

    第四步

    得到国际标准时间(UTC)后,再获得目标城市的国际标准时间(UTC)小时偏移值,把它转换成毫秒,再加上国际标准时间(UTC)。

    // obtain and add destination's UTC time offset

    // for example, Bombay

    // which is UTC + 5.5 hours

    offset = 5.5;

    bombay = utc + (3600000*offset);

    注意:万一你想知道我是如何得到3600000这个倍增因数的,记住1000毫秒等于一秒,而一小时等于3600秒。因此 ,将小时转换成毫秒,要用3600乘以1000等于3600000。

    此时,变量bombay包含印度孟买城的当地时间。此当地时间以1970年1月1日到现在所含有的毫秒数来表示。显然,这不是很合理,因此我们还要 进行一些计算。

    第五步

    通过初始化一个新的Data()对象,并调用此对象的toLocalString()方法,我们将前一步中计算得到的时间值转换成一个大家可以看得 懂的日期/时间字符串。

    // convert msec value to date string

    nd = new Date(bombay);

    document.writeln("Bombay time is " + nd.toLocaleString() + "<br>");

    这样转换就完成了!

    总结

    理解上面的步骤后,我们再看一看这段脚本(列表A),它建立一个紧凑,自定义的函数calcTime()来执行所有的计算并返回一个时间值。

    列表A

    <html>

    <head>

    <script language="JavaScript">

    // function to calculate local time

    // in a different city

    // given the city's UTC offset

    function calcTime(city, offset) {

    // create Date object for current location

    d = new Date();

     

    // convert to msec

    // add local time zone offset

    // get UTC time in msec

    utc = d.getTime() + (d.getTimezoneOffset() * 60000);

     

    // create new Date object for different city

    // using supplied offset

    nd = new Date(utc + (3600000*offset));

     

    // return time as a string

    return "The local time in " + city + " is " + nd.toLocaleString();

    }

    // get Bombay time

    alert(calcTime('Bombay', '+5.5'));

    // get Singapore time

    alert(calcTime('Singapore', '+8'));

    // get London time

    alert(calcTime('London', '+1'));

    </script>

    </head>

    <body>

    </body>

    </html>

    /** 
     * 获得当前时间的UTC时间戳 
     * @return {int} unix timestamp 
     */ 

    Date.prototype.getTimeUTC = function() {  
        return this.getTime() + this.getTimezoneOffset() * 60 * 1000;  
    }  
    /** 
     * 将当前操作的时间变更时区(主要用于转换一个其他时区的时间) 
     *  
     * @param {int} tzo 原时区 -12~13 
     * @param {int} tzo 目标时区 -12~13 默认为当前时区 
     */ 

    Date.prototype.changeTimezone = function(tzo,tzn) {  
        tzo = tzo * 60;  
        tzn = tzn ? tzn * 60 : -this.getTimezoneOffset();  
        this.setTime(this.getTime() - (tzo - tzn) * 60 * 1000);  

     /**
      * 获得当前时间的UTC时间戳
      * @return {int} unix timestamp
      */

     Date.prototype.getTimeUTC = function() {
      return this.getTime() + this.getTimezoneOffset() * 60 * 1000;
     }
     /**
      * 将当前操作的时间变更时区(主要用于转换一个其他时区的时间)
      *
      * @param {int} tzo 原时区 -12~13
      * @param {int} tzo 目标时区 -12~13 默认为当前时区
      */

     Date.prototype.changeTimezone = function(tzo,tzn) {
      tzo = tzo * 60;
      tzn = tzn ? tzn * 60 : -this.getTimezoneOffset();
      this.setTime(this.getTime() - (tzo - tzn) * 60 * 1000);
     }
    至此,就可以基于这个扩展过的Js Date对象进行新的开发。

    思路很简单,就是把任意格式任意时区的时间先通过changeTimezone方法转到和当前系统同一时区下,然后再对getTimeUTC生成的 UTC Unix时间戳进行操作。

    比如我要计算 +8 时区下 4 Jun 2008, 16:30时刻 与 +9 时区下的当前时刻相差的时间

    //自动解析成unix时间戳  
    var p = Date.parse('4 Jun 2008, 16:30');  
     
    var time_parse = new Date(p);  
     
    //转换到要对比的时区  
    time_parse.changeTimezone(8,9);  
     
    var time_now = new Date();  
     
    //都转换为UTC进行对比  
    var der = time_now.getTimeUTC() - time_parse.getTimeUTC();  
     
    alert(' 相差' + parseInt(der / 1000 / 60) + '分钟'); 
     //自动解析成unix时间戳
     var p = Date.parse('4 Jun 2008, 16:30');

     var time_parse = new Date(p);

     //转换到要对比的时区
     time_parse.changeTimezone(8,9);

     var time_now = new Date();

     //都转换为UTC进行对比
     var der = time_now.getTimeUTC() - time_parse.getTimeUTC();

     alert('相差' + parseInt(der / 1000 / 60) + '分钟');
    当然有更简单的编码,但在复杂运用中 理清思路更加不容易出错。

    如果想要实现本Blog左侧栏XX天XX月前这样更人性化的提示,这里可以根据自己的需要做进一步扩展。实现的函数如下

    /** 
     * 表示指定时间与现在的差值 
     *  
     * @param {int} t 所要比较的时间 unix timestamp (UTC) 
     * @param {int} n 作为标准的时间,默认为现在时刻 unix timestamp (UTC) 
     * @return {string} 对相差时间的表述 
     */ 

    Date.prototype.derTime = function(t,n) {  
        var n = n ? n : this.getTimeUTC();  
        function ms2min(ms) {  
            return parseInt(ms / 1000 / 60);  
        }  
        var der = ms2min(n - t);  
        var ba = der > 0 ? '前' : '后';  
        der = Math.abs(der);  
        var res = '';  
        if(der == 0) {  
            res = '刚刚';  
        }  
        else if(0 < der && der < 60) {  
            res = der + '分钟' + ba;  
        }  
        else if(60 <= der && der < 24 * 60) {  
            var min = der % 60 == 0 ? '' : String(der % 60) + '分钟';  
            res = String(parseInt(der / 60)) + '小时' + min + ba;  
        }  
        else if(der >= 24 * 60 && der < 24 * 60 * 31) {  
            res = String(parseInt(der / 60 / 24)) + '天' + ba;  
        }  
        else if(der >= 24 * 60 * 31 && der < 24* 60 * 365) {  
            res = String(parseInt(der / 60 / 24 / 31)) + '个月' + ba;  
        }  
        else if(der > 24 * 60 * 365) {  
            res = String(parseInt(der / 60 / 24 / 365)) + '年' + ba;  
        }  
        return res;  
    }  
    /** 
     * 解析一个时间字段与当前时间的差 
     * @param {string} i 
     * @param {int} 时区 -12~13 
     */ 

    function time_der(i,tz) {  
        var p = Date.parse(i);  
        if(!p) {  
            return i;  
        }  
        var time_parse = new Date(p);  
        if(tz != undefined) {  
            time_parse.changeTimezone(tz);  
        }  
        var time_now = new Date();  
        return time_now.derTime(time_parse.getTimeUTC());

    为了尽可能简便的改善Js的Date处理能力,这里只对Js的 Date对象做了两个扩展

  • 相关阅读:
    VS自带的诊断工具
    Electron学习
    PC跨*台
    .NET调试学习
    Mac使用
    SSL/TLS
    UKey学习
    授权机制OAuth、JWT
    代理服务器
    .NET相关源码查找
  • 原文地址:https://www.cnblogs.com/cuiwenke/p/2031723.html
Copyright © 2011-2022 走看看