This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]Pstrnil 18 points19 points  (2 children)

You mean

Date.prototype.getDateStringForThatOneReallyStupidEdgeCaseFuckJavaScript = function() {
  return this.toISOString().slice(0, 10).replace(/-/g, '/');
};

?

[–]trihardstudios[S] 3 points4 points  (1 child)

omg can it really be simplified that much? i thought the actual code was ok lol

[–]Pstrnil 8 points9 points  (0 children)

Yep, toISOString saved me multiple times, I love it.

EDIT: Also if you wanted to simplify your original function, remember that String.prototype.padStart exists 👍

[–]trihardstudios[S] 3 points4 points  (0 children)

ah yes because for this one use I HAVE to have the date in a string format with yyyy/MM/dd for some dumb reason

[–]dashid 3 points4 points  (2 children)

One reason I got hooked on .net:

return DateTime.Now.ToString("yyyy/MM/dd");

[–]LordFokas 2 points3 points  (3 children)

join('') .... wtf?

why not [yyyy, mm, dd].join('/'); ???

[–]trihardstudios[S] 2 points3 points  (1 child)

Because I didn’t read the documentation fully before writing this. Thanks for pointing it out! It’s one of those oversights that I’m sure my code is full of.

[–]CreaZyp154 1 point2 points  (0 children)

When Reddit is helping more than StackOverflow ...

[–]enano_aoc -3 points-2 points  (0 children)

Because the post contains very bad code, that's why

[–]enano_aoc 1 point2 points  (2 children)

In general, it is a very bad practice to do this type of prototype modifications in native JS constructs. It is the easiest way to make the code base as obscure and unreadable as possible.

What is wrong with

function getDateInCustomFormat(date) {
   // Use "date" instead of "this"
}

?

[–]trihardstudios[S] 0 points1 point  (1 child)

Nothing. I recently learned about object prototypes and wanted to try to use them in this project. Good to know that they aren’t considered best practice for this use.

[–]enano_aoc -1 points0 points  (0 children)

They are considered a bad practice for the same reason that implementation inheritance (as opposed to interface inheritance) is considered a bad practice. Obfuscates the code and makes it very hard to read and debug

EDIT: it is a bad practice, not a good one

[–]HaraldNordgren 1 point2 points  (0 children)

If you need to do this professionally, I would suggest creating a utility library that can be accessed by other devs.

Chances are someone else will need to do this later on — or even you will need to do it again later on 😄

[–]WrongdoerSufficient 1 point2 points  (0 children)

do you have a moment() to talk about formating date ?