如何将此字符串转换为 Luxon Datetime 对象(Typescript)?

问题描述 投票:0回答:1

我有以下 Javascript/Typescript 代码将字符串转换为 Luxon DateTime:

import { DateTime } from 'luxon';    
const x = '2023-10-27T01:00:57.830+00:00'
const y = DateTime.fromFormat(x, 'yyyy-MM-dd');
console.log('y = ', y)

这会产生以下输出:

  y =  DateTime {
    ts: 1698282057830,
    _zone: SystemZone {},
    loc: Locale {
      locale: 'en-US',
      numberingSystem: null,
      outputCalendar: null,
      intl: 'en-US',
      weekdaysCache: { format: {}, standalone: {} },
      monthsCache: { format: {}, standalone: {} },
      meridiemCache: null,
      eraCache: {},
      specifiedLocale: null,
      fastNumbersCached: null
    },
    invalid: Invalid {
      reason: 'unparsable',
      explanation: `the input "2023-10-27T01:00:57.830+00:00" can't be parsed as format yyyy-MM-dd`
    },
    weekData: null,
    c: null,
    o: null,
    isLuxonDateTime: true
  }

如何正确转换这个对象?

javascript typescript datetime luxon
1个回答
0
投票

尝试使用

fromISO
方法。

import { DateTime } from 'luxon';

const x = '2023-10-27T01:00:57.830+00:00';
const y = DateTime.fromISO(x);
© www.soinside.com 2019 - 2024. All rights reserved.