DateTime — Basic date and time types in python

Nkugwa Mark William
2 min readMar 10, 2023

--

Introduction: Date and time handling is a crucial aspect of any programming language, including Python. In Python, the datetime module provides classes for working with dates and times. It is a built-in module that provides various classes and methods to work with dates and times. This module is highly used in various data-driven industries, including finance, healthcare, and research.

Basic Date and Time Types: The datetime module provides the following basic date and time types:

  • date: It represents the date with the attributes year, month, and day.
  • time: It represents the time of day with attributes hour, minute, second, microsecond, and tzinfo.
  • datetime: It is a combination of a date and time. It has all the attributes of both date and time.
  • timedelta: It represents a duration, the difference between two dates or times.

Example Code:

import datetime

# Get the current date and time
current_time = datetime.datetime.now()

# Print the current date and time
print("Current Date and Time:", current_time)

# Get the current date
current_date = datetime.date.today()

# Print the current date
print("Current Date:", current_date)

# Get the current time
current_time = datetime.datetime.now().time()

# Print the current time
print("Current Time:", current_time)

# Create a datetime object
my_date = datetime.datetime(2022, 12, 25, 10, 30, 00)

# Print the datetime object
print("My Date:", my_date)

# Get the year, month, day, hour, minute, and second from a datetime object
print("Year:", my_date.year)
print("Month:", my_date.month)
print("Day:", my_date.day)
print("Hour:", my_date.hour)
print("Minute:", my_date.minute)
print("Second:", my_date.second)

# Create a timedelta object
my_timedelta = datetime.timedelta(days=7)

# Print the timedelta object
print("My Timedelta:", my_timedelta)

# Add the timedelta to a date
new_date = current_date + my_timedelta

# Print the new date
print("New Date:", new_date)

Explanation:

  • The datetime.datetime.now() method is used to get the current date and time. The datetime.datetime class is used to create a datetime object.
  • The datetime.date.today() method is used to get the current date. The datetime.date class is used to create a date object.
  • The datetime.datetime.now().time() method is used to get the current time. The time() method is used to extract the time object from the datetime object.
  • The datetime.datetime(year, month, day, hour, minute, second) method is used to create a datetime object with the specified values.
  • The attributes of a datetime object can be accessed using dot notation. For example, my_date.year is used to get the year attribute of the my_date object.
  • The datetime.timedelta(days, seconds, microseconds) method is used to create a timedelta object with the specified values.
  • The + operator can be used to add a timedelta object to a date object.

Conclusion: The datetime module provides powerful classes and methods to work with dates and times in Python. These classes and methods can be used to perform various operations such as creating, manipulating, and formatting dates and times. Understanding the basics of the datetime module is essential for any Python developer who needs to work with date and time data.

--

--

Nkugwa Mark William
Nkugwa Mark William

Written by Nkugwa Mark William

Nkugwa Mark William is a Chemical and Process engineer , entrepreneur, software engineer and a technologists with Apps on google play store and e commerce sites

No responses yet