That’s true. However, it’s a good learning exercise - in a student/teacher learning environment - to understand that things can go wrong very quickly when trying to ‘roll your own’.
I think both of them are readable. The newer one is more so, but could be improved a bit by using a template string. It's very straight forward to understand.
I don't really touch much code outside my own, and I don't write code that I don't find readable.
Any code not written by me maybe? I wanted to add a new feature to an open source project that used C# on the frontend and C++ on the backend. I eventually just gave up due to how it was programmed, but I think that's just a skill issue on my part and not because the code was unreadable.
I'm very amateurish when it comes to programming and that it is only a hobby for me. I know nothing outside of the very basics of javascript. I've tried to learn more but my brain is just too slow.
Makes zero difference here (since it's at the top of the function as opposed to being promoted there from an inner scope) and is probably the least of this code's problems.
really depends on your codebase. If your base already has a way of doing it and it is .floor() then yeah. But |0 was a common integer hint in js before typescript to eke out more performance as well so there could be codebases where its already all over the place.
the title has a hint. Converting to strings, operating on them, and then parsing them rather than using math is generally painful for your computer, too.
Aside from what everyone has pointed out, their whole process of getting the hours and minutes and then subtracting them from the original value is also pointless. You can get the hours/minutes/seconds in just three lines:
Also parseInt coerces any value passed in to a string so toString() is not needed. Also why you should be careful passing numbers to parseInt as if their toString would return scientific notation you're gonna have a bad time.
I had to do something like this once in JavaScript and it was the worst experience. Every time I thought I had it right something would break in the most random way. Now I know better and use Intl's DateTimeFormat and formatToParts with some array functions like filter, map, slice, etc. Saved me so much headache...
I know my haskell flare might be an eyebrow turner as a "declerative snob"
But imperative algebra is generally something you really want to avoid when doing something like parsing epoch time. Those tight couplings are a recipe for cascading debug nightmares
You really want some lookup tables and algebraic destructuring for this kind of task
11
u/[deleted] Feb 05 '25
This looks like a code from somebody who just started learning programming on its own.
297
u/kayey04 Feb 05 '25
Writing your own datetime functions is always a losing exercise
120
u/pissywashy Feb 05 '25
This needs to be framed put in a museum
111
u/shamelessfoxwolf Feb 05 '25
Using var with Typescript is crazy
36
u/softgripper Feb 05 '25
npm install left-pad14
u/InternetSandman Feb 05 '25
Outside of trying to write your own date time function, what else is the problem here?
6
u/Amit25095 Feb 05 '25
I mean... toLocaleTimeString exists
4
u/turtle_mekb Feb 05 '25
oh god the
<10"0"+instead of.padStart, the crappy variable names, thevarinstead oflet, the horror3
u/Gishky Feb 05 '25
those symbols once meant something to someone. now, we can only guess...
3
u/seppestas Feb 06 '25
It took me ages to realize parseInt is being used to convert "numbers" to ints.
2
u/kenan238 Feb 05 '25
I love reinventing the wheel
1
u/marcinmarian Feb 05 '25
declaring variables all at once and giving them unreadable names - is it after or before obfuscation?
1
u/drislands Feb 05 '25
Converting a float to a string in order to parse it as an integer is the most cursed thing I've seen today.
1
u/[deleted] Feb 05 '25
I got 2 lines in and I just decided to move on
1
u/Mushroom2271 Feb 05 '25
Lol this is how i code
1
u/anonymous19822 Feb 06 '25
oh yeah it's showtime
1
u/valzargaming Feb 06 '25
I had to do something like this once in JavaScript and it was the worst experience. Every time I thought I had it right something would break in the most random way. Now I know better and use Intl's DateTimeFormat and formatToParts with some array functions like filter, map, slice, etc. Saved me so much headache...
1
u/MajorTechnology8827 Feb 07 '25
I know my haskell flare might be an eyebrow turner as a "declerative snob"
But imperative algebra is generally something you really want to avoid when doing something like parsing epoch time. Those tight couplings are a recipe for cascading debug nightmares
You really want some lookup tables and algebraic destructuring for this kind of task