You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Luxon解析德语日期字符串失败,求问题排查与解决方法

Luxon解析德语格式日期失败问题排查

问题描述

我想用Luxon将德语格式日期字符串“02.03.2023”(对应2023年3月2日)解析为DateTime对象,编写了如下代码:

const format = 'dd.MM.YYYY';
console.log(format);
const date = DateTime.fromFormat('02.03.2023', format, {
    locale: 'dt-AT',
});
console.log(date);

运行后解析无效,控制台输出:

{
  "ts": 1710780576251,
  "_zone": {},
  "loc": {
    "locale": "de-AT",
    "numberingSystem": null,
    "outputCalendar": null,
    "intl": "de-AT",
    "weekdaysCache": {
      "format": {},
      "standalone": {}
    },
    "monthsCache": {
      "format": {},
      "standalone": {}
    },
    "meridiemCache": null,
    "eraCache": {},
    "specifiedLocale": null,
    "fastNumbersCached": null
  },
  "invalid": {
    "reason": "unparsable",
    "explanation": "the input \"02.03.2023\" can't be parsed as format dd.MM.YYYY"
  },
  "weekData": null,
  "c": null,
  "o": null,
  "isLuxonDateTime": true
}

错误原因与修复

有两处问题导致解析失败:

  • 年份格式符错误:YYYY在Luxon中代表ISO周年(基于周数的年份),而你输入的是常规公历日期,应该使用yyyy表示公历年份。
  • Locale拼写错误:dt-AT是无效标识,正确的奥地利德语Locale应为de-AT(虽然Luxon自动修正了Locale,但规范写法很重要)。

修正后的代码:

const format = 'dd.MM.yyyy';
console.log(format);
const date = DateTime.fromFormat('02.03.2023', format, {
    locale: 'de-AT',
});
console.log(date);

运行后即可正确解析出对应2023年3月2日的DateTime对象。


内容的提问来源于stack exchange,提问作者Gener4tor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 10:15:22