简体   繁体   English

Moment.js和时区仅在Android Chrome上不起作用

[英]Moment.js and timezone doesn't work only on Android Chrome

I added moment.js to manage the timezone in my webapp. 我添加了moment.js来管理Webapp中的时区。 I setup my devices in New Zealand (GTM +13) and works fine on: 我在新西兰(GTM +13)设置了设备,并在以下情况下可以正常工作:

  • Iphone: Safari & Chrome iPhone:Safari和Chrome
  • Android: Firefox Android:Firefox
  • OSX: Safari & Chrome OSX:Safari和Chrome

Returns 1 more day (I'm in GTM +0). 再返回1天(我在GTM +0中)。 But it doesn't work on Android Chrome (I tested it with several Android devices) and I don't have any idea why. 但这在Android Chrome上不起作用(我已经在多个Android设备上对其进行了测试),我也不知道为什么。 My code: 我的代码:

  var dateFunctions = {

    "getCurrentDate": function(){
      return moment().valueOf();
    },

    //I need Date type to use in a custom Calendar component.
    "getFormatDate": function(dateLong){
      var d = moment(parseInt(dateLong)).toDate();
        },

   //For jquery calendar. input: Date without local time; output: timestamp
   "getDate": function(date){
      var currentDay = moment();
      var d = moment(date).set("hour",currentDay.get("hour")).set("minute",currentDay.get("minute")).set("second",currentDay.get("second")).set("millisecond",currentDay.get("millisecond"));
      return d.valueOf();

    },

        ...
        }

... ... ……

//it should return tomorrow (GTM +13) in Android Chrome
var currentDay = managedates.getFormatDate(managedates.getCurrentDate())

I think that maybe it's related with the google account associated with the Android device. 我认为这可能与与Android设备关联的Google帐户有关。 Any idea? 任何想法?

Edit: I edited it to add my updated code 编辑:我对其进行了编辑以添加更新的代码

A few things: 一些东西:

  • Your timezoneOffset variable name is actually a time zone identifier, not an offset. 您的timezoneOffset变量名称实际上是时区标识符,而不是偏移量。 Not a big deal, but could cause some confusion. 没什么大不了的,但是可能引起一些混乱。

  • You actually don't need moment-timezone here at all. 实际上,您根本不需要此处的时区。 Moment.js can work in the local time zone without help from moment-timezone. Moment.js可以在本地时区工作,而无需得到即时时区的帮助。

  • Time zones are irrelavent when you're working with numeric timestamps anyway. 无论如何,当您使用数字时间戳记时,时区是无可避免的。 Unix time is always UTC-based. Unix时间始终基于UTC。

  • No need to be using seconds-based functions with */ 1000. Moment can handle that for you. 无需在* / 1000上使用基于秒的函数。Moment可以为您处理。

Your code, corrected: 您的代码已更正:

"getCurrentDate": function(){
  return moment().valueOf();
},

"getFormatDate": function(dateLong){
  return moment(dateLong).toDate();
},

One step further - since you are only working with numeric timestamps and date objects, there's really no good reason to use Moment here at all. 更进一步-由于您仅使用数字时间戳和日期对象,因此根本没有充分的理由在这里使用Moment。

"getCurrentDate": function(){
  return Date.now();
},

"getFormatDate": function(dateLong){
  return new Date(dateLong);
},

If you were trying to work with local time, you'll need to be clearer about how you're sending data back and forth. 如果您尝试使用当地时间,则需要更清楚地了解如何来回发送数据。 You can't do that with timestamps and retain the local time. 您无法使用时间戳记来保留本地时间。 There is no information about local time or time zone in a Unix timestamp. 在Unix时间戳中没有有关本地时间或时区的信息。

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

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