Javascript:如何从UTC字符串和偏移量解析日期? [英] Javascript: How to parse date from UTC string and offset?

查看:89
本文介绍了Javascript:如何从UTC字符串和偏移量解析日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情景

我有一个字符串格式的UTC日期和一个以分钟为单位的关联偏移号:

I have a UTC date in string format and an associated offset number in minutes:


  • 日期:2017-10-01T12:00:00.000Z

  • 抵消:360

用户的浏览器位于Mountain Standard Time(-7)时区。日期值是从中央标准时间(-6)时区记录的,并与其偏移量一起保存(因此偏移量为360分钟)。可以假设偏移量不包括夏令时。

The user's browser is in the Mountain Standard Time (-7) timezone. The date value was recorded from the Central Standard Time (-6) timezone, and was saved along with its offset (hence the offset of 360 minutes). It can be assumed the offset does not include for daylight savings time.

问题

如何使用记录时区的偏移量解析UTC日期?换句话说,尽管用户的浏览器是MST,我仍然希望将字符串中的日期显示为2017-10-01 6:00 AM。不需要显示时区。这是否可以使用时区时区而不具有时区名称并且只有不包含DST的偏移量?

How do I parse the UTC date using the offset for the timezone it was recorded in? In other words, despite the user's browser being MST, I still want to display the date in a string as something like "2017-10-01 6:00 AM". Displaying the timezone is not required. Is this possible using moment-timezone without having the name of the timezone and just having the offset that doesn't include DST?

推荐答案

那就是你需要的吗?

const moment = require('moment');
moment
  .utc('2013-06-20T07:00:00.427')
  .zone(-360)
  .format();

在这里你可以找到很多显示选项 - > http://momentjs.com/docs/#/displaying/

here you'll find a lot of displaying options -> http://momentjs.com/docs/#/displaying/

或者只是:

const date = new Date('2017-10-01T12:00:00.000Z');
date.setMinutes(-360);
date.toISOString();

这篇关于Javascript:如何从UTC字符串和偏移量解析日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆