python
  1. python-date-and-time

Python Date and Time

Python has a built-in module called datetime that provides various classes to work with date and time. The datetime module contains classes for working with dates, times, time intervals, and more.

Syntax

import datetime

# get current date and time
now = datetime.datetime.now()

# create a datetime object
dt = datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0)

# format datetime object
dt.strftime(format)

# convert string to datetime object
string = "2022-09-27 14:20:30"
dt_object = datetime.datetime.strptime(string, format)

Example

import datetime

# get current date and time
now = datetime.datetime.now()
print("Current date and time:", now)

# create a datetime object
dt = datetime.datetime(2022, 9, 27, 14, 20, 30)
print("Datetime object:", dt)

# format datetime object
dt_string = dt.strftime("%d-%m-%Y %H:%M:%S")
print("Formatted datetime string:", dt_string)

# convert string to datetime object
string = "27-09-2022 14:20:30"
format = "%d-%m-%Y %H:%M:%S"
dt_object = datetime.datetime.strptime(string, format)
print("Datetime object from string:", dt_object)

Output

Current date and time: 2022-09-27 14:20:30.123456
Datetime object: 2022-09-27 14:20:30
Formatted datetime string: 27-09-2022 14:20:30
Datetime object from string: 2022-09-27 14:20:30

Explanation

  • To work with date and time, we need to import the datetime module.
  • To get the current date and time, we can use the now() method of the datetime class.
  • To create a datetime object, we need to specify the year, month, and day as arguments, and optionally the hour, minute, second, and microsecond.
  • To format a datetime object, we can use the strftime() method, which takes a format string as argument. The format string can contain placeholders for the year, month, day, hour, minute, second, etc.
  • To convert a string to a datetime object, we can use the strptime() method, which takes the string and the format string as arguments.

Use

The datetime module allows us to perform various operations on dates and times, such as:

  • Create datetime objects representing specific dates and times.
  • Format datetime objects as strings.
  • Convert strings to datetime objects.
  • Calculate the difference between two datetime objects.
  • Perform arithmetic operations on datetime objects, such as adding or subtracting time intervals.

Important Points

  • The datetime module is part of the Python standard library and does not require installation.
  • The datetime class is immutable, meaning that once a datetime object is created, its value cannot be changed.
  • The strftime() and strptime() methods use a format string that specifies the format of the date and time string. The format string contains format codes that are replaced with the corresponding values when formatting or parsing a datetime object.

Summary

In this article, we learned about the datetime module in Python, which provides classes to work with dates and times. We saw examples of how to create datetime objects, format datetime objects as strings, convert strings to datetime objects, and perform various operations on datetime objects. We also learned about important points to keep in mind when working with the datetime module.

Published on: