简体   繁体   English

moment()。format(…)在某些设备上返回本地时间,在其他设备上返回UTC

[英]moment().format(…) returns local time on some devices and UTC on other devices

I have a cordova app. 我有一个cordova应用。 To load data from a server, I send as parameters the local time and the Unix timestamp: 要从服务器加载数据,我将本地时间和Unix时间戳作为参数发送:

var timestamp = Math.floor(date.getTime() / 1000);
var localtime = moment().format("YYYY-MM-DDTHH:mm:ss");

Now I saw that on some devices, localtime is not "local time" but it looks like UTC. 现在,我看到在某些设备上,本地时间不是“本地时间”,但看起来像UTC。 From my logs: 从我的日志中:

2017-11-28 08:33:08 [...] URL/timestamp/1511879588/localtime/2017-11-28T14:33:08

The first date/time is "Chicago" (GMT-6), but the device was in New York (GMT-5) and on a screenshot I saw that it shows 9:33 as time (in the Android system), so this is correct. 第一个日期/时间是“芝加哥”(GMT-6),但设备在纽约(GMT-5),在屏幕截图中,我看到它显示的时间是9:33(在Android系统中),因此是正确的。

On my devices (Android and iOS), localtime is correct, ie, the same as the system time. 在我的设备(Android和iOS)上,本地时间是正确的,即与系统时间相同。

What could be wrong here, why is it sometimes "local time" and sometimes UTC? 这可能是什么问题,为什么有时是“本地时间”,有时又是UTC?

Please try this: 请尝试以下方法:

var timestamp = Math.floor(date.getTime() / 1000);
var localtime = moment.parseZone(new Date()).local().format("YYYY-MM-DDTHH:mm:ss");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM