dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

Invalid date while parsing `YYYYMMMDD` or `DDMMMYYYY`

Open lbtoma opened this issue 2 years ago • 5 comments

Describe the bug All the following inputs give Invalid Date as output:

  • dayjs("2023NOV01")
  • dayjs("01NOV2023")
  • dayjs("01NOV2023", "DDMMMYYYY")
  • dayjs("2023NOV2023", "YYYYMMMDD")

Works as expected on Chrome and Node Works as expected for:

  • dayjs("2023-NOV-01")
  • dayjs("01-NOV-2023")
  • dayjs("01-NOV-2023", "DD-MMM-YYYY")
  • dayjs("2023-NOV-2023", "YYYY-MMM-DD")

Expected behavior Expected to return a valid dayjs object.

Information

  • Day.js Version 1.11.9 and 1.10.7
  • OS: OSX
  • Browser Firefox and Safari
  • Time zone: EDT (East Daylight Time)

lbtoma avatar Jul 10 '23 21:07 lbtoma

Temporary workaround:

const dayjsDate = dayjs(
  dateString
    .replace(/[0-9][A-z]/, (a) => `${a[0]}-${a[1]}`)
    .replace(/[A-z][0-9]/, (a) => `${a[0]}-${a[1]}`),
);

lbtoma avatar Jul 10 '23 21:07 lbtoma

@lbtoma i had same issue, use this plugin to fix it: https://day.js.org/docs/en/plugin/custom-parse-format

amir-armantaheri avatar Jul 21 '23 11:07 amir-armantaheri

Yeah, even using the plugin the problem persists on my end 😢

lbtoma avatar Jul 21 '23 11:07 lbtoma

At first I thought: MMM outputs a capitalized string (Nov) so it expects the same input.

However these tests show, that the output is not very predictable and adding customParseFormat creates more invalid dates: https://codepen.io/a-sholz/pen/XWyBMBr?editors=0010

func with customParseFormat without customParseFormat
dayjs('2023NOV01') works works
dayjs('01NOV2023') works works
dayjs('01Nov2023', 'DDMMMYYYY') works works
dayjs('01-Nov-2023', 'DD-MMM-YYYY') works works
dayjs('01NOV2023', 'DDMMMYYYY') invalid date works
dayjs('01-NOV-2023', 'DD-MMM-YYYY') invalid date works
dayjs('2023-NOV-01') works works
dayjs('01-NOV-2023') works works
dayjs('01-NOV-2023', 'DD-MMM-YYYY') invalid date works
dayjs('2023-NOV-01', 'YYYY-MMM-DD') invalid date works

Edit: This is only true for Chrome. It's a different story in Safari and Firefox

LucidityDesign avatar Jul 22 '23 09:07 LucidityDesign

I am seeing a similar issue: https://jsfiddle.net/ygtuoLpn/12/

My format is 'DDMMMYY HH:mm' for '02JAN24 20:15'

rklomp avatar Jan 02 '24 14:01 rklomp