Date Time Functions in Python


Python datetime module is provided in Python to work with dates and times. 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. These classes provide a number of functions to deal with dates, times and time intervals.

Here are some examples of common date and time-related functions in Python:

  • datetime.now(): Returns the current date and time.
  • datetime.date(): Returns date object of today's date.
  • datetime.time(): Returns the current time.
  • datetime.datetime(): Returns the current date and time as a datetime object.
  • datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0): Represents the difference between two date or time values.

The strftime() function is used to convert date and time objects to their string representation.
      Syntax :
           strftime ( format )

List of format codes

DirectiveDescriptionExample
%AWeekday full name Monday
%aWeekday short name Mon
%dDay of month (1-31)26
%bMonth of short nameDec
%BMonth of full name December
%YYear of full version , without century 2022
%yYear of short version22
%wWeekday as a number ( 0-Sun , 1-Mon , 2-Tue , 3-Wed , 4-Thu , 5-Fri , 6-Sat )1(Monday)
%WWeek number of Year ( Monday as the first day of week ( 00-53 ) ) 48
%mMonth as a Number ( 1(Jun) - 12(Dec) )12(Dec)
%HHours ( 00-23 ) 15
%MMinute ( 00-59 ) 50
%SSecond ( 00-59 ) 23
%pPM / AM PM
%cLocal version of date and time Mon Dec 26 15 : 50 : 23 2022
%XLocal version of time 15:50:23
%xLocal version of date 12/26/22


Source Code

# Date Time in Python
import datetime as dt
 
current_date = dt.date.today()
print("Current Date : ", current_date)
 
new = dt.date(2021, 10, 25)
print(new)
print("Year : ", new.year)
print("Month : ", new.month)
print("Day : ", new.day)
print("--------------------------------------")
a = dt.time(10, 45, 5, 555505)
print(a)
print("Hour : ", a.hour)
print("minute : ", a.minute)
print("second : ", a.second)
print("microsecond : ", a.microsecond)
print("--------------------------------------")
current_time = dt.datetime.now()
print("Current Time : ", current_time)
print("--------------------------------------")
new = dt.datetime(2021, 5, 31, 12, 2, 10)
print(new)
print(new.date())
print(new.time())
print("--------------------------------------")
current = dt.datetime.now()
new_year = dt.datetime(2022, 1, 1)
difference = new_year - current
print(difference)
print("--------------------------------------")
current = dt.datetime.now()
print(current)
s=current.strftime("%A %b %d %Y")
print(s)
To download raw file Click Here

Output

Current Date :  2022-03-25
2021-10-25
Year :  2021
Month :  10
Day :  25
--------------------------------------
10:45:05.555505
Hour :  10
minute :  45
second :  5
microsecond :  555505
--------------------------------------
Current Time :  2022-03-25 12:12:56.376654
--------------------------------------
2021-05-31 12:02:10
2021-05-31
12:02:10
--------------------------------------
-84 days, 11:47:03.621624
--------------------------------------
2022-03-25 12:12:56.378856
Friday Mar 25 2022

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial