datetime模块
datetime模块属于Python标准库,用来处理时间和日期。
官方文档: https://docs.python.org/3.6/library/datetime.html
先看一个练习题:
有一类文件,文件名为 output_1981.10.21.txt 这种格式。
要求使用Python 读取文件名中的日期时间信息,并找出这一天是星期几,并将文件改名为output_YYYY-MM-DD-W.txt
W:星期几(1-7), 星期一对应’1’, 星期天对应’7’
1 | import re |
datatime.strftime(format) 格式化
Return a string representing the date and time, controlled by an explicit format string.
返回一个表示日期和时间的字符串,由一个明确的格式化字符串控制(即传入的参数).
具体传入参数对应的返回值格式如下:
Directive | Meaning | Example | Notes |
---|---|---|---|
%a |
Weekday as locale’s abbreviated name. | Sun, Mon, …, Sat (en_US);So, Mo, …, Sa (de_DE) | (1) |
%A |
Weekday as locale’s full name. | Sunday, Monday, …, Saturday (en_US);Sonntag, Montag, …, Samstag (de_DE) | (1) |
%w |
Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. | 0, 1, …, 6 | |
%d |
Day of the month as a zero-padded decimal number. | 01, 02, …, 31 | |
%b |
Month as locale’s abbreviated name. | Jan, Feb, …, Dec (en_US);Jan, Feb, …, Dez (de_DE) | (1) |
%B |
Month as locale’s full name. | January, February, …, December (en_US);Januar, Februar, …, Dezember (de_DE) | (1) |
%m |
Month as a zero-padded decimal number. | 01, 02, …, 12 | |
%y |
Year without century as a zero-padded decimal number. | 00, 01, …, 99 | |
%Y |
Year with century as a decimal number. | 0001, 0002, …, 2013, 2014, …, 9998, 9999 | (2) |
%H |
Hour (24-hour clock) as a zero-padded decimal number. | 00, 01, …, 23 | |
%I |
Hour (12-hour clock) as a zero-padded decimal number. | 01, 02, …, 12 | |
%p |
Locale’s equivalent of either AM or PM. | AM, PM (en_US);am, pm (de_DE) | (1), (3) |
%M |
Minute as a zero-padded decimal number. | 00, 01, …, 59 | |
%S |
Second as a zero-padded decimal number. | 00, 01, …, 59 | (4) |
%f |
Microsecond as a decimal number, zero-padded on the left. | 000000, 000001, …, 999999 | (5) |
%z |
UTC offset in the form +HHMM or -HHMM (empty string if the object is naive). | (empty), +0000, -0400, +1030 | (6) |
%Z |
Time zone name (empty string if the object is naive). | (empty), UTC, EST, CST | |
%j |
Day of the year as a zero-padded decimal number. | 001, 002, …, 366 | |
%U |
Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. | 00, 01, …, 53 | (7) |
%W |
Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. | 00, 01, …, 53 | (7) |
%c |
Locale’s appropriate date and time representation. | Tue Aug 16 21:30:00 1988 (en_US);Di 16 Aug 21:30:00 1988 (de_DE) | (1) |
%x |
Locale’s appropriate date representation. | 08/16/88 (None);08/16/1988 (en_US);16.08.1988 (de_DE) | (1) |
%X |
Locale’s appropriate time representation. | 21:30:00 (en_US);21:30:00 (de_DE) | (1) |
%% |
A literal '%' character. |
% |
详见https://docs.python.org/3.6/library/datetime.html#strftime-strptime-behavior
datetime对象
datetime模块下的datetime对象的构造方法为:
class datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])
year, month 和 day 参数是必须的,其他参数可选, 参数tzinfo表示可选的时区信息,一般我们也用不到。参数值的范围如下:
- MINYEAR <= year <= MAXYEAR
- 1 <= month <= 12
- 1 <= day <= number of days in the given month and year
- 0 <= hour < 24
- 0 <= minute < 60
- 0 <= second < 60
- 0 <= microsecond < 1000000
例:
1 | from datetime import datetime |
如果我们只传入参数year, month, day, 则返回结果为:
1 | 2015-08-23 00:00:00 |
下面我们看下datetime对象的几个方法:
datetime.today() 返回本地当前的时间
1 | from datetime import datetime |
datetime.now([tz]) 返回本地当前的日期和时间。如果可选的参数 tz 为 None 或者没有指定,结果同today()
tz参数为时区参数
1 | "Construct a datetime from time.time() and optional time zone info." |
1 | from datetime import datetime |
datetime对象还有两个属性:min和max
1 | from datetime import datetime |
datetime模块的对象有如下:
- timedelta
- date
- datetime
- time
- tzinfo
有时间再来更新其他对象的用法…