Python3模块整理笔记-datetime

datetime模块的组成

datetime模块定义了5个类

  1. datetime.date 日期
  2. datetime.datetime 日期时间
  3. datetime.time 时间
  4. datetime.timedelta 时间间隔,两个时间点的间隔
  5. datetime.tzinfo 时区相关信息

日期时间格式化符号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
%y,%Y 两,四 位的年份表示
%m 月份
%d 月
%H,%I 24,12小时的小时数
%M 分钟数
%S 秒数

%a,%A 星期 简化,完整 名称
%b,%B 月份 简化,完成 名称
%c 本地相应的日期表示和时间表示
%j 年内的第几天
%p 本地AM或者PM的等价符号
%U 一年中的星期数,第几周
%w 星期0-6
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身

datetime.date类

类方法

1
2
3
4
5
6
7
8
9
10
11
12
fromisoformat(...) from builtins.type #根据一个日期字符串YYYY-MM-DD 生成日期对象
str -> Construct a date from the output of date.isoformat()
eg: datetime.date.fromisoformat("2018-09-09")

fromordinal(...) from builtins.type
int -> date corresponding to a proleptic Gregorian ordinal.

fromtimestamp(...) from builtins.type #根据一个时间戳,返回日期对象
timestamp -> local date from a POSIX timestamp (like time.time()).

today(...) from builtins.type 返回今天的日期对象
Current date or datetime: same as self.__class__.fromtimestamp(time.time()).

构造方法

1
datetime.date(year,month,day) 创建一个datetime.date对象

实例属性

1
2
3
day 
month
year

实例方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
ctime(...)
Return ctime() style string. 返回该日期的格式化时间 'Thu Jan 22 00:00:00 2015'

isocalendar(...) 返回 年,周,星期
Return a 3-tuple containing ISO year, week number, and weekday.

isoformat(...) 返回格式 YYYY-MM-DD
Return string in ISO 8601 format, YYYY-MM-DD.

isoweekday(...) 返回星期几,从1开始
Return the day of the week represented by the date.
Monday == 1 ... Sunday == 7

replace(...) 替换日期
Return date with new specified fields.

strftime(...) 根据给定的格式进行格式化 _date.strftime("%Y/%m/%d")
format -> strftime() style string.

timetuple(...) 返回该日期对应的time.struct_time对象
Return time tuple, compatible with time.localtime().

toordinal(...) 返回日期对应的公历时间
Return proleptic Gregorian ordinal. January 1 of year 1 is day 1.

weekday(...) 返回的是周记,从0开始
Return the day of the week represented by the d
ate.
Monday == 0 ... Sunday == 6

datetime.time类

类方法

1
2
3
# 根据给定的字符串如“23:30”返回相应的time对象
fromisoformat(...) from builtins.type
string -> time from time.isoformat() output

构造方法

1
time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object

实例属性

1
2
3
4
5
6
fold 
hour
microsecond 微妙
minute
second
tzinfo 时区

实例方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
dst(...) 
Return self.tzinfo.dst(self).

isoformat(...) #格式化返回时间
Return string in ISO 8601 format, [HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].
timespec specifies what components of the time to include.

replace(...) #替换时间
Return time with new specified fields.

strftime(...) #根据格式 返回时间
format -> strftime() style string.

tzname(...)
Return self.tzinfo.tzname(self).

utcoffset(...)
Return self.tzinfo.utcoffset(self).

datetime.datetime 类

类方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
combine(...) from builtins.type 传入日期,时间 构造datetime.datetiem对象
date, time -> datetime with same date and time fields

fromisoformat(...) from builtins.type
string -> datetime from datetime.isoformat() output

fromtimestamp(...) from builtins.type
timestamp[, tz] -> tz's local time from POSIX timestamp.

now(tz=None) from builtins.type 返回当前日期时间
Returns new datetime object representing current time local to tz.

tz
Timezone object.

If no tz is specified, uses local timezone.

strptime(...) from builtins.type 格式化日期
string, format -> new datetime parsed from a string (like time.strptime()).

utcfromtimestamp(...) from builtins.type
Construct a naive UTC datetime from a POSIX timestamp.

utcnow(...) from builtins.type 返回Utc时间
Return a new datetime representing UTC day and time.

构造方法

1
datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])

实例属性

1
2
3
4
5
6
fold
hour
microsecond
minute
second
tzinfo

实例方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
astimezone(...) 返回时区
tz -> convert to local time in new timezone tz

ctime(...) 打印时间时间
Return ctime() style string.

date(...) 返回datetime.date对象
Return date object with same year, month and day.

dst(...)
Return self.tzinfo.dst(self).

isoformat(...) 返回本地化时间
[sep] -> string in ISO 8601 format, YYYY-MM-DDT[HH[:MM[:SS[.mmm[uuu]]]]][+HH:MM].
sep is used to separate the year from the time, and defaults to 'T'.
timespec specifies what components of the time to include (allowed values are 'auto', 'hours', 'minutes', 'seconds', 'milliseconds', and 'microseconds').

replace(...) 替换日期时间
Return datetime with new specified fields.

time(...) 返回datetime.time对象
Return time object with same time but with tzinfo=None.

timestamp(...) 返回时间戳
Return POSIX timestamp as float.

timetuple(...) 返回time_status格式
Return time tuple, compatible with time.localtime().

timetz(...) 返回时间
Return time object with same time and tzinfo.

tzname(...)
Return self.tzinfo.tzname(self).

utcoffset(...)
Return self.tzinfo.utcoffset(self).

utctimetuple(...)
Return UTC time tuple, compatible with time.localtime().

datetime.timedelta类

类方法

1
2


构造方法

1
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

实例属性

1
2


实例方法

1
2
total_seconds(...)
Total seconds in the duration.

datetime.tzinfo类