【问题标题】:Get time (hour, minute) from a non-local ISO8601 string with moment从非本地 ISO8601 字符串中获取时间(小时,分钟)
【发布时间】:2015-09-05 21:21:45
【问题描述】:

我想获取一个 ISO8601 字符串在指定时区的时间。

但是,每当我抓取 ISO8601 字符串的时刻对象时,它都会将所有内容转换为我机器的本地时间。

例如当我执行moment("2015-09-15T07:55+02:00").hours() 时,它会返回 1,因为它会将其转换为 2015-09-15T01:55:00-04:00"

如何让它在几个小时内返回 7?

【问题讨论】:

    标签: javascript ruby-on-rails datetime momentjs


    【解决方案1】:

    我认为这会有所帮助:

    http://momentjs.com/docs/#/parsing/special-formats/

    moment("2010-01-01T05:06:07", moment.ISO_8601);
    

    【讨论】:

      【解决方案2】:

      不确定时刻,但这是我的东部时区转换器,如果有帮助的话......

      getServerDate: function(dateValue) {
          var dateJan;
          var dateJul;
          var timezoneOffset;
      
          // Get dates for January and July
          dateJan = new Date(dateValue.getFullYear(), 0, 1);
          dateJul = new Date(dateValue.getFullYear(), 6, 1);
      
          // Get timezone offset
          timezoneOffset = Math.max(dateJan.getTimezoneOffset(), dateJul.getTimezoneOffset());
      
          // Check if daylight savings
          if (dateValue.getTimezoneOffset() < timezoneOffset) {
            // Adjust date by 4 hours
            dateValue = new Date(dateValue.getTime() + ((1 * 60 * 60 * 1000) * 4));
          } else {
            // Adjust date by 5 hours
            dateValue = new Date(dateValue.getTime() + ((1 * 60 * 60 * 1000) * 5));
          }
      
          return dateValue;
      },
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 2018-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多