Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

strftime python

 strftime :

  •  The method represents the string consists of data and time with objects as time and date or date time.
  • The datetime class is imported from the date time module.
  • It takes one or more input format codes and returns string representation.
Syntax: strftime(format)
  • The list of format codes are:
  1. %a:  It is used for weekdays representation in short forms as wed, thr, etc...
  2. %A: It is used for weekdays representation in the full name as of Sunday, Monday.
  3. %b: It is used for month name representation in short form as of Jan, Feb, etc
  4. %B: It is used for month name representation in the full name as of April, May, etc.
  5. %d: It is used as a representation of the day of the month as zero added.
  6.  %-d: day of the month as a decimal number.
  7. %m: Month as zero added decimal number. 
  8. %-m: Month as a decimal number.
  9. %H:  hours as zero added decimal number.
  10. %-H: hours as a decimal number.
  11. %M: minutes as zero added decimal number.
  12. %-M: minutes as a decimal number.
  13. %S: seconds as a zero added decimal number
  14. %-S: seconds as a decimal number.
  15. %J: day of the year as zero added decimal number.
  16. %-J: day of the year as a decimal number.

#write a python program to demonstrate strftime function.

  1. from datetime import datetime
  2. now=datetime.now()
  3. year=now.strftime("%Y")
  4. print("year:",year)
  5. month=now.strftime("%m")
  6. print("month:",month)
  7. day=now.strftime("%d")
  8. print("day:",day)
  9. time=now.strftime("%H:%M:%S")
  10. print("time:",time)
  11. date_time=now.strftime("%m%d%Y, %H%M%S")
  12. print("date and time:",date_time)
output:
year:2021
month:07
day:05
time:10:50:36
date and time:07052021,105036





This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

strftime python

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×