Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import datetime
- def format_time(date):
- d_f = date.count('-')
- t_f = date.count(':')
- if d_f == 2:
- date_format = "%Y-%m-%d"
- elif d_f == 1:
- date_format = "%m-%d"
- else:
- date_format = ''
- if t_f == 2:
- time_format = '%H:%M:%s'
- elif t_f == 1:
- time_format = '%H:%M'
- else:
- time_format = ''
- format_string = " ".join([date_format, time_format]).rstrip()
- return datetime.datetime.strptime(date, format_string)
- print(format_time("2018-12-11"))
- print(format_time("12-11"))
- print(format_time("12-11 02:10"))
Add Comment
Please, Sign In to add comment