I want to parse a german Date "02.03.2023" (Which is the second of March 2023) to a dateTime with luxon. This is my code:
const format = 'dd.MM.YYYY';
console.log(format);
const date = DateTime.fromFormat('02.03.2023', format, {
locale: 'dt-AT',
});
console.log(date);
But unfortunally the result is invalid. This is what my console logs:
{
"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
}
What I'm doing wrong?
Issue is the format token, you should use lowercase yyyy
for four-digit year instead of YYYY
, see Table of tokens in the documentation.
Example:
const DateTime = luxon.DateTime;
const format = 'dd.MM.yyyy';
console.log(format);
const date = DateTime.fromFormat('02.03.2023', format, {
locale: 'dt-AT',
});
console.log(date.toISO());
<script src="https://cdn.jsdelivr.net/npm/luxon@3.4.4/build/global/luxon.min.js"></script>