How to get part of string and pass it to other function in python?

前端 未结 4 432
一整个雨季
一整个雨季 2021-01-28 06:58

My task is to verify whether string is valid date and time or not.

\"2013-01-01W23:01:01\"

The date and the time are separated with

4条回答
  •  心在旅途
    2021-01-28 07:20

    If all you need to do is to check whether the string in the format you specified is a valid datetime or not, no need to split it and pass values to some handmade functions, just use this:

    from datetime import datetime
    
    try:
        valid_datetime = datetime.strptime('2013-01-01W23:01:01', '%Y-%m-%dW%H:%M:%S')
        print 'this datetime is valid'
    except ValueError:
        print 'this datetime is invalid'
    

提交回复
热议问题