SoFunction
Updated on 2025-03-01

About the difference and usage of time and datetime in python

1. Two ways to represent time in Python:

  • Timestamp: Offset calculated in seconds relative to 1970.1.1 00:00:00, unique
  • Time tuple struct_time: There are 9 elements in total > tm_year: year 1-12> tm_mon: month 1-12> tm_mday: day 1-31> tm_hour: time 0-23> tm_min: minute 0-59> tm_sec: second 0-59> tm_wday: week 0-6 (0 means Sunday)> tm_day: what day of the year 1-366> tm_isdst: whether it is summer vacation, default is -1

2. time

 1. () #Current timestamp, no parameters, unique value 2. (secs) #Pause for a few seconds 3. (secs)  #The current Greenwich time is obtained without a parameter, and the time stamp parameter is converted to Greenwich structure time 4. (secs)  #Get local structured time without parameters, and convert the parameter to local structured time if there is a time stamp           print(())  # time.struct_time(tm_year=2022, tm_mon=10, tm_mday=20, tm_hour=11, tm_min=20, tm_sec=44, tm_wday=3, tm_yday=293, tm_isdst=0)
           print((13912345678))  # time.struct_time(tm_year=2410, tm_mon=11, tm_mday=12, tm_hour=20, tm_min=27, tm_sec=58, tm_wday=4, tm_yday=316, tm_isdst=0)
 5. (format, t) #Convert structured time into custom formatted time           print(('%Y-%m-%d %H:%M:%S', ()))  #Beijing Time           print(('%Y-%m-%d %H:%M:%S', (()+60*60)))  #Tokyo time 6. (string, format) # Convert custom time (string) to structure time           print(("2023-1-11 1:2:3", "%Y-%m-%d %H:%M:%S")) # Convert daily formatted time to structured time           print(("2023-1-11", "%Y-%m-%d")) # Convert daily formatted time to structured time 7. (secs)  # Convert a timestamp to a structured time           print((123))  # Thu Jan  1 08:02:03 1970
           print((() + 60 * 60))  # Thu Oct 20 11:28:32 2023
 8. (t) # Convert the given structured time into a time stamp           print(((())))  # 1666233100.0
           print(())  # 1666233100.5075898 # The effect of the two lines is the same           # Find a time stamp for a specified time           print((('2023-2-3', '%Y-%m-%d'))) # 1580659200.0 

3. datetime

datetime is much more advanced than time. It can be understood that datetime is encapsulated based on time and provides more practical functions, mainly including the following categories:

  • timedelta: mainly used to calculate time span
  • time: only focus on time
  • date: only focus on date
  • datetime: There are both time and date

In actual use, the most commonly used is the sequential one, and the other two are not much different from the actual use.

After instantiation, the following properties and common methods will be mainly used:

time_now = ()  #Instantiation







():Time zone
():returndateObject
():returntimeObject
(name=value)
():returntime.struct_time Object
(format):according toformatFormat output
#In addition to the methods that the instance itself has, the class itself also provides many useful methods:
(date_string,format): Parse strings for a given time format
([tz]):Current time default localtime
():Current time

(3) , advance 3 days
(-3), retreat for 3 days
(hour = 3), advance 3 hours
(minutes = 3) , advance 3 minutes

import time
import datetime

dt = ()

print(())
print()
print()
print()
print()
print()
print()
print()
print()
print(())
print(())
print(("%Y-%m-%d %H:%M:%S"))  # 2023-1-31 13:42:16
print(('2023-1-20 1:1:1', "%Y-%m-%d %H:%M:%S"))

delta = (11)

print(dt + delta)
print((dt + delta).strftime("%Y-%m-%d %H:%M:%S"))
print((dt - delta).strftime("%Y-%m-%d %H:%M:%S"))

print('....')
print(()) #Return 2023-1-20 13:40:29.151057print((()) )  # Convert the timestamp directly to date format 2023-1-20print(())
print(() + (3)) #Current time + 3 daysprint(() + (-3)) #Current time-3 daysprint(() + (hours=3)) #Current time + 3 hoursprint(() + (minutes=30)) #Current time +30 minutes
print((()))  # 2023-1-20

This is the end of this article about the difference and usage of time and datetime in python. For more information about the differences and usage of time and datetime, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!