【问题标题】:Get device time/local time from UTC time in Titanium从 Titanium 中的 UTC 时间获取设备时间/本地时间
【发布时间】:2013-01-16 12:22:01
【问题描述】:

我正在开发 Titanium 应用程序。 我想在其中取当地时间。 我的问题是,当我使用时:

var currentDateTime = new Date();

显示UTC时间

有没有办法获取当前的本地时间或设备时间?

【问题讨论】:

    标签: ios datetime titanium titanium-mobile


    【解决方案1】:

    我使用getTimezoneOffset 函数解决了这个问题。

    我写了一个以UTC时间为参数返回本地时间的方法

    //Function Processes the passed UTC time and returns the current device time.
    function changeUTCToLocal(utcTime)
    {
        var offset = utcTime.getTimezoneOffset();
        var localTime = utcTime + offset;
        return localTime;
    }
    

    【讨论】:

      【解决方案2】:

      获取GMT时间的时间间隔以及当前本地时间与GMT的偏移量,将它们相加即可得到本地时间的实际时间间隔:

      假设 timeInterval 是从 GMT 开始的 NSInteger 毫秒时间, 使用以下获得偏移量

        NSInteger millisecondsFromGMT = 1000 * [[NSTimeZone localTimeZone] secondsFromGMT];
      

      现在将这两个相加得到实时间隔

      NSInteger realTimeInterval = timeInterval + millisecondsFromGMT ;
      

      【讨论】:

      • 我已经提到我正在开发基于 Titanium 的 iOS 应用程序。 :)
      【解决方案3】:

      我认为moment.js也可以解决这个问题

      【讨论】:

        猜你喜欢
        • 2018-07-02
        • 2012-10-08
        • 2014-09-15
        • 2011-11-24
        • 2013-03-23
        • 2012-09-23
        • 2014-03-03
        • 2014-10-18
        • 1970-01-01
        相关资源
        最近更新 更多