【发布时间】:2013-01-16 12:22:01
【问题描述】:
我正在开发 Titanium 应用程序。 我想在其中取当地时间。 我的问题是,当我使用时:
var currentDateTime = new Date();
显示UTC时间。
有没有办法获取当前的本地时间或设备时间?
【问题讨论】:
标签: ios datetime titanium titanium-mobile
我正在开发 Titanium 应用程序。 我想在其中取当地时间。 我的问题是,当我使用时:
var currentDateTime = new Date();
显示UTC时间。
有没有办法获取当前的本地时间或设备时间?
【问题讨论】:
标签: ios datetime titanium titanium-mobile
我使用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;
}
【讨论】:
获取GMT时间的时间间隔以及当前本地时间与GMT的偏移量,将它们相加即可得到本地时间的实际时间间隔:
假设 timeInterval 是从 GMT 开始的 NSInteger 毫秒时间, 使用以下获得偏移量
NSInteger millisecondsFromGMT = 1000 * [[NSTimeZone localTimeZone] secondsFromGMT];
现在将这两个相加得到实时间隔
NSInteger realTimeInterval = timeInterval + millisecondsFromGMT ;
【讨论】:
我认为moment.js也可以解决这个问题
【讨论】: