-2

I need to find whether a datetime "string format", that is in a list, is valid. I am not trying to convert actual dates into datetime objects rather I'm checking whether a description of a datetime is valid. So for example:

YYYY-MM-DD would be valid while

YYYYY-MM-DDD does not make sense since days do not have 3 values.

So for example if I had a list of strings:

>>> x = ['abc','2020-01-01', 'YYYYMM-DDThh:mm:ss.sss', 'YYYY-MM-DD' ]
>>> check_isostring_valid(x)
[False, False, True, True]

I can't seem to find an answer since every search result seems to be how to convert actual dates to different formats while I only care about whether a representation of a possible date is valid.

The datetime library seems to use a specific format like %Y%m%d that is not the same representation that is normally used to represent date strings.

RedM
  • 268
  • 2
  • 13
  • 1
    The third one _isn't_ a valid ISO8601 format - it's not really clear what you're trying to achieve here. – jonrsharpe May 30 '22 at 13:37
  • 1
    Your question seems a school assignment. In any case: you should check how it is an ISO 8601 date-time format (check e.g. Wikipedia), and then you should write yourself a function which check every character if it is consistent with format, and then if a number is in the allowed range, and then if the numbers are consistent. Possibly with good error messages (they help you to find errors in your code: it will be "complex" because there are a lot of simple rules and it is easy to miss one, or missplace it) – Giacomo Catenazzi May 30 '22 at 13:42
  • See [How do I parse an ISO 8601-formatted date?](https://stackoverflow.com/questions/127803/how-do-i-parse-an-iso-8601-formatted-date) – martineau May 30 '22 at 14:42
  • added some clarity, the problem here is A: not a school project, B: not about doing actual conversions from strings to datetime objects. – RedM May 30 '22 at 15:47

0 Answers0