Python has a module named “datetime” to work with dates and times.
Get Current(System) Date and Time
Python script to display system date and time
Example:
#to display date and time import datetime dt = datetime.datetime.now() print("date time is ",dt) print(dt)
Output:
date time is 2019-11-10 04:52:16.516284
2019-11-10 04:52:16.516284
>>>
To display only date
In this program, we have used today() method defined in the date class to get a date object containing the current local date.
Example:
import datetime dt = datetime.date.today() print(dt)
Output:
2019-11-10
>>>
Example:
from datetime import date today = date.today() print("Current date =", today)
Output:
Current date = 2019-11-10
>>>
To display day , month and year parts of the date
Print today’s year, month and day
We can get year, month, day, day of the week etc. from the date object easily.
Example:
from datetime import date #date object of today's date today = date.today() print("Current year:", today.year) print("Current month:", today.month) print("Current day:", today.day)
Output:
Current year: 2019
Current month: 11
Current day: 10
>>>
To display hour, min and sec parts of time
We can also create date objects from a timestamp. A Unix timestamp is the number of seconds between a particular date and January 1, 1970.
Example:
from datetime import datetime a = datetime.now() #to display hour,min and sec print("hour =", a.hour) print("minute =", a.minute) print("timestamp =", a.timestamp())
Output:
hour = 4
minute = 57
timestamp = 1573342028.110114
>>>
To display day, month, year, hour , min and second
Example:
from datetime import datetime a = datetime.now() #to display day,month and year print("year =", a.year) print("month =", a.month) print("day =", a.day) to display hour,min and sec print("hour =", a.hour) print("minute =", a.minute) print("timestamp =", a.timestamp())
Output:
year = 2019
month = 11
day = 10
hour = 4
minute = 58
timestamp = 1573342105.516302
>>>
To display date in a formmatted manner
Example:
from datetime import datetime a = datetime.now() #to display day,month and year print("dd/mm/yyyy") print(a.day,"/",a.month,"/",a.year) print("mm/dd/yyyy") print(a.month,"/",a.day,"/",a.year) #to display hour,min and sec print("hh:mm:ss") print(a.hour,":",a.minute,":",a.second) print("timestamp =", a.timestamp())
Output:
dd/mm/yyyy
10 / 11 / 2019
mm/dd/yyyy
11 / 10 / 2019
hh:mm:ss
5 : 46 : 9
timestamp = 1573344969.664423
>>>
Example : Get date from a timestamp
You can convert a timestamp to date using fromtimestamp() method.
from datetime import datetime from datetime import date a = datetime.now() print("timestamp =", a.timestamp()) ts=a.timestamp() d1=date.fromtimestamp(ts) print(ts) print(d1) print(d1.day," ",d1.month," ",d1.year)
Output:
timestamp = 1573345238.680002
1573345238.680002
2019-11-10
10 11 2019
>>>
Python format datetime
The date and time is represented differently in different places, organizations etc. It’s more common to use mm/dd/yyyy in the US, whereas dd/mm/yyyy is more common in the UK.
Python has strftime() and strptime() methods to handle this.
Python strftime() – datetime object to string
The strftime() method is defined under classes date, datetime and time. The method creates a formatted string from a given date, datetime or time object.
Here we use %Y, %m, %d, %H format codes.
The strftime() method takes one or more format codes and returns a formatted string based on it.
In the above program, t, s1 and s2 are strings.
• %Y – year [0001,…, 2018, 2019,…, 9999]
• %m – month [01, 02, …, 11, 12]
• %d – day [01, 02, …, 30, 31]
• %H – hour [00, 01, …, 22, 23
• %M – month [00, 01, …, 58, 59]
• %S – second [00, 01, …, 58, 59]
Example:
#Example : Format date using strftime() from datetime import datetime #current date and time now = datetime.now() t = now.strftime("%H:%M:%S") print("time:", t) s1 = now.strftime("%m/%d/%Y, %H:%M:%S") #mm/dd/YY H:M:S format print("mm/dd/yy :", s1) s2 = now.strftime("%d/%m/%Y, %H:%M:%S") #dd/mm/YY H:M:S format print("dd/mm/yy : ", s2)
Output:
time: 05:54:37
mm/dd/yy : 11/10/2019, 05:54:37
dd/mm/yy : 10/11/2019, 05:54:37
>>>