print current datetime in python code example
Example 1: python print time
import datetime
now = datetime.datetime.now()
print (now.strftime("%Y-%m-%d %H:%M:%S"))
Example 2: python current date
from datetime import date
today = date.today()
print("Today's date:", today)
Example 3: python get the current date
import datetime
date_time = datetime.datetime.now()
print(date_time)
--> 2020-10-03 15:29:54.822751
date_time.strftime("%d/%m/%Y")
--> '03/10/2020'
date_time.strftime("%m/%d/%y")
--> '10/03/20'
date_time.strftime("%Y/%m/%d")
--> '2020/10/03'
date_time.strftime("%Y-%m-%d")
--> '2020-10-03'
date_time.strftime("%B %d, %Y")
--> 'October 03, 2020'
Directive Description Example
%a Weekday, short version Wed
%A Weekday, full version Wednesday
%w Weekday as a number 0-6, 0 is Sunday 3
%d Day of month 01-31 31
%b Month name, short version Dec
%B Month name, full version December
%m Month as a number 01-12 12
%y Year, short version, without century 18
%Y Year, full version 2018
%H Hour 00-23 17
%I Hour 00-12 05
%p AM/PM PM
%M Minute 00-59 41
%S Second 00-59 08
%f Microsecond 000000-999999 548513
%z UTC offset +0100
%Z Timezone CST
%j Day number of year 001-366 365
%U Week number of year 00-53 52
%c Local version of date and time Mon Dec 31 17:41:00 2018
%x Local version of date 12/31/18
%X Local version of time 17:41:00
%% A % character %
Example 4: getting time python
import datetime
currentDT = datetime.datetime.now()
print(str(currentDT))
import datetime
currentDT = datetime.datetime.now()
print ("Current Year is: %d" % currentDT.year)
print ("Current Month is: %d" % currentDT.month)
print ("Current Day is: %d" % currentDT.day)
print ("Current Hour is: %d" % currentDT.hour)
print ("Current Minute is: %d" % currentDT.minute)
print ("Current Second is: %d" % currentDT.second)
print ("Current Microsecond is: %d" % currentDT.microsecond)
"""
Current Year is: XXXX
Current Month is: XX
Current Day is: XX
Current Hour is: XX
Current Minute is: XX
Current Second is: XX
Current Microsecond is: XXXXXX
"""
Example 5: how to print time python
import datetime
datetime.datetime.now()
datetime.datetime(2009, 1, 6, 15, 8, 24, 78915)
print(datetime.datetime.now())