Beginner Level
Intermediate Level
Advanced Level
Introduction
Understanding how to work with datetime in Python is essential for a wide range of applications, from simple scripts to complex data analysis and web development projects. In this tutorial, we will explore how to use Python's built-in modules to determine the current date and time, as well as manipulate and format that data to meet our specific needs. Whether you are a beginner learning Python for the first time or an experienced developer looking to improve your skills, this tutorial is a valuable resource for anyone working with date and time data in Python.
Table of Contents :
- Different ways of getting current datetime in python
- get current datetime using datetime Object
- get current UTC date-time using datetime object
- get current date-time in ISO format using datetime object
- get datetime attributes using datetime Object
- How to get current date using datetime object
- How to get timezone using datetime object
Different ways of getting current date-time in python
- We have already read about the datetime module in detail.
- The datetime module provides datetime objects, which are used to work with date and time values.
- The most important class in the datetime module is
datetime.datetime
, which represents a date and time together. - We'll learn how to get current time values in our next tutorial.
- In this section we'll learn how to use the
datetime
object to get -- current date-time
- current UTC date-time
- current date-time in ISO format
- date-time attributes
How to get current date-time using datetime object
- The
datetime
object can be used to get the current date-time together. - The
now()
function of datetime object returns the current date-time. - Code Sample :
from datetime import datetime
now = datetime.now()
print("Current date and time:", now)
# Output
# Current date and time: 2022-02-09 18:51:44.089391
How to get current UTC date-time using datetime object
- UTC stands for Coordinated Universal Time.
- It is the primary time standard by which the world regulates clocks and time.
- We can use the
today()
function ofdatetime
object to get the current UTC date. - This function returns a
datetime.date
object. - Code Sample :
from datetime import datetime
dt = datetime.now()
# Get the current date
current_utc_date = dt.utcnow()
print("Current UTC Date : ", current_utc_date)
# Output
# Current UTC Date : 2022-02-09 13:38:00.788949
How to get current date-time in ISO format using datetime object
- The
isoformat()
method of thedatetime
class can be used to get the current date and time in ISO format. - Example code:
from datetime import datetime
now = datetime.now()
iso_time = now.isoformat()
print("Current date and time in ISO format:", iso_time)
# Output
# Current date and time in ISO format: 2022-08-14T11:20:20.951170
How to get date-time attributes using datetime object
- The
datetime
class provides different attributes to access year, month, day, hour, minute, second, and microsecond values separately. - Code Sample :
from datetime import datetime
now = datetime.now()
print("Year:", now.year)
print("Month:", now.month)
print("Day:", now.day)
print("Hour:", now.hour)
print("Minute:", now.minute)
print("Second:", now.second)
print("Microsecond:", now.microsecond)
# Output
# Year: 2022
# Month: 2
# Day: 9
# Hour: 18
# Minute: 51
# Second: 44
# Microsecond: 089391
How to get current date using datetime object
- We can use the
date()
function ofdatetime
object to get the current date. - This function returns a
datetime.datetime.date
object. - Code Sample :
from datetime import datetime
# Get datetime object
dt = datetime.now()
# Get the current date
current_date = dt.date()
print("Current Date : ", current_date)
# Output
# Current Date : 2022-02-09
How to get timezone using datetime object
- The
pytz
module provides timezone support in Python. - Here's how we can get the current date-time in a particular timezone.
- Code Sample :
from datetime import datetime
import pytz
tz = pytz.timezone('US/Eastern')
now = datetime.now(tz)
print("Current date and time in US/Eastern:", now)
# Output
# Current date and time in US/Eastern: 2022-03-07 12:18:57.820845-05:00
Prev. Tutorial : dates and time
Next Tutorial : Get current time