Python program to print the current date and time
Python Dates
A date in Python is not a data type of its own, but we can import a module named datetime
to work with dates as date objects.
example :-
import datetime
x = datetime.datetime.now()
print(x)
The strftime() Method
The datetime
object has a method for formatting date objects into readable strings.
The method is called strftime()
, and takes one parameter, format
, to specify the format of the returned string:
Python program to display the current date and time
import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))
output:-
Current date and time :
2022-01-03 13:46:50
>>>
Get the current date and time
from datetime import datetime
now = datetime.now()
print("now =", now)
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)
output:-now = 2022-01-03 13:50:59.575978
date and time = 03/01/2022 13:50:59
>>>
Python get current date without time
import datetime
now = datetime.date.today()
currentDate = datetime.datetime.strptime('01/08/2015','%d/%m/%Y').date()
print(currentDate)
output:-
2022-01-03
>>>
Python get current time in seconds
import time
milliseconds = int(round(time.time() * 1000))
print(milliseconds)
output:-
1641198498286
>>>
Post a Comment
If you have any doubts, Please let me know
Thanks!