Question About DayJS Date Formatting

Hey everyone, sorry for the "just google it lol" question but I've tried searching around and can't find anything. I am trying to parse a date using dayJS with the following code. However, it returns invalid date. Could anyone point me in the right direction on how I could solve this? Thanks!
import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";
import timezone from "dayjs/plugin/timezone";
import DayJSUtc from "dayjs/plugin/utc";

dayjs.extend(DayJSUtc);
dayjs.extend(customParseFormat);
dayjs.extend(timezone);

console.log(dayjs("11/05/22 9:00 AM", "MM/DD/YY hh:mm A", "America/Chicago")) // Invalid Date
import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";
import timezone from "dayjs/plugin/timezone";
import DayJSUtc from "dayjs/plugin/utc";

dayjs.extend(DayJSUtc);
dayjs.extend(customParseFormat);
dayjs.extend(timezone);

console.log(dayjs("11/05/22 9:00 AM", "MM/DD/YY hh:mm A", "America/Chicago")) // Invalid Date
5 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JulieCezar
JulieCezar2y ago
Although not directly an answer to your question... I only use UTC to display the date string, but when saving it somewhere (like DB) I use the ISO string format. ISO can easily be parsed to dayjs() and you can add the timezone offset depending on the user's timezone. And then you won't have the problem you have now.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JulieCezar
JulieCezar2y ago
ISO is zero timezone offset. So technically GMT-0. Although this article says to use with timezone I find impractical... So I just use the ISO format. Actual example of that from my app --> When a user registers, they get a activation code which is valid for 1h. If I insert the date I get from my client, it will have the clients timezone which will then be saved to db. But then when I check if the activation code is still valid, I do it on the server. But what happens if the server's timezone is different than the user's? Then the timezones get mixed and we got a problem. Which is why I always use ISO, then it's easy to get the timezone offset. Might not be the prettiest, but it works.
{dayjs(order.date)?.utc()?.tz(dayjs.tz.guess(), true)?.toDate().toDateString()}
{dayjs(order.date)?.utc()?.tz(dayjs.tz.guess(), true)?.toDate().toDateString()}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server