Back to all posts

DateTime in Python

In Python, the datetime module provides classes for manipulating dates and times. It offers several functions and methods to create, manipulate, and format…

In Python, the datetime module provides classes for manipulating dates and times. It offers several functions and methods to create, manipulate, and format date and time objects.

C++
import datetime

Classes in the datetime Module

  1. datetime.date: Represents a date (year, month, day).
  2. datetime.time: Represents a time (hour, minute, second, microsecond).
  3. datetime.datetime: Represents both date and time.
  4. datetime.timedelta: Represents the difference between two dates or times.
  5. datetime.tzinfo: An abstract base class for dealing with time zones.

Creating Date and Time Objects

PHP
# Creating a date object
date_obj = datetime.date(2023, 6, 4)
print(date_obj)  # Output: 2023-06-04

# Creating a time object
time_obj = datetime.time(14, 30, 45)
print(time_obj)  # Output: 14:30:45

# Creating a datetime object
datetime_obj = datetime.datetime(2023, 6, 4, 14, 30, 45)
print(datetime_obj)  # Output: 2023-06-04 14:30:45

Methods and Functions

SQL
#  today(), now(), and utcnow() Methods

# Current local date
current_date = datetime.date.today()
print(current_date)  # Output: current date in YYYY-MM-DD

# Current local date and time
current_datetime = datetime.datetime.now()
print(current_datetime)  # Output: current date and time in YYYY-MM-DD HH:MM:SS

# Current UTC date and time
current_utc_datetime = datetime.datetime.utcnow()
print(current_utc_datetime)  # Output: current UTC date and time in YYYY-MM-DD HH:MM:SS

PHP
# Creating a timedelta object
delta = datetime.timedelta(days=10, hours=5, minutes=30)
print(delta)  # Output: 10 days, 5:30:00

# Adding timedelta to a datetime object
future_date = datetime.datetime.now() + delta
print(future_date)  # Output: current date and time + 10 days, 5 hours, 30 minutes
PHP
# Formatting a datetime object to a string
formatted_date = datetime_obj.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_date)  # Output: 2023-06-04 14:30:45

# Parsing a string to a datetime object
parsed_date = datetime.datetime.strptime("2023-06-04 14:30:45", "%Y-%m-%d %H:%M:%S")
print(parsed_date)  # Output: 2023-06-04 14:30:45

Method NameDescriptionExampleOutput
datetime.date()Creates a date object.datetime.date(2023, 6, 4)2023-06-04
datetime.time()Creates a time object.datetime.time(14, 30, 45)14:30:45
datetime.datetime()Creates a datetime object.datetime.datetime(2023, 6, 4, 14, 30, 45)2023-06-04 14:30:45
datetime.today()Returns the current local date and time.datetime.datetime.today()Current date and time
datetime.now()Returns the current local date and time.datetime.datetime.now()Current date and time
datetime.utcnow()Returns the current UTC date and time.datetime.datetime.utcnow()Current UTC date and time
datetime.fromtimestamp()Converts a timestamp to a datetime object.datetime.datetime.fromtimestamp(1609459200)2021-01-01 00:00:00
datetime.strptime()Parses a string into a datetime object.datetime.datetime.strptime("2023-06-04 14:30:45", "%Y-%m-%d %H:%M:%S")2023-06-04 14:30:45
datetime.strftime()Formats a datetime object to a string.datetime_obj.strftime("%d/%m/%Y %H:%M:%S")04/06/2023 14:30:45
datetime.replace()Replaces parts of a datetime object.datetime_obj.replace(year=2024)2024-06-04 14:30:45
datetime.combine()Combines a date and a time into a datetime object.datetime.datetime.combine(date_part, time_part)2023-06-04 14:30:45
datetime.weekday()Returns the day of the week as an integer (0=Monday).datetime_obj.weekday()Integer (0 for Monday to 6 for Sunday)
datetime.isoweekday()Returns the day of the week as an integer (1=Monday).datetime_obj.isoweekday()Integer (1 for Monday to 7 for Sunday)
timedelta(days=10)Creates a timedelta object representing a duration.datetime.timedelta(days=10, hours=5, minutes=30)10 days, 5:30:00
datetime_obj + timedelta(days=10)Adds a timedelta to a datetime object.datetime.datetime.now() + deltaCurrent date and time + 10 days, 5 hours, 30 minutes

Keep building your data skillset

Explore more SQL, Python, analytics, and engineering tutorials.