Introduction to Python datetime module

Introduction to Python datetime module

datetime mainly includes date (date-related operations), time (time-related operations), datetime (integration of date and time), datetime_CAPI (C language interface), timezone (time zone operation), timedelta (time difference representation).

At the same time, it should be noted that since the functions of date, time and datetime are extremely similar, the same method is only introduced once, and it feels that the applicable methods are basically applicable and have similar functions. Another thing is that the member functions of an object can usually be called through the static methods of the specified class. The text will not introduce too much, and the reader can try it out and use it.

date class

The date class creates an object through datetime.date(year, month, day), and then can refer to the corresponding information in the date instance through the year, month, and day objects. Among them, the range of year is [1, 9999], month is [1, 12], and day is determined according to the specific year and month.

Common functions are introduced as follows:

  • datetime.date.today(): Returns the date object of the current date
  • datetime.date.weekday(date): The passed date object returns an integer, which represents the week number of the passed date, Monday to Sunday is 0 to 6
  • datetime.date.isoweekday(date): Similar to weekday function, but Monday to Sunday is 1 to 7
  • datetime.date.isocalendar(date): The incoming date object returns a cell, which represents the year, the week of the current year, and the day of the week. Day of the week means from 0 to 6
  • [date].replace([year, month, day]) or datetime.date.repalce(date, [year, month, day]): Replace the corresponding attribute in the specified object, and other attributes remain unchanged. For example d.replace(year=2020)
  • datetime.date.resolution: returns a datetime.deltatime object, representing the current resolution

time class

The time class creates an object through datetime.time(hour, minute, second, microsecond), and the attribute values ​​of the corresponding object are obtained through hour, minute, second, microsecond.

datetime class

The datetime class creates an object through datetime.datetime(year, month, day, hour, minute, second, microsecond, tzinfo), and the attribute value of the corresponding object is obtained through a similar method.

  • datetime.datetime.now(): Get the detailed time information of the current moment
  • datetime.datetime.combine(date, time): Pass in date and time objects, and combine the two information to generate corresponding datetime objects.
  • timestamp(): Convert a datetime object into a timestamp object (floating point number, calculated from 1970-1-1 00:00:00 UTC+0:00)

timezone class

The timezone class describes the time zone corresponding to the time, and an instance can be constructed through timezone(timedelta). It should be noted that only datetime.datetime class time zone information and the time zone information of datetime objects created by default are empty.

  • astimezone(): Set the time zone for a datetime object

timedelta 类

Adding and subtracting between time will generate the timedelta class, which is constructed by timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0). The specific operation is to add, subtract, and subtract time. It is relatively simple. It should be noted that the days attribute of timedelta is the number of complete days between the two moments, and seconds is the number of seconds after the difference of days is removed (so the number of seconds must be less than 1 In the same way, microseconds is the number of microseconds after the time difference is removed (so the number of microseconds must be less than 1 second).

Time format

In fact, a large number of operations involving time are the conversion of time in different formats. First, I give an explanation of formatting characters, and then introduce the commonly used functions one by one.

symbol Description
%and Two-digit year representation (00-99)
%AND Four-digit year representation (000-9999)
%m Month (01-12)
%d     Day of the month (0-31)
%H Hours in 24-hour format (0-23)
%I     Hours in 12-hour format (01-12)
%M     Number of minutes (00=59)
%S     Seconds (00-59)
%a     Local simplified week name
%A     Full local week name
%b     Locally simplified month name
%B     Local full month name
%c     Local corresponding date and time display
%j     Day of the year (001-366)
%p     Equivalent of local AM or PM
% U    The number of weeks of the year (00-53) Sunday is the beginning of the week
%w     Week (0-6), Sunday is the beginning of the week
%W     The number of weeks in the year (00-53) Monday is the start of the week
%x     Local corresponding date representation
%X     Local corresponding time representation
%WITH     The name of the current time zone
%%     % Sign itself

Common functions:

  • datetime.*.fromtimestamp(time): Pass in the time information (timestamp) obtained by time.time to generate the specified object corresponding to the time
  • datetime.*.utcfromtimestamp(time): Pass in the time information (timestamp) obtained by time.time, and generate the specified object corresponding to the time utc timestamp (using Greenwich Mean Time)
  • strftime(format): Format the string according to the above formatting characters
  • strptime(string, format): According to the incoming time string and format string, output the datetime object at the specified time
  • __str__(): Built-in representation string, the format is like 2020-02-25 15:48:46.748326
  • ctime(): ctime format representation, like Tue Feb 25 15:48:46 2020
  • format(): Format the string according to the above formatting characters
  • isoformat(): Format the string according to the iso standard

time class introduction

What follows is not the content of datetime, but the content of the independent time package. At the same time, only a brief introduction is given here.

Common functions:

  • time.time(): Returns the UTC time expressed as a floating point number
  • time.gmtime(time): Convert floating-point number representation to time_struct class
  • time.mktime(time_struct): Convert the time_struct class to floating point representation. After mktime(gmtime(time)), there is a certain deviation from the previous time floating point number, but the difference after conversion between different times can be guaranteed (accurate to the second)
  • time.strftime(time_stuct, format): Convert the time_struct class to the specified format
  • time.strptime(string, format): Format the string variable according to the specified format, and get a time_struct class.

Guess you like

Origin blog.csdn.net/a40850273/article/details/104491694
Recommended