xxxxxxxxxx
from datetime import datetime
now = datetime.now().time().strftime("%H:%M:%S") # time object
date = datetime.now().strftime("%Y-%m-%d") # date object
print("date:",date)
print("time =", now)
xxxxxxxxxx
from datetime import datetime
now = datetime.now().time().strftime("%H:%M:%S") # time object
date = datetime.now().strftime("%Y-%m-%d") # date object
print("date:",date)
print("time =", now)
xxxxxxxxxx
from datetime import datetime
# datetime object containing current date and time
now = datetime.now()
print("now =", now)
#Output: now = 2021-06-25 07:58:56.550604
# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)
#Output: date and time = 25/06/2021 07:58:56
xxxxxxxxxx
import datetime
print(datetime.datetime.now())
2021-11-13 23:30:38.419951
print(datetime.date.today())
2021-11-13
xxxxxxxxxx
date_and_time = datetime.now()
print("The today current date and time is:- ",date_and_time)
xxxxxxxxxx
from datetime import datetime
# datetime object containing current date and time
now = datetime.now()
print("now =", now)
xxxxxxxxxx
# Python3 code to demonstrate
# attributes of now()
# importing datetime module for now()
import datetime
# using now() to get current time
current_time = datetime.datetime.now()
# Printing attributes of now().
print("The attributes of now() are :")
print("Year :", current_time.year)
print("Month : ", current_time.month)
print("Day : ", current_time.day)
print("Hour : ", current_time.hour)
print("Minute : ", current_time.minute)
print("Second :", current_time.second)
print("Microsecond :", current_time.microsecond)
xxxxxxxxxx
import time
time.time()
# In The OutPut It will show no of secs. For EXAMPLE:- Mine is 1668486863.7566664
print(time.ctime(1668486863.7566664))
# Then the Date and Time will seen in Output:- Tue Nov 15 10:04:23 2022
xxxxxxxxxx
import time
# Getting time and date
local_time = time.localtime(time.time())
print(f"local current time: {local_time}")
# Getting formatted time and date
local_time = time.asctime(time.localtime(time.time()))
print(f"local current time: {local_time}")