Python Standard Library] [Date and Time Processing Library - datetime

datetime module provides time and date may be operated in various ways class. Date and time in support of the mathematical operation, the focus is more focused on how to achieve more effectively resolve its properties for formatting and outputting data manipulation.

datetime module comprises Data, all functions of the time, the common datatime example as follows:

DateTime.Now () : Gets the date and time of the day

datetime.date (t) : Gets today's date, t is datetime instance parameter

datetime.time (t) : get the time of day, t is datetime instance parameter

datetime.ctime (t) : [get every minute of the week Month Day Year] string format, t is datetime instance parameter

DateTime.UtcNow (t) : (when Universal Time Coordinated, UTC) UTC Gets the current date and time

datetime.timestamp (t) : Gets the day of the UNIX timestamp, t is datetime instance parameter

datetime.fromtimestamp (t_tamp) : Returns the UTC date and time according to the time stamp, t_tamp timestamp float

datetime.combine (D, t) : binding the date, time, generate a new datetime objects, d is the date of the object, t is the time of the object

datetime.strptime (DATE_STR, sf) : generates a new string and datetime objects according to the specified format, date_str string of date and time, sf specified format

datetime.timetuple (T) : all the properties of the datetime objects into objects tuples time, t is the instance parameter datetime

t.isocalendar () : Get ISO format of the date (in the form of tuples), t is the instance object datetime

t.strftime (date_str_format) : date time string obtained from a defined format, t is datetime objects instance, date_str_format specified format


Examples of basic functions inside the module datetime datetime class:

>>> from datetime import datetime, date, time          # 从 datetime 模块导入 datetime、date、time
>>> 
>>> print(datetime.now())                              # 返回当天的日期和时间
2020-02-21 15:32:27.356006
>>> 
>>> today = datetime.now()                             # 定义 today 为当前日期时间对象
>>> print(datetime.date(today))                        # 返回当天的日期对象
2020-02-21
>>> 
>>> print(datetime.time(today))                        # 返回当天的时间对象
15:57:38.770308
>>> 
>>> print(datetime.ctime(today))                       # 返回【星期 月 日 时 分 秒 年】格式的字符串
Fri Feb 21 15:57:38 2020
>>> 
>>> print(datetime.utcnow())                           # 返回当前的 UTC 日期和时间
2020-02-21 07:59:27.180956
>>> 
>>> print(datetime.timestamp(today))                   # 返回当天的 UNIX 时间戳,浮点数类型
1582271858.770308
>>> 
>>> print(datetime.fromtimestamp(datetime.timestamp(today)))   # 根据时间戳返回 UTC 日期时间
2020-02-21 15:57:38.770308
>>> 
>>> date1 = date(2020,2,21)                            # 使用 date 类,实现实例化 date1 对象
>>> time1 = time(16,2,30)                              # 使用 time 类,实现实例化 time1 对象
>>> print(datetime.combine(date1, time1))              # 绑定日期、时间,生成新的 datetime 对象
2020-02-21 16:02:30
>>> 
>>> NewDateTime = datetime.strptime("21/2/20 16:10", '%d/%m/%y %H:%M')   # 用字符串和指定格式生成新的 datetime 对象
>>> print(NewDateTime)
2020-02-21 16:10:00
>>> 
>>> today = datetime.now()                             # 定义 today 为当前日期时间对象
>>> for i in datetime.timetuple(datetime.now()):       # 将 today 当作时间元组,循环打印
	print(i)

2020
2
21
17
57
43
4
52
-1
>>> 
>>> print(today.isocalendar())                           # ISO 格式的日期
(2020, 8, 5)
>>> 
>>> print(today.strftime('%Y 年 %m 月 %d 日 %H:%M:%S %p   # 对 datetime 对象自定义格式,返回字符串类型的值
2020022117:57:02 PM

strftime()And strptime()methods will be used to format the date symbol time, meaning the respective symbols represent the following format:

Formatting symbols meaning
%a Local simplify week name
%A Local full weekday name
%b Local simplify month name
%B Local full month name
%c Local representation appropriate date and time representation
%d Within a month of the day (0-31)
%f Milliseconds decimal number, the zero-extension
%H 24-hour number (0-23) hours
%I 12-hour number (01-12) h
%j Day (001-366) during the year
%m Month (01-12)
%M The number of minutes (00 = 59)
%p Local AM or PM equivalent character
%S Sec (00-59)
% U Weeks of the year (00-53) for the week beginning Sunday
%w Week (0-6), Sunday is the start of week
%W Weeks of the year (00-53) for the week beginning Monday
%x Corresponding date local representation
%X Represents the corresponding local time
%Y Two-digit year representation (00-99)
%Y Four-digit year representation (000-9999)
%from ± HHMM UTC difference value [SS [.ffffff]] form (if the object is a simple type or an empty string)
%FROM Name of the current time zone
%% % Number itself
Published 149 original articles · won praise 518 · Views 460,000 +

Guess you like

Origin blog.csdn.net/qq_36759224/article/details/104427220