Guest User

Untitled

a guest
Jan 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. import datetime
  2.  
  3.  
  4. def format_time(date):
  5. d_f = date.count('-')
  6. t_f = date.count(':')
  7. if d_f == 2:
  8. date_format = "%Y-%m-%d"
  9. elif d_f == 1:
  10. date_format = "%m-%d"
  11. else:
  12. date_format = ''
  13.  
  14. if t_f == 2:
  15. time_format = '%H:%M:%s'
  16. elif t_f == 1:
  17. time_format = '%H:%M'
  18. else:
  19. time_format = ''
  20. format_string = " ".join([date_format, time_format]).rstrip()
  21. return datetime.datetime.strptime(date, format_string)
  22.  
  23.  
  24. print(format_time("2018-12-11"))
  25. print(format_time("12-11"))
  26. print(format_time("12-11 02:10"))
Add Comment
Please, Sign In to add comment