Datetime module:

Datetime is python’s standard library for handling times and dates!

The name of the class function
data Date objects. Common attributes are year,month, and day
time Time object, the common properties are hour,minute,second, millisecond
Datetime (common!) Date/time object, commonly used the properties of the hour, minute, second, microsecond
Timedelta (common!) An interval object, which is the length between two points in time

(1) Date object — data class

The date class is a date class consisting of year, month, and day. Since Python is also an object-oriented programming language, there are constructors for classes. Constructors for this class are as follows:

class datetime.date(year, month, day):

Year: indicates the year, range [MINYEAR, MAXYEAR], i.e. [1, 9999]

Month: month, range [1, 12]

Day: specifies the day in a month. The maximum value is determined by the given year and month parameters. For example, February has 29 days in a leap year

1) use:

import datetime

d = datetime.date(2019.1.11)	# year, month, day
print(d)
Copy the code

(2) :

(2) Time object — time class:

import datetime

t = datetime.time(20.36.15.1)	# hour, minute, second, millisecond
print(t)
Copy the code

(3) Datetime object — datetime class

① Common methods:

  1. Date () : datetime.now().date() : datetime.now().time()

Practical:

from datetime import datetime

now = datetime.now()
print('The current date and time is:',now)

print('The current date is:',now.date())
print('The current time is:',now.time())
Copy the code

Effect:


  1. Date time converted to timestamp: time date object.timestamp ()

For example: datetime. Now (). Timestamp ()

Practical:

from datetime import datetime

print(datetime.now().timestamp())
Copy the code

Effect:


  1. Datetime.fromtimestamp (timestamp)

For example: datetime. Fromtimestamp (1534231316.796308)

Practical:

from datetime import datetime

print(datetime.fromtimestamp(1627700208.446621))
Copy the code

Effect:


  1. Date-time object to string: date-time object. Strftime (format)

For example: datetime. Now (). Strftime (” % % m – Y % d “)

Practical:

from datetime import datetime

print(datetime.now().strftime("%Y+%m+%d"))
Copy the code

Effect:


  1. String to datetime object: datetime.strptime(data_str, format)

For example: datetime. Strptime (‘ 15:28:18 2018-3-22 ‘, ‘% % Y – m – H: % d % % m: % S’)

Note: Format must match data_str format! \

Practical:

from datetime import datetime

a = datetime.strptime('the 2022-5-22 15:23:38'.'%Y-%m-%d %H:%M:%S')
print(type(a))
print(a)
Copy the code

Effect:

② Format strings are commonly used in the following formats:

format describe
%Y / %y years
%m month
%d day
%H / %I when
%M points
%s seconds

(4) Timedelte class:

This class is used for time calculation! Note: when used — must be a time object and a timedelta object to calculate, otherwise error!

Practical:

import datetime

now = datetime.datetime.now()
a = datetime.timedelta(hours=8,minutes=20,seconds=10)
print(type(a))
print(now - a)
print(now + a)
Copy the code

Effect:

Development:

In the project — our

object is usually derived from two date-time operations!

Practical:

import datetime

now = datetime.datetime.now()
td = datetime.datetime.fromtimestamp(1547211555.024259)
a = now - td
print(a)
print(type(a))
Copy the code

Effect:

Extension:

  1. Datetime.date.timetuple () : Returns the time.struct_time object corresponding to the date

    time.struct_time(tm_year=2017, tm_mon=4, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=105, tm_isdst=-1)

  2. Datetime.date.weekday () : returns the week of the date

  3. Datetime. Date. Isocalendar () : returns to format such as tuples (year, month, day), (2017, 15, 6)

  4. Datetime.date.isoformat () : Returns the format as YYYY-MM-DD

  5. Datetime.date.isoweekday () : returns the week of the given date (0-6). Monday =0, Sunday =6

  6. Datetime.date.replace (year,month,day) : Replaces the given date without changing the original date