• 第十九章:UTC time和local time的互换


    通常我们服务器端的时间使用UTC格式,避免服务器的时区对最终的时间产生影响。而客户端需要根据具体的时区显示local time,本文将介绍如何将服务器的UTC time(基于asp.net web api实现的web service)与客户端的local time(基于ionic实现的hybrid app)进行相互转换。

    1. Convert UTC time to local time

    应用场景:通常在请求结束之后,把服务器的数据转换成本地数据。

    在service中定义filter,需要传入连个参数:utc格式的时间字符串和转换格式。

    .filter('utcToLocal', function utcToLocal($filter) {
            return function (utcDateString, format) {
                if (!utcDateString) {
                    return;
                }
    
                if (utcDateString.indexOf('Z') === -1 && utcDateString.indexOf('+') === -1) {
                    utcDateString += 'Z';
                }
    
                return $filter('date')(utcDateString, format);
            };
        })
    

    在页面中使用filter:

    <p>{{item.triggeredTimeString|utcToLocal:"yyyy-MM-dd HH:mm:ss"}}</p>
    

    如何在directives中使用呢?需要重新定义utcToLocal函数,调用如下。注意:需要在根指令处声明$filter。

    var utcToLocal = function(utcDateString) {
                    if (!utcDateString) {
                        return;
                    }
    
                    if (utcDateString.indexOf('Z') === -1 && utcDateString.indexOf('+') === -1) {
                        utcDateString += 'Z';
                    }
    
                    return $filter('date')(utcDateString, "yyyy-MM-dd HH:mm:ss");
                }
    
    $filter('date')(utcDateString, "yyyy-MM-dd HH:mm:ss");
    

    2. Convert local time to UTC time

    应用场景:通常调用服务发送数据时使用。

    2.1 定义一个函数,使用UTC Date的构造函数来生成UTC时间。

    var toUTCDate = function(date){
                        var _utc = new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(),  date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
                        return _utc;
                    };
                    
                    var localTime = new Date();
                    var utcTime = toUTCDate(localTime);
                    $log.info(localTime);
                    $log.info(utcTime);

    2.2 直接使用库函数toUTCString:

    var localTime = new Date();
    var utc = localTime.toUTCString();
    

    2.3 使用Moment.js

    var localTime = new Date();
    moment.utc(localTime);

    2.4 扩展库函数,封装addDays和convertToUtcString:

    Date.prototype.convertToUtcString = function() {
        return (this.getUTCMonth()+1) + "-" + this.getUTCDate() + "-" + this.getUTCFullYear() + " " + this.getUTCHours() + ":" + this.getUTCMinutes() + ":" + this.getUTCSeconds();
    }
    
    Date.prototype.convertToLocalString = function() {
        return (this.getMonth()+1) + "-" + this.getDate() + "-" + this.getFullYear() + " " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();
    }
    
    Date.prototype.getYtdUtcString = function() {
        return "1-1-" + this.getFullYear() + " 12:00:00";
    }
    
    Date.prototype.addDays = function(num) {
        var value = this.valueOf();
        value += 86400000 * num;
        return new Date(value);
    }
    

    参考资料:

    http://stackoverflow.com/questions/31778624/converting-local-time-into-utc-time-in-angularjs

    https://github.com/urish/angular-moment 

    http://stackoverflow.com/questions/31785364/converting-date-from-local-time-zone-to-utc-and-then-to-local

  • 相关阅读:
    python模拟android屏幕高频点击工具
    android adb shell and monkey 学习记录
    appium+python环境搭建
    python监控接口请求
    JetBrains Pycharm 破解+汉化
    loadrunner调用jar包方法
    python抢小米6自动化脚本
    CDlinux制作U盘启动盘,打造自己的口袋系统
    无线渗透测试之wifi密码破解
    测试-test1
  • 原文地址:https://www.cnblogs.com/allanli/p/utc_time_local_time_conversion.html
Copyright © 2020-2023  润新知