dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

dayjs not parsing german date string

Open ooorndtski opened this issue 2 years ago • 1 comments

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>

ooorndtski avatar Apr 22 '22 12:04 ooorndtski

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

tbaliukynas avatar Apr 22 '22 15:04 tbaliukynas