Basic usage of Python datetime module

import datetime as dt
The module mainly has five classes,
datetime class: displays the date and time
Date class: displays the date
Time class: display time
timedelta class: used to calculate date and time objects
timezone class: used to adjust the time zone

For a class, the functions in the class are collectively referred to as methods.

1. Use datetime class


A datetime object must pass at least three parameters: year, month and day,
3-6 parameters can be passed
m1 = dt.datetime(2020,2,28): define a datetime class object
m2 = dt.datetime(2020,2,29,23,59,59)
Use function:
dt.datetime.today(): returns the current local time
dt.datetime.now(tz=None): returns the current time. The tz parameter is used to select the time zone
dt.datetime.fromtimestamp(0): UNIX timestamp will be returned, starting from 08:00:00 on January 1, 1970 locally, plus parameters (in seconds)

dt.datetime.utcfromtimestamp(0): UNIX timestamp will be returned. UTC time starts from 00:00:00 on January 1, 1970, plus parameters (in seconds)

import datetime as dt
#As long as a datetime object needs to pass three parameters: year, month and day,
m1 = dt.datetime(2020,2,28,1)
print(m1)

m2 = dt.datetime(2020,2,29,23,59,59)
print(m2)

m3 = dt.datetime.today()
print(m3)

m4 = dt.datetime.now(tz=None)
print(m4)

#   The UNIX timestamp will be returned, starting from 08:00:00 on January 1, 1970, plus the parameter (in seconds)
m5 = dt.datetime.fromtimestamp(0)
print(m5)
#   UNIX timestamp will be returned. UTC time starts from 00:00:00 on January 1, 1970, plus parameters (in seconds)
m6 = dt.datetime.utcfromtimestamp(0)
print(m6)


2. Use date class and time class


m1 = dt.date(2020,2,29): define a data class object

Use function:
dt.date.today(): displays the current date

m4 = dt.time(23, 59, 59): define a time class object

import datetime as dt

m1 = dt.date(2020,2,29)
print(m1)

#Show current date
m2 = dt.date.today()
print(m2)

m3 = dt.date.fromtimestamp(0)   #Returns the UNIX Timeline start date (January 1, 1970)
print(m3)

m4 = dt.time(23, 59, 59)
print(m4)

3. Use DT Timedelta () class to calculate the date and time


If DT. Is used Timedelta () class object, then you can make DT The datetime class uses addition and subtraction directly
To adjust the time
Example:
m1 = dt.datetime.today()
delta = dt.timedelta(10) #10 day DT Timedelta object
m1 += delta # can be the same as DT The datetime class object performs addition and subtraction directly
print(m1)

import datetime as dt

m1 = dt.datetime.today()
print(m1)
#dt. The timedelta () function can create timedelta objects of days, hours, minutes and seconds,
#It can be used to add and subtract dates and times.
delta = dt.timedelta(10)
print(delta)
m1 += delta
print(m1)

delta2 = dt.timedelta(weeks=5)
print(delta2)

m1 += delta2
print(m1)


4. Formatting and parsing of date and time


datetime class, date class and time class all have formatting instance methods. strftime(format)
Only the datetime class has an instance resolution method. strptime(str,format)
Format is the format control character, which can be accessed online
Example:
Formatting is to change a class object into a string
d = dt.datetime.today()
time_d = d.strftime('%Y-%m-%d %H:%M:%S')
print(time_d)
print(type(time_d))    #time_ D is a string


When parsing, the string {becomes a} class object
str_date = '2020-02-29 23:59:59'
d2 = dt.datetime.strptime(str_date, '%Y-%m-%d %H:%M:%S')
print(d2)
print(type(d2)) #d2 is DT Datetime class object

The time format control character is shown at the bottom!

import datetime as dt

d = dt.datetime.today()
#d.strftime() formats the original datetime object into a string
time_d = d.strftime('%Y-%m-%d %H:%M:%S')
print(time_d)
print(type(time_d))
print('\n')
time_d2 = d.strftime('time:%I:%M:%S %p')
print(time_d2)
print('\n')

#You can use DT datetime. Strptime() parsing function,
#Parses a string into a date time object in the format of a time format controller.
str_date = '2020-02-29 23:59:59'
d2 = dt.datetime.strptime(str_date, '%Y-%m-%d %H:%M:%S')
print(d2)
print(type(d2)) #d2 is DT Datetime class object

5. Use of timezone class. A function of datetime class, astimezone (tz), is used to adjust the time zone


Example:
d = dt.datetime.now(tz= dt.timezone.utc) # current time, time zone UTC, i.e. + 0
bj_tz = dt.timezone(offset=dt.timedelta(hours=8)) # time zone: + 8
d2 = d.astimezone(bj_tz) # time zone is adjusted to the original time zone + 8, that is, 0 + 8 = + 8
print(d2)

import datetime as dt

d = dt.datetime.now(tz= dt.timezone.utc)
print(d)
print()

#Here is the timezone object defined. The parameter is the object of date time calculation
bj_tz = dt.timezone(offset=dt.timedelta(hours=8))
print(bj_tz)
print(type(bj_tz))  #<class 'datetime.timezone'>
print()
#d. The astimezone() function is used to change the time zone. The parameter is the timezone object
d2 = d.astimezone(bj_tz)
print(d2)


Date time controller
%y = two digit year
%Y = four digit year
%m , expressed in two digit months
%d) two digit days
%H = two digit hour (24-hour system)
%I  two digit hours (12 hour value)
%M = two minutes
%S = two seconds
%f , six bit microsecond representation
%Z = time zone


 

Keywords: Python Back-end

Added by namasteaz on Fri, 11 Feb 2022 23:47:00 +0200