Quick question about date and string types

I have a date property (e.g: createdAt) from the backend. I'm sending it to the frontend as JSON data. Since JSON only works with strings, what's a good way to handle this as my TypeScript definitions are Dates. Should I rewrite my types to take Date | string or should I make some type of parsing function? I feel like if I make it a Date | string, it'll be a bit ambiguous. I have multiple functions in the frontend that handle the createdAt as a Date, no parsing method needed. But when I get the createdAt from the backend, I'll need to know it's from the backend and parse it as a date in the frontend. Does that make sense? I'm trying to word it but I guess what I'm trying to get at is it might not be 'standardized' when working with it in the frontend. Typing that out, I realize what I could potentially do is rework my frontend methods that use createdAt to get the data from the backend, now that I actually have it setup, that way it'll be in a standardized format
33 Replies
ἔρως
ἔρως2w ago
json doesn't have a date type it's all string you can use the replacer function that js has
vince
vinceOP2w ago
Should I just specify createdAt as a string and treat it as a datetime string? Even with Date | string I can't use Date.parse() since it expects a string
ἔρως
ἔρως2w ago
you can parse the string
vince
vinceOP2w ago
Right, it's more of a question of how should I specify the type for it. It's semantically a Date but it can either be a Date or datetime string depending on where the data is coming from (backend as JSON or already parsed as a Date in the frontend) If I specify it as a Date | string I can't pass it to Date.parse(), so should I just specify the type as a string?
ἔρως
ἔρως2w ago
you know what you can do? convert it to a string, to clone the date object
vince
vinceOP2w ago
Wdym?
ἔρως
ἔρως2w ago
instead of worrying about how to handle it, just convert it to a string the toString() method exists on dates and on strings
vince
vinceOP2w ago
Ohh I see what you're saying okay. I definitely could do that, but I also just tried the Date constructor and that worked as well 😅
ἔρως
ἔρως2w ago
DO NOT USE THE DATE CONTRUCTOR do not
vince
vinceOP2w ago
Why?
ἔρως
ἔρως2w ago
seriously the format is unspecified and each browser does it's own thing and handling is done differently between browser versions too
vince
vinceOP2w ago
Oh god I get why everyone says dates are a PITA to work with now 😂 This is ridiculous
ἔρως
ἔρως2w ago
it is it is so very undefined
vince
vinceOP2w ago
So if I can't use the date constructor how do I actually make a date object / parse date strings?
ἔρως
ἔρως2w ago
depends if you have a string, use the parse method for everything else, create an object without value in the constructor if you need, you can set a timestamp after
vince
vinceOP2w ago
Alright so I have this:
.setTimestamp(Date.parse(leaderboard.createdAt.toString()));
.setTimestamp(Date.parse(leaderboard.createdAt.toString()));
(ignore the .setTimestamp, I'm building a discord embed). I feel like this is still a crap solution since createdAt can be Date | string and yes it works but it's like meh :/ Should I make a separate parsing function so it's more semantic / explicit that 'hey, this is a leaderboard / createdAt from the backend and we need to parse' type of thing instead of just a random one of Date.parse() with .toString()? Granted I'm only using this once at the moment but I feel like it's almost like a 'magic number' type of solution if that makes sense
ἔρως
ἔρως2w ago
nah, just add documentation saying why you're doing this and that if the value is a date, converts to a string, effectively cloning the date object and not affect the date object and a string just assures it is a string
vince
vinceOP2w ago
Word okay thanks epic!
ἔρως
ἔρως2w ago
you're welcome if you need to do anything else with dates, for the love of god, find an alternative to moment,js
vince
vinceOP2w ago
Lol I'll keep that in mind
ἔρως
ἔρως2w ago
lol alright
vince
vinceOP2w ago
Well glad that's done - spent the last hour / hour and a half on that 😂
ἔρως
ἔρως2w ago
what were you trying to do before?
vince
vinceOP2w ago
I was trying to figure out why the setTimestamp() method wasn't working, then I realized what I thought was a Date was actually a string and was trying to brainstorm how to actually keep it as a Date. But then I was like it's from the backend so it's JSON and has to be a string so what's actually the best way to standardize the data which is when I asked here (kind of silly that the setTimestamp() method from the discord.js library can't handle a datetime string imo but hey, probably would have ran into this issue again sometime down the road anyway)
ἔρως
ἔρως2w ago
they don't want to deal with the complexity bs
vince
vinceOP2w ago
Yea it either takes a Date, number, or null
ἔρως
ἔρως2w ago
smart
clevermissfox
clevermissfox2w ago
I've been wrestling with converting data types back and forth all day too, I feel your pain.
ἔρως
ἔρως2w ago
dates are one of the worst pitad in js
OrionPax
OrionPax3d ago
Spoiler alert: They are a pain in many other programming languages as well. 😦
ἔρως
ἔρως3d ago
but in javascript, it's specially shitty compared to php, mysql and a few other languages
OrionPax
OrionPax3d ago
Oh, I agree. Just what they did to months with the Date constructor. And if you really want to punish yourself, get into timezones.
ἔρως
ἔρως3d ago
i would rather eat a cactus for breakfast

Did you find this page helpful?