0

I have been searching a lot to find out how can I know whether the date has overflown. Basically I want to identify invalid date strings. for Example using normal javascript date class or moment when we provide a date string like "4/31/2021" it overflows because April doesn't contain 31st date. so it jumps to next date valid date that is "5/1/2021".

moment has this strict validation like

moment("4/31/2021","MM/DD/YYYY",true).isValid() //false

where it provides strict validation for such overflown dates. but since in my use Case user can enter whatever the date string he wants and in different formats. I want to validate the date string. so the only way for me is to iterate over all the known formats and check with true flag but then sometimes user can also enter time component with it which makes it even worse then we need to iterate through all the datetime formats as well.

is there any better/optimized way to handle this?

aravind_reddy
  • 5,236
  • 4
  • 23
  • 39
  • 1
    Is your overflow feature really necessary here, or could you live with just having validation of your input dates? – Tim Biegeleisen Feb 19 '21 at 06:22
  • yes overflow feature is necessary for me i need to mark invalid for dates like "4/31/2021" – aravind_reddy Feb 19 '21 at 06:23
  • 1
    That doesn't actually answer the question I asked in the above comment... – Tim Biegeleisen Feb 19 '21 at 06:24
  • oh sorry, I don't need overflow for me I just need to validate my input dates – aravind_reddy Feb 19 '21 at 06:27
  • @RobG with due respect, I don't think it's duplicate because here the date input is a string coming from the user and it might be of any format. if I know the format beforehand I could have easily used the strict argument of momentjs. but that is not the case here. – aravind_reddy Feb 19 '21 at 07:52
  • 1
    It starts “*I'm trying to test to make sure a date is valid in the sense that if someone enters 2/30/2011 then it should be wrong*”, so yes, it’s about validating string dates. – RobG Feb 19 '21 at 13:13
  • yeah, how do I check that validation keep in mind that the input can be of any date/time format? the answer you mentioned as duplicate splits the date by ''-" but that is not the case here – aravind_reddy Feb 19 '21 at 14:03
  • It’s the same issue, you want to parse an arbitrary format timestamp. So you have to break it into tokens, test each one to see what it might be, then test patterns to see which match date parts and deal with ambiguities with out any hint as to the format. That’s pretty much what *Date.parse* does. It’s been around for 25 years and likely hundreds if not thousands of developers have worked on it in hundreds of implementations, yet it’s still seen as unreliable. The bottom line is that parsing arbitrary timestamp formats can’t be reliable. – RobG Feb 19 '21 at 22:20
  • A simple approach would be to just pass the string to *Date.parse* and reflect the result back to the user in an unambiguous format, say d mmm yyyy h:mm or similar and let the user find a format that returns what they expect. – RobG Feb 19 '21 at 22:24

0 Answers0