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

The name of the class function
date 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, the date 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: year, range [MINYEAR, MAXYEAR], i.e. [1, 9999]
    • month: indicates month, range [1, 12]
    • day: Number of days 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.1, the sample

Import datetime now_date = datetime.date(2021, 8, 8) # print(now_date) # 2021-08-08Copy the code

1.2. Return the week corresponding to the date

import datetime

print(datetime.date(2021, 8, 8).weekday())  # 6
Copy the code

2, time

Import datetime now_time = datetime.time(13, 27, 22, 3) print(now_time) # 13:27:22.000003Copy the code

3, a datetime class

3.1. Common methods:

  • Datetime.now ()
  • Get the current date: datetime.now().date()
  • Get the current time: datetime.now().time()

3.1.1, sample

From datetime import datetime now_datetime = datetime.now() print(' datetime :', now_datetime) print(' datetime :', Now_datetime.date ()) print(' current time :', now_datetime.time())Copy the code

3.2. Date-time is converted to timestamp

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

3.2.1, sample

From datetime import datetime print(datetime.now().timestamp()) # 1628400938.312667Copy the code

3.3. Date-time is converted to timestamp

Datetime. Fromtimestamp (1628400938.312667)Copy the code

3.3.1, sample

From datetime import Datetime print(datetime.fromtimestamp(1628400938.312667)) # 2021-08-08 13:35:38.312667Copy the code

3.4. Convert date and time objects to strings

datetime.now().strftime('%Y-%m-%d')
Copy the code

3.4.1 track, sample

from datetime import datetime

print(datetime.now().strftime('%Y-%m-%d'))  # 2021-08-08
Copy the code

3.5 string to date and time object

datetime.strptime(data_str, format)
Copy the code

3.5.1 track of, sample

from datetime import datetime
print(datetime.strptime('2021-8-8 13:42:00', '%Y-%m-%d %H:%M:%S'))  # 2021-08-08 13:42:00
Copy the code

4. Interval objects

This class is used for time calculation! Note: The time object and timedelta object must be used to calculate, otherwise an error is reported.

4.1 the sample

import datetime

now = datetime.datetime.now()
a = datetime.timedelta(hours=10, minutes=12, seconds=22)
print(now - a)
print(now + a)
Copy the code

5. Obtain the days of the specified month

There is no method of how many days there are in a month in Python’s DateTime module, but you can get it using the Calendar module.

import calendar

monthRange = calendar.monthrange(2021, 8)
print(monthRange)  # (6, 31)
Copy the code

MonthRange can return two integers:

  • The first integer indicates the number of the week starting in the month (0: Monday… 6: Sunday).
  • Second integer: the number of days representing the last day of the month.