Python DateTime Module
Unlike many other programming languages, Python does not have a default date object. Instead, it has a separate module for manipulating date and time. This module is called datetime.
The DateTime module has several different functions that can be used to work with date and time. These functions generally return date, time, or both in different formats.
Table of Content:
- Types of Date and time objects
- Datetime module constants
- Using DateTime in python
- Get Current local date and time.
- Get Current local Date
- Get current year, month, day
- Converting timestamp to date and time
- Create a time object
- Create a Date object
- Formatting Date and time
Types of Date and time objects
In python data and time objects can be categorized as “aware” or “naive”.
Aware Datetime object
If the date-time object includes information about the time zone, daylight saving, or geographical time adjustment it is called “aware” DateTime as it is not open to interpratation and is aligned with the geographical timezones. It has an additional tzinfo attribute
Naive Datetime objects
These type of datetime objects are open to interpretation as they do not include time zone information. Naive datetime is specific to the program and are mear number which lack context and standard time units.
Datetime module Constants
Datetime module exports 2 constans
- datetime.MINYEAR ~ smallest year allowed by datetime module
- datetime.MAXYEAR ~ largest year allowed by datetime module
Using datetime in python
Like any other module in python you can import datetime module by –
To import only specific function of datetime module use-
from datetime import date
The datetime module has many classes such as “date”,”time”, “datetime”, “timezone”, “tzinfo” etc.
You can see the list of available classes using the dir function.
Get current date and time
use datetime.datetime.now() to get current local date and time.
Why datetime.datetime ??
the first datetime refers to the module and the later datetime refers to the “datetime” class of the “datetime module”
import datetime datetime = datetime.datetime.now() print(datetime)
Get Current date
To get the current date, we use the today() function of the datetime module defined in the date class.
from datetime import date date = date.today() print(date)
Get Year, Month, Day
The today() function of the date class can also be used to get the year, month, or day separately.
from datetime import date year = date.today().year month = date.today().month day = date.today().day print(year) print(month) print(day)
Timestamp(Unix Epoch) to Human readable date
We can also convert a timestamp into a date using the timestamp() provided by the date class.
from datetime import date date = date.timestamp(1604938680) print(date)
Creating a time object
The time class of the datetime module can be used to represent/ create a time object.
from datetime import time time = time(10, 44, 12, 234222) print(time)
The arguments are in this order – hour, minutes, seconds, and milliseconds. (The fourth argument is optional)
Creating a Date Object
The date class can be used to create date objects.
import datetime x = datetime.datetime(2020, 9, 12) print(x)
Formatting date and time
Date and time can be written in different formats such as MM-DD-YYYY or DD-MM-YYYY etc. or HH:MM:SS or MM:SS:HH
To format a date returned by the now() function of datetime class, we can use the strftime() function.
from datetime import datetime datetime = datetime.datetime.now().strftime("%H:%M:%S") print(datetime)
To Learn more in depth about datetime module read date time documentation.
Comment below if you want me to cover time zone datetime.