iamkun / dayjs

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
https://day.js.org
MIT License
46.98k stars 2.3k forks source link

Parsing string to time issue with post meridiem #1495

Open Subha opened 3 years ago

Subha commented 3 years ago

Describe the bug Replaced our application from moment to use dayjs and facing issues with our Timepicker.

while parsing this string to time, had to pass the format as array of strings unlike Moment but still we are not getting the expected result.

Plugins used: import CustomParseFormat from "dayjs/plugin/customParseFormat"; import advancedFormat from "dayjs/plugin/advancedFormat";

Current Behavior dayjs('3p', ['LT', 'h:mm', 'hh', 'h a', 'h']).format('LT'); // result 3.00 AM dayjs('3:03 PM', ['LT', 'h:mm', 'hh', 'h a', 'h']).format('LT'); // result 3.03 AM

Expected behavior

When 3p is passed expected to return 3:00 PM

Match moment api and pass same format.

moment('3p', 'LT').format('LT'); // result 3:00 PM moment('3:03 pm', 'LT').format('LT'); // 3:03PM

Information

BePo65 commented 2 years ago

In dayjs the format 'LT' translates to 'h:mm A' and 'A' allows for 'pm', 'PM', 'am' or 'AM' but mot for 'a' or 'p' alone; therefore '3p' does not match neither 'LT' nor 'h a' (you see the extra space).

And then you get

import CustomParseFormat from "dayjs/plugin/customParseFormat";
import advancedFormat from "dayjs/plugin/advancedFormat";

dayjs.extend(customParseFormat)
dayjs.extend(advancedFormat)

dayjs('3pm', 'ha').format('LT'); // result 3:00 PM
dayjs('3:03 PM', 'LT').format('LT'); // result 3:03 PM