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.
- The list of format codes are:
- %a: It is used for weekdays representation in short forms as wed, thr, etc...
- %A: It is used for weekdays representation in the full name as of Sunday, Monday.
- %b: It is used for month name representation in short form as of Jan, Feb, etc
- %B: It is used for month name representation in the full name as of April, May, etc.
- %d: It is used as a representation of the day of the month as zero added.
- %-d: day of the month as a decimal number.
- %m: Month as zero added decimal number.
- %-m: Month as a decimal number.
- %H: hours as zero added decimal number.
- %-H: hours as a decimal number.
- %M: minutes as zero added decimal number.
- %-M: minutes as a decimal number.
- %S: seconds as a zero added decimal number
- %-S: seconds as a decimal number.
- %J: day of the year as zero added decimal number.
- %-J: day of the year as a decimal number.
#write a python program to demonstrate strftime function.
- from datetime import datetime
- now=datetime.now()
- year=now.strftime("%Y")
- print("year:",year)
- month=now.strftime("%m")
- print("month:",month)
- day=now.strftime("%d")
- print("day:",day)
- time=now.strftime("%H:%M:%S")
- print("time:",time)
- date_time=now.strftime("%m%d%Y, %H%M%S")
- print("date and time:",date_time)
year:2021
month:07
day:05
time:10:50:36
date and time:07052021,105036
No comments