datetime string conversion


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import datetime
  2. d=datetime.datetime.now()
  3.  
  4. # datetime to string
  5. print d.strftime("%d/%m/%Y %H:%M:%S")
  6.  
  7. # string to datetime
  8. print d.strptime("14/11/1970 12:03:30","%d/%m/%Y %H:%M:%S")
  9.  
  10. """
  11. %a Locale's abbreviated weekday name.
  12. %A Locale's full weekday name.
  13. %b Locale's abbreviated month name.
  14. %B Locale's full month name.
  15. %c Locale's appropriate date and time representation.
  16. %d Day of the month as a decimal number [01,31].
  17. %H Hour (24-hour clock) as a decimal number [00,23].
  18. %I Hour (12-hour clock) as a decimal number [01,12].
  19. %j Day of the year as a decimal number [001,366].
  20. %m Month as a decimal number [01,12].
  21. %M Minute as a decimal number [00,59].
  22. %p Locale's equivalent of either AM or PM. (1)
  23. %S Second as a decimal number [00,61]. (2)
  24. %U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
  25. %w Weekday as a decimal number [0(Sunday),6].
  26. %W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
  27. %x Locale's appropriate date representation.
  28. %X Locale's appropriate time representation.
  29. %y Year without century as a decimal number [00,99].
  30. %Y Year with century as a decimal number.
  31. %Z Time zone name (no characters if no time zone exists).
  32. % A literal "%" character.
  33. """

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.