PYTHON : Date Time, Strftime
HOW TO USE PYTHON : date time, Strftime
Date : 30/05/2019
INTRODUCTION
Python date time provides a number of function to deal with date and time .
Before you run the code for datetime, it is important that you import the date time modules.
IMPORT DATE TIME
import date function from date time.
=> from datetime import date
import time function from date time.
=> from datetime import time
import datetime from datetime.
=> from datetime import datetime
PRINT DATE AND TIME USING datetime
from datetime import date from datetime import time from datetime import datetime def main(): #print today date todaydate=datetime.now() print (todaydate) # Get the current time t = datetime.time(datetime.now()) print "The current time is", t wd=date.weekday(todaydate) days= ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"] print("Today is day number %d" % wd) print("which is a " + days[wd]) if __name__== "__main__": main()
OUTPUT:
Format Date and Time with Strftime()
As of now we have learned, how to use datetime and date object in Python.
We will advance a step further and learn how to use a formatting function to format Time and Date.
from datetime import datetime def main(): now= datetime.now() #get the current date and time print(now.strftime("%c")) print(now.strftime("%x")) print(now.strftime("%X")) #%I/%H - 12/24 Hour, %M - minute, %S - second, %p - local's AM/PM print(now.strftime("%I:%M:%S %p")) # 12-Hour:Minute:Second:AM print(now.strftime("%H:%M")) # 24-Hour:Minute if __name__== "__main__": main()
%c – local date and time
%x-local’s date
%X- local’s time
OUTPUT:
Thanks for using pheonix solutions.
You find this tutorial helpful? Share with your friends to keep it alive.