python-different-date-time-formats.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import time
  2. import datetime
  3. print('Today: ', datetime.date.today())
  4. print("Time in seconds since the epoch: %s" %time.time())
  5. print("Current date and time: ", datetime.datetime.now())
  6. print("Or Current date and time: ", datetime.datetime.now().strftime("%y-%m-%d-%H-%M"))
  7. print("Full weekday name: ", datetime.date.today().strftime("%A"))
  8. print("Abbreviated weekday name: ", datetime.date.today().strftime("%a"))
  9. print("Full month name: ", datetime.date.today().strftime("%B"))
  10. print("Abbreviated month name: ", datetime.date.today().strftime("%b"))
  11. print("Appropriate date and time: ", datetime.date.today().strftime("%c"))
  12. print("Day of the month as a decimal number: ", datetime.date.today().strftime("%d"))
  13. print("Microsecond as a decimal number: ", datetime.date.today().strftime("%f"))
  14. print("Hour (24-hour clock) as a decimal number: ", datetime.date.today().strftime("%H"))
  15. print("Hour (12-hour clock) as a decimal number: ", datetime.date.today().strftime("%I"))
  16. print("Day of the year as a decimal number: ", datetime.date.today().strftime("%j"))
  17. print("Month as a decimal number: ", datetime.date.today().strftime("%m"))
  18. print("Minute as a decimal number: ", datetime.date.today().strftime("%M"))
  19. print("Either AM or PM: ", datetime.date.today().strftime("%p"))
  20. print("Second as a decimal number: ", datetime.date.today().strftime("%S"))
  21. print("Week number of the year: ", datetime.date.today().strftime("%U"))
  22. print("Weekday as a decimal number: ", datetime.date.today().strftime("%w"))
  23. print("Week number of the year: ", datetime.date.today().strftime("%W"))
  24. print("Appropriate date representation: ", datetime.date.today().strftime("%x"))
  25. print("Appropriate time representation: ", datetime.date.today().strftime("%X"))
  26. print("Year without century as a decimal number: ", datetime.date.today().strftime("%y"))
  27. print("Year with century as a decimal number: ", datetime.date.today().strftime("%Y"))
  28. print("UTC offset in the form ±HHMM[SS[.ffffff]]: ", datetime.date.today().strftime("%z"))
  29. print("Time zone name (empty string if the object is naive): ", datetime.date.today().strftime("%Z"))
  30. print("A literal '%' character: ", datetime.date.today().strftime("%%"))
  31. print("ISO 8601 year with century: ", datetime.date.today().strftime("%G"))
  32. print("ISO 8601 weekday as a decimal number: ", datetime.date.today().strftime("%U"))
  33. print("ISO 8601 week as a decimal number: ", datetime.date.today().strftime("%V"))
  34. print("Combine directives to form date and time: ", datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S%z %p"))