1 #!/urs/bin/env python
2 # -*- coding:utf-8 -*-
3 import time
4 print(time.time())
5 # 返回时间戳
6 print(time.sleep(5))
7 # 线程推迟指定时间运行
8 print(time.localtime())
9 # 返回本地时间 的struct_time对象格式
10 print(time.mktime(time.localtime()))
11 # 将一个时间对象(struct_time)转成时间戳
12 print(time.ctime(time.time()))
# 把一个时间戳转化成time.asctime的形式。
13 print(time.asctime(time.localtime()))
14 # 把时间戳或时间对象(struct_time)转成time.asctime()的形式
15 print("#"*80)
16 print(time.strftime("%y-%m-%d %X", time.localtime()))
17 print(time.strftime("%Y-%m-%d %X", time.gmtime()))
18 # time.strftime(format,t)把一个代表时间元组或者struct_time(time.gmtime()和time.localtime())转化成时间字符串,t未指定,将传入time.localtime()。
19 print("*"*80)
20 print(time.strptime("2018-03-26 07:57", "%Y-%m-%d %H:%M"))
21 # 把一个格式化时间字符串转化成struct_tim
1522051889.3851166
None
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=26, tm_hour=16, tm_min=11, tm_sec=34, tm_wday=0, tm_yday=85, tm_isdst=0)
1522051894.0
Mon Mar 26 16:11:34 2018
Mon Mar 26 16:11:34 2018
################################################################################
18-03-26 16:11:34
2018-03-26 08:11:34
********************************************************************************
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=26, tm_hour=7, tm_min=57, tm_sec=0, tm_wday=0, tm_yday=85, tm_isdst=-1)
时间戳:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。time.time返回float类型。
元组方式:struct_time元组共有9个元素,返回struct_time的函数有gmtime() ,localtime(), strptime().
属性 值
tm_year (年) 2018
tm_mon(月) 1-12
tm_mdy(日) 1-30
tm_hour(时) 0-23
tm_min(分) 0-59
tm_sec(秒) 0-60
tm_wday(周) 0-6
tm_yday(一年中的第几天) 1-366
tm_isdst(是否是夏令时) 默认为-1
常见格式:
Directive | Meaning | Notes |
---|---|---|
%a |
Locale’s abbreviated weekday name. | |
%A |
Locale’s full weekday name. | |
%b |
Locale’s abbreviated month name. | |
%B |
Locale’s full month name. | |
%c |
Locale’s appropriate date and time representation. | |
%d |
Day of the month as a decimal number [01,31]. | |
%H |
Hour (24-hour clock) as a decimal number [00,23]. | |
%I |
Hour (12-hour clock) as a decimal number [01,12]. | |
%j |
Day of the year as a decimal number [001,366]. | |
%m |
Month as a decimal number [01,12]. | |
%M |
Minute as a decimal number [00,59]. | |
%p |
Locale’s equivalent of either AM or PM. | (1) |
%S |
Second as a decimal number [00,61]. | (2) |
%U |
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. | (3) |
%w |
Weekday as a decimal number [0(Sunday),6]. | |
%W |
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. | (3) |
%x |
Locale’s appropriate date representation. | |
%X |
Locale’s appropriate time representation. | |
%y |
Year without century as a decimal number [00,99]. | |
%Y |
Year with century as a decimal number. | |
%z |
Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. | |
%Z |
Time zone name (no characters if no time zone exists). | |
%% |
A literal '%' character. |
将时间戳转为字符串格式 print(time.gmtime(time.time()-86640)) #将utc时间戳转换成struct_time格式 print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将utc struct_time格式转成指定的字符串格式
日期字符串 转成 时间戳 string_2_struct = time.strptime("2016/05/22","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式 print(string_2_struct) struct_2_stamp = time.mktime(string_2_struct) #将struct时间对象转成时间戳 print(struct_2_stamp)
datetime模块
1 #!/urs/bin/evn python
2 # -*- coding:utf-8 -*-
3 #时间加减
4 import datetime
5 import time
6 print(datetime.datetime.now())
7 # 返回2018-03-26 17:23:03.617732
8 print(datetime.datetime.time(datetime.datetime.now()))
9 # 返回17:23:03.617732
10 print(datetime.time(3))
11 # 00:00:00
12 print(datetime.date.fromtimestamp(time.time()))
13 # 时间戳直接转成日期格式 2018-03-26
14 print(datetime.timedelta(3))
15 # 返回3 days, 0:00:00
16 """def __new__(cls, days=None, seconds=None, microseconds=None, milliseconds=None, minutes=None, hours=None, weeks=None)
17 """
18 # 时间计算
19 print(datetime.datetime.now()+datetime.timedelta(3))
20 # 加3天
21 print(datetime.datetime.now()-datetime.timedelta(3))
22 # 减3天
23 print(datetime.datetime.now()-datetime.timedelta(hours=3))
24 # 减3小时
25 print(datetime.datetime.now()+datetime.timedelta(minutes=3))
26 # 加3分钟
27
28 # 时间替换
29 c_time = datetime.datetime.now()
30 print(c_time.replace(minute=3, hour=2)) #时间替换
31 print(c_time.replace(2017, 5, minute=3, hour=2)) #时间替换
32 print(c_time.replace(2017,5,12, 2,44,23))
2018-03-26 17:52:35.131057 17:52:35.132057 03:00:00 2018-03-26 3 days, 0:00:00 2018-03-29 17:52:35.132057 2018-03-23 17:52:35.132057 2018-03-26 14:52:35.132057 2018-03-26 17:55:35.132057 2018-03-26 02:03:35.132057 2017-05-26 02:03:35.132057 2017-05-12 02:44:23.132057