0

IE11's Date.parse function "succeeds" on a large number of invalid dates.

Without using a third-party library, what is the proper way to validate whether a string in the format YYYY/MM/DD contains an actual "legal" date (e.g. "2020/02/29" succeeds, but "2019/02/29" fails, etc) in javascript?

Examples from IE11 console:

new Date(Date.parse('2020/05/99')     // Fri Aug 07 2020
new Date(Date.parse('2020/05/100')    // Sat Aug 08 20202
new Date(Date.parse('2020/05/1000')   // Wed Jan 25 2023
new Date(Date.parse('2020/69/800000') // Fri Dec 29 4215
new Date(Date.parse('2020/69/1000')   // Sat May 27 2028
new Date(Date.parse('2020/70/1000')   // Invalid Date
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Michael Waddell
  • 279
  • 2
  • 6
  • 2
    Please refrain from sharing screenshots of code/text. Always copy-paste to better readability for the people who's gonna answer your question :) – Balastrong May 27 '20 at 19:29
  • 3
    Not to sound like a smart ass, but your options are to either: a) use a library or b) write your own. Date.parse(....) will accept a wide range of date formats -- and different browsers will support different formats. – Jeremy J Starcher May 27 '20 at 19:36
  • It "succeeds" by not throwing an error. Doesn't mean it's correct - when you parse non-standard date strings, you can get any non-standard behaviour. I wouldn't recommend relying on it in any way shape or form. Either a) use a date parsing library or b) use standard date strings. – VLAZ May 27 '20 at 19:37

2 Answers2

0

Thank you all for the feedback. This is for an application which has very strict limits on third-party libraries, so I was hoping there was a built-in javascript function that would support this, but that appears to not be the case.

Unless anyone knows of a better approach, I'm going to use this example code from w3resource.com for evaluating the strings manually - https://www.w3resource.com/javascript/form/javascript-date-validation.php

Thanks again, Michael

Michael Waddell
  • 279
  • 2
  • 6
0

You could write something like this:

function parseDate(dateString) {
  var dateParts = dateString.split("/"),
      years = dateParts[0],
      months = dateParts[1],
      days = dateParts[2]

  // sanity check date parts
  if (days > 31) return "Invalid Date - Days"
  if (months > 12) return "Invalid Date - Months"

  var myDate = new Date(years, months, days)

  return myDate
}

Demo in Stack Snippets

function parseDate(dateString) {
  var dateParts = dateString.split("/"),
      years = dateParts[0],
      months = dateParts[1],
      days = dateParts[2]
  
  // sanity check date parts
  if (days > 31) return "Invalid Date - Days"
  if (months > 12) return "Invalid Date - Months"
  
  var myDate = new Date(years, months, days)
  
  return myDate
}

console.log('2020/05/15: ', parseDate('2020/05/15'))
console.log('2020/05/99: ', parseDate('2020/05/99'))
console.log('2020/05/100: ', parseDate('2020/05/100'))
console.log('2020/70/1000: ', parseDate('2020/70/1000'))
KyleMit
  • 30,350
  • 66
  • 462
  • 664