0

When I try to parse a string - to check if it is a valid date, javascript parses it as a date, and returns a valid date.

console.log(new Date("Agent 007"));  // returns Sun Jul 01 2001 00:00:00 GMT+0200 
console.log(Date.parse("Agent 007")); // returns 993938400000
console.log(moment.utc("Agent 007").valueOf()); // returns 993945600000
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>

How to prevent it ?

For you @Andreas: Chrome Debugger console window: enter image description here

Andreas
  • 21,535
  • 7
  • 47
  • 56
user759792
  • 61
  • 7
  • which browser/node version are you using ? – jonatjano Jul 29 '20 at 07:13
  • 1
    In addition to the [standard date time string formats](https://stackoverflow.com/questions/51715259/what-are-valid-date-time-strings-in-javascript), JavaScript implementations are allowed to parse whatever they want. You can't prevent that. – str Jul 29 '20 at 07:20
  • 1
    See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Jul 29 '20 at 08:59

0 Answers0