Help with dateObject.toString() === "Invalid Date"

So, in this exercise they use dateObject.toString() === “Invalid Date” to make sure it is a valid date. I have a few question.

I tried to search on MDN’s documentation about the “Invalid Date” error. The idea is that I don’t know about the existence of it, so I want to get to the kind of error that is returned when there’s an invalid date (could be null ,undefined, or apparently anything else. So, I went to Date.prototype.toString() and Date MDN’s documentation. I could not find any reference about “Invalid Date”. So:

  • How am I supposed to find it by browsing if I don’t know about it’s existence?

  • Where is it the be found BY BROWSING MDN’S DOCUMENTATION?

  • Why does it take the form of a string(instead of null, undefined, etc)?

  • Why did they choose the dateObject.toString() method to validate the date on the resolution?

  • Which other method could have been used and why?

This is the web for the error itself, but it seems to be disconnected somehow from the Date documentation (can’t find a way to get by growsing from Date to the error page). And besides, it states that the error comes from Date or Date.parse(), but in the resolution they use dateObject.toString() to get the error, which seems unnecessary, or at least, arbitrary. Please, make any comment on this.

Thanks!!

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36.

Challenge: Timestamp Microservice

Link to the challenge:

If you console.log a new Date() with invalid parameters e.g new Date(‘3939292929’), they return ‘Invalid Date’. You can try in chrome console.

The MDN sometimes doesn’t explain everything so we need to explore things… I’m not an expert but hope this helps.

I didn’t use the toString() when doing this challenge tho… maybe this is bad practice but this works new Date(entry) != 'Invalid Date'.

As a fellow newbie, I think console.log is very helpful to understand how the program works. Sorry for not answering your questions as I don’t know the answers.

Thanks @danzel-py for your reply. I think it’s a great idea using the console, and I see it as a workaround, I just want to know if there’s anything I’m missing while browsing for information on “official” documentation.

At the top of the articles about Date.prototype.toString() and Date/parse is a demo box where you can try things. If you enter an invalid date there, you’ll see what’s returned:

  • for Date.parse() it’s NaN
  • for Date.prototype.toString() it’s "Invalid Date"

Plus, when I throw “MDN invalid date” into my search engine, this is the first hit:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_date

It’s really not that difficult to find.


null and undefined could occur in your code for all kinds of reasons, they just don’t give as much information as "invalid date". Same with NaN. Also, checking for NaN requires some understanding of that value, because:

const date = Date.parse('abcdefg'); // NaN

console.log(date === NaN); // false

Off the top of my head, I can’t think of any. Checking for "invalid date" seems like the smartest solution.


I don’t know where to find the code of the resolution you’re referring to, but I hope the above already explained it.

Thanks @jsdisco for your reply. Maybe you did not read my post that well, or you just did not understand. First of all, I’ve found and linked to the post the web about MDN invalid date, you did not see it. Second, I’m not looking for a demo box, just to know if I’m missing a way to browse the documentation that takes me from Date to date error responses. That’s what was asked. See, I can’t search for something I don’t know that exists (“Invalid Date”), it’s a general question, and in this is an example case. Thanks again.

I’ve read your post very well, I just don’t understand the problem. You can reach the relevant part in MDN by starting at the Date page and clicking on the article for Date.parse.

You don’t have to “magically know” about the existence of “invalid date”, one of the user stories expects you to check the validity of the date, so you can find the answer relatively easy by googling “javascript how to check for invalid date”, I don’t understand why it’s so important that you reach that point just by clicking your way through MDN.

I agree that sometimes it’s difficult to navigate, because many topics have multiple articles in completely different sections (behind \reference or behind \learn), and cover different aspects with varying depth. So, if you think you’re missing something about “how to use MDN”, I don’t think that’s the case. It’s a monster of a collection of documents. There’s no way for you to know that you have to click on Date.parse in order to find what you’re looking for. Often, it’s more efficient to use a search engine and enter “MDN whatever I want to know about”, because you’ll get the most highly ranked articles for your search keywords as first results.

1 Like