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 Date
s. 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 format33 Replies
json doesn't have a date type
it's all string
you can use the replacer function that js has
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 stringyou can parse the string
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?
you know what you can do?
convert it to a string, to clone the date object
Wdym?
instead of worrying about how to handle it, just convert it to a string
the
toString()
method exists on dates and on stringsOhh 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 😅
DO NOT USE THE DATE CONTRUCTOR
do not
Why?
seriously
the format is unspecified and each browser does it's own thing
and handling is done differently between browser versions too
Oh god I get why everyone says dates are a PITA to work with now 😂
This is ridiculous
it is
it is so very undefined
So if I can't use the date constructor how do I actually make a date object / parse date strings?
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 afterAlright so I have this:
(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 sensenah, 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
Word okay thanks epic!
you're welcome
if you need to do anything else with dates, for the love of god, find an alternative to moment,js
Lol I'll keep that in mind
lol alright
Well glad that's done - spent the last hour / hour and a half on that 😂
what were you trying to do before?
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)they don't want to deal with the complexity bs
Yea it either takes a Date, number, or null
smart
I've been wrestling with converting data types back and forth all day too, I feel your pain.
dates are one of the worst pitad in js
Spoiler alert: They are a pain in many other programming languages as well. 😦
but in javascript, it's specially shitty compared to php, mysql and a few other languages
Oh, I agree. Just what they did to months with the Date constructor. And if you really want to punish yourself, get into timezones.
i would rather eat a cactus for breakfast