dayjs
dayjs copied to clipboard
dayjs not parsing german date string
dayjs is not able to parse a date string with this format: DD.MM.YYYY (2-digits day.2-digits month.4-digits year).
Given the date string "22.04.2022", dayjs tells me it is an invalid date.
Example:
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.1/dayjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.1/locale/de.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.1/plugin/localizedFormat.js"></script>
<script>
dayjs.locale('de')
console.log(dayjs().locale()) <!-- Output: de -->
console.log(dayjs('22.04.2022')) <!-- Output: M {$L: 'de', $d: Invalid Date, $x: {…}, $y: NaN, $M: NaN, …} -->
</script>
Load the CustomParseFormat
plugin and provide one of the localized formats from this list when calling dayjs()
:
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import 'dayjs/locale/de';
dayjs.extend(customParseFormat);
dayjs.locale('de');
dayjs('22.04.2022', 'L') // 2022-04-22T00:00:00+03:00