SoFunction
Updated on 2025-03-02

Some common operations on Python time and date

Preface

In Python, the most common module we use to deal with time and date related types isdatetimeModule. This module provides many classes related to time and dates, which makes it very convenient for us to deal with time and dates.

Here are some common operations about time and date.

1. Datetime class

1. Get the current date and time (year, month, day, hour, minute, second, microsecond)

from datetime import datetime

today = ()
now = ()
print("The current date and time are:", today)  # The current date and time are: 2024-07-29 21:05:42.281563print("The current date and time are:", now)  # The current date and time are: 2024-07-29 21:05:42.281563

2. Output the specified date

specific_date = datetime(2024, 7, 29)
specific_date1 = datetime(2024, 7, 30, 21, 55, 00)
print("The specified date is:", specific_date)  # The specified date is: 2024-07-29 00:00:00print("The specified date is:", specific_date1)  # The specified date is: 2024-07-30 21:55:00

3. Calculate the time difference

# Subtracting two dates will result in a timedeltadelta = specific_date1 - specific_date
print(delta, type(delta))  # 1 day, 21:55:00 <class ''>
# Get the number of days and seconds between the difference between two datesprint(, )  # 1 78900

4. Access the properties of the datetime object

# Get the time, month, day, hour, minute, and second separately through the datetime object's attributesyear = 
month = 
day = 
hour = 
minute = 
second = 
print(f"Year: {year}, moon: {month}, day: {day}, hour: {hour}, point: {minute}, Second: {second}")
# Output->Year: 2024, moon: 7, day: 29, hour: 21, point: 08, Second: 40

5. Format time

# Format the time objectformatted_datetime = ('%Y year %m month %d day %H hour %M minute %S seconds')
print("Format time:", formatted_datetime)  # 2024Year07moon29day 21hour08point19Second

2. Date class

dateClasses are generally used to process dates (years, months, and days).

1. Get the current date (year, month, day) and attributes

from datetime import date

today1 = ()
year = 
month = 
day = 
print(today1)  # 2024-07-29
print(f"Year: {year}, moon: {month}, day: {day}")  # Year: 2024, moon: 7, day: 29

3. Time class

timeClasses are mainly used to process time (hours, minutes, seconds, microseconds).

1. Specify time

from datetime import time

current_time = time(15, 48, 6)  # Assume that the current time is 15:48:6 secondsprint("Current time:", current_time)  # Current time: 15:48:06

2. Get time, minute, second and microseconds by accessing the time attribute

precise_time = time(15, 48, 6, 123456)
print("Precise time:", precise_time)
hour = current_time.hour
minute = current_time.minute
second = current_time.second
microsecond = precise_time.microsecond
print(f"hour: {hour}, point: {minute}, Second: {second}, 微Second: {microsecond}")  # hour: 15, point: 48, Second: 6, 微Second: 123456

4. Timedelta class

1. Calculate the past and future dates

from datetime import timedelta

# Calculate the dates for the next three daysfuture_date = now + timedelta(days=3)
print("Date after three days:", future_date)  # Date after three days: 2024-08-01 21:16:26.496122
# Calculate the time of the past hourpast_time = now - timedelta(hours=1)
print("Previous hour:", past_time)  # past1Hours:2024-07-28 20:16:26.496122

2. Create a timedelta object using multiple parameters

delta = timedelta(weeks=1, days=1, hours=1, minutes=1, seconds=1, microseconds=1)
print("time:", delta)  # time: 8 days, 1:01:01.000001

Summarize

This is the end of this article about some common operating methods for Python time and date. For more related Python time and date operation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!