SoFunction
Updated on 2024-11-20

Summary of Python's methods for handling dates and times

1 Getting Started

1.1 Getting the current time

import datetime
 
datetime_object = ()
print(datetime_object)

exports

2022-03-29 16:36:44.749582
 
Process finished with exit code 0

1.2 Getting the current date

import datetime
 
date_object = ()
print(date_object)

exports

2022-03-29
 
Process finished with exit code 0

1.3 Classes in datetime

import datetime
 
print(dir(datetime))

exports

 ['MAXYEAR', 'MINYEAR', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']
 
Process finished with exit code 0

2 Common classes in datetime

  • date class
  • time class
  • datetime class
  • timedelta class

2.1 Date class

(1) Example of a date object

import datetime
 
d = (2022, 3, 29)
print(d) 

exports

2022-03-29
 
Process finished with exit code 0

(2) Get the current date

from datetime import date
 
today = ()
 
print("current date =", today)

exports

Current date = 2022-03-29
 
Process finished with exit code 0

(3) Getting dates from timestamps

We can also create date objects from timestamps.Unix timestamps are the number of seconds between a specific date and January 1, 1970 in UTC. You can use the fromtimestamp() method to convert a timestamp to a date.

from datetime import date
 
timestamp = (1576244364)
print("dates =", timestamp)

exports

Date = 2019-12-13
 
Process finished with exit code 0

(4) Print today's year, month and day

from datetime import date
 
# Today's date object
today = () 
 
print("Current year:", )
print("Current month:", )
print("Current day:", )

exports

Current year: 2022
Current month: 3
Current day: 29
 
Process finished with exit code 0

2.2 Time class

(1) The time object instantiated from the time class represents the local time.

from datetime import time
 
# time(hour = 0, minute = 0, second = 0)
a = time()
print("a =", a)
 
# time(hour, minute and second)
b = time(11, 34, 56)
print("b =", b)
 
# time(hour, minute and second)
c = time(hour = 11, minute = 34, second = 56)
print("c =", c)
 
# time(hour, minute, second, microsecond)
d = time(11, 34, 56, 234566)
print("d =", d)

exports

a = 00:00:00
b = 11:34:56
c = 11:34:56
d = 11:34:56.234566
 
Process finished with exit code 0

(2) Printing hours, minutes, seconds and microseconds

from datetime import time
 
a = time(11, 34, 56)
 
print("hourly=", )
print("minutes=", )
print("unit of angle or arc equivalent one sixtieth of a degree=", )
print("微unit of angle or arc equivalent one sixtieth of a degree=", )

exports

Hours = 11
Minutes = 34
Seconds = 56
Microseconds = 0
 
Process finished with exit code 0

2.3 The datetime class

(1) The datetime module has a named dateclass class that can contain information from date and time objects.

from datetime import datetime
 
#datetime(year, month, day)
a = datetime(2019, 11, 28)
print(a)
 
# datetime(year, month, day, hour, minute, second, microsecond)
b = datetime(2019, 11, 28, 23, 55, 59, 342380)
print(b)

exports

2019-11-28 00:00:00
2019-11-28 23:55:59.342380
 
Process finished with exit code 0

(2) Print year, month, hour, minute and time stamps

from datetime import datetime
 
a = datetime(2022, 3, 29, 23, 55, 59, 342380)
print("surname Nian =", )
print("moon =", )
print("date =", )
print("hour =", )
print("classifier for gifts; newspaper, magazine, documents etc. as a whole; for a complete menu of food. =", )
print("hour间戳 =", ())

exports

Year = 2022
Months = 3
Day = 29
Hour = 23
portions = 55
Timestamp = 1648569359.34238
 
Process finished with exit code 0

2.4 Classes

(1) The timedelta object represents the time difference between two dates or times.

from datetime import datetime, date
 
t1 = date(year = 2018, month = 7, day = 12)
t2 = date(year = 2017, month = 12, day = 23)
t3 = t1 - t2
print("t3 =", t3)
 
t4 = datetime(year = 2018, month = 7, day = 12, hour = 7, minute = 9, second = 33)
t5 = datetime(year = 2019, month = 6, day = 10, hour = 5, minute = 55, second = 13)
t6 = t4 - t5
print("t6 =", t6)
 
print("type of t3 =", type(t3)) 
print("type of t6 =", type(t6))

exports

t3 = 201 days, 0:00:00
t6 = -333 days, 1:14:20
type of t3 = <class ''>
type of t6 = <class ''>
 
Process finished with exit code 0

(2) Time difference between two timedelta objects

from datetime import timedelta
 
t1 = timedelta(weeks = 2, days = 5, hours = 1, seconds = 33)
t2 = timedelta(days = 4, hours = 11, minutes = 4, seconds = 54)
t3 = t1 - t2
 
print("t3 =", t3)

exports

t3 = 14 days, 13:55:39
 
Process finished with exit code 0

To this point, this article on Python processing date and time methods summarized in this article, more related Python date and time processing content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!