【问题标题】:Create JavaScript Date object from date string + time string + timezone offset string从日期字符串 + 时间字符串 + 时区偏移字符串创建 JavaScript Date 对象
【发布时间】:2020-08-18 21:27:21
【问题描述】:

我总共有 4 个不同的输入,即:

  1. 日期字符串 (2020-05-05)
  2. 时间字符串 (15:30)
  3. 时区偏移 (-09:00)

无论我的本地浏览器时区是什么,我都想将这些字符串组合成一个日期时间对象,例如 (2020-05-05T15:30:00-09:00)。问题是当我组合这些字符串并尝试使用 new Date() 函数创建日期对象时,我的日期时间被转换为 UTC 时间戳。

我试过了:

const date =
  moment(this.actualDateOfSurgeryDate).format(YYYYMMDD) +
  'T' +
  moment(this.actualDateOfSurgeryTimeDropDown + ' ' + this.actualDateOfSurgeryTimeAM_PMDropDown, ['h:mm A']).format('HH:mm:ss') +
  offsetTime;

this.caseDetail.actualDateOfSurgery = new Date(date);

这给了我类似的输出:2020-05-05T04:30:00.000Z

如何获得我想要的输出:2020-05-05T15:30:00-09:00 ?? 我的项目中有可用的时刻 js

【问题讨论】:

  • 您要完全忽略时区吗?
  • 实际上,我有专门的时区选择作为下拉列表列出所有时区,我选择的时区偏移量为“-09:00”,我想在时间戳中附加它

标签: javascript datetime timestamp momentjs


【解决方案1】:

由于夏令时,我得到 16:30

3 月之前或 10 月之后的日期将给出 15:30

let dateString = "2020-05-05"+"T"+"15:30"+":00"+"-09:00"
console.log(dateString)
const date = new Date(dateString)
console.log(date)
const Anchorage = date.toLocaleString('en-US', {timeZone: 'America/Anchorage', hour12: false})
console.log(Anchorage)

let options = {}
options.timeZone = 'America/Anchorage';
options.timeZoneName = 'short';
console.log(date.toLocaleDateString('en-US'), date.toLocaleTimeString('en-US', options));

【讨论】:

  • 这确实有效,但我没有时区名称。我只是有像'-09:00'这样的偏移量
  • 所以从 Wikipedia 和 mdn 创建一个查找表。就这样找到了锚地
【解决方案2】:

我想将这些字符串组合成一个日期时间对象,例如 (2020-05-05T15:30:00-09:00)

日期对象非常简单,它们只是一个时间值,它是自 1970-01-01T00:00:00Z 以来以毫秒为单位的偏移量,因此本质上是 UTC。内置解析器不可靠,并且缺少格式标记等任何功能。

因此,如果您有单独的值,例如:

  • 日期字符串 (2020-05-05)
  • 时间字符串 (15:30)
  • 时区偏移 (-09:00)

然后您可以创建一个符合defined in ECMA-262 格式的字符串,并且 应该 由内置解析器正确解析,例如

new Date('2020-05-05T15:30:00.000-09:00')

但是,由于实现方式的差异,一般建议是避免使用内置解析器。此外,格式必须准确(例如,在时间戳中包含秒和毫秒,在偏移量中包含冒号 (:)),否则某些实现会将其视为格式错误并返回无效日期。

一旦你有了一个 Date 对象,获取一个带偏移量的“本地”时间戳就是一个格式问题,之前已经回答过很多次了(例如How to format a JavaScript date)。没有任何像样的内置格式化函数(toLocaleString with options 可以用于某些用途,但通常缺乏功能),因此您必须要么 write your own function,要么使用库。

以下示例使用Luxon,建议作为moment.js的升级路径。

使用 Luxon,如果您指定一个代表位置,您将获得该位置在日期和时间的偏移量。或者,您可以将偏移量固定为一个设定值,本质上将其设置为没有代表性位置的时区,因此它没有任何对夏令时或历史偏移量更改的参考:

let DateTime = luxon.DateTime;

// Offset per the specified location
let d0 = DateTime.fromISO('2020-01-01', {zone: 'America/Yakutat'});
let d1 = DateTime.fromISO('2020-06-30', {zone: 'America/Yakutat'});
console.log(d0.toString());
console.log(d1.toString());

// Fixed offset per the supplied string
let d2 = DateTime.fromISO('2020-05-05T15:30:00.000-09:00', { setZone: true});
let d3 = DateTime.fromISO('2020-01-01T15:30:00.000-09:00', { setZone: true});
console.log(d2.toString());
console.log(d3.toString());
  
<script src="https://cdn.jsdelivr.net/npm/luxon@1.24.1/build/global/luxon.min.js"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2012-10-08
    • 1970-01-01
    相关资源
    最近更新 更多