day 16 常用模块 time 和 datetime(1)

时间模块(time和datetime)

time模块

  1时间模块优先掌握的操作

  时间分为三种格式

  第一种:时间戳 从1970年就要开始计时到现在的秒数
~作用:用于时间间隔的计算
1 import time
2 res=time.time()
3 print(res)
# 输出结果:   1595684090.4307303

第二种: 按照某种格式显示时间:2020-07-25 02:30:00
~作用:用于展示时间(具体举例)
1 import  time
2 res1=time.strftime("%Y-%m-%d %H:%M:%S %p")#这里注意格式的大小写
3 print(res1)
4 res2=time.strftime("%Y-%m-%d %X %p")#这种简单些  与res1一样
5 print(res2)
# res1 输出结果:2020-07-25 21:46:49 PM
# res2 输出结果:2020-07-25 21:46:49 PM

第三种: 结构化的时间
~作用:用于单独的获取时间的某一部分
1 import time
2 res =time.localtime() #提取的是现在所有的时间包括所有
3 print(res)
4 
5 res1 = res.tm_year #提取时间 :年 2020
6 res2 =res.tm_mday#提取时间 :天  25
7 print(res1,res2)
# 输出结果:
# time.struct_time(tm_year=2020, tm_mon=7, tm_mday=25,
# tm_hour=21, tm_min=55, tm_sec=29, tm_wday=5, tm_yday=207,
# tm_isdst=0)


datatime模块
  常用的如下:
1 import datetime
2 import  time
3 print(datetime.datetime.now()) #返回 2020-07-25 22:09:11.938874
4 print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2020-07-25
5 print(datetime.datetime.now() )
6 print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
7 print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
8 print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
9 print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分

# 输出结果
2020-07-25 22:09:11.938874
2020-07-25
2020-07-25 22:09:11.938874
2020-07-28 22:09:11.938874
2020-07-22 22:09:11.938874
2020-07-26 01:09:11.938874
2020-07-25 22:39:11.938874



2时间模块需要掌握的操作 :时间格式的转换


struct_time->时间戳
1 import time
2 s_time=time.localtime()
3 print(s_time)
4 print(time.mktime(s_time))

# 输出结果
time.struct_time(tm_year=2020, tm_mon=7, tm_mday=25, tm_hour=22,tm_min=43, tm_sec=9, tm_wday=5, tm_yday=207, tm_isdst=0)
1595688189.0
时间戳->struct_time    和上面的相反

1 import time
2 tp_time=time.time()
3 print(tp_time)
4 print(time.localtime(tp_time))


# 补充:世界标准时间与本地时间
 1 import time
 2 print(time.localtime())
 3 print(time.gmtime()) # 世界标准时间,了解
 4 
 5 print(time.localtime(1595688189.0))  #里面填的是秒 从1970年开始进行推算的
 6 print(time.gmtime(1111111)) #里面填的是秒 从1970年开始进行推算的
 7 
 8 
 9 # struct_time->格式化的字符串形式的时间
10 import time
11 s_time=time.localtime()
12 print(s_time)
13 print(time.strftime('%Y-%m-%d %H:%M:%S',s_time))
14 
15 print(time.strptime('1988-03-03 11:11:11','%Y-%m-%d %H:%M:%S'))
16 
17 # !!!真正需要掌握的只有一条:format string<------>timestamp
18 # '1988-03-03 11:11:11'+7
19 # format string--->struct_time--->timestamp
20 
21 import time
22 struct_time=time.strptime('1988-03-03 11:11:11','%Y-%m-%d %H:%M:%S')
23 timestamp=time.mktime(struct_time)+7*86400
24 print(timestamp)
25 
26 # format string<---struct_time<---timestamp
27 import time
28 res=time.strftime('%Y-%m-%d %X',time.localtime(timestamp))
29 print(res)
30 
31 time.sleep(3)

 了解知识
1 import time
2 print(time.asctime())
3 
4 
5 import datetime
6 # print(datetime.datetime.now())
7 # print(datetime.datetime.utcnow())
8 
9 print(datetime.datetime.fromtimestamp(333333))

    补充知识:

 1.0格式化字符串的时间格式
%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]. 以十进制数[01,31]表示的月份中的一天。
%H Hour (24-hour clock) as a decimal number [00,23]. 小时(24小时制)为十进制数字[00,23]。

%I Hour (12-hour clock) as a decimal number [01,12]. 小时(12小时制)为十进制数字[01,12]。
%j Day of the year as a decimal number [001,366]. 一年中的天,以十进制数字[001,366]为准。

%m Month as a decimal number [01,12]. 以十进制数字[01,12]表示的月份。
%M Minute as a decimal number [00,59]. 分钟以十进制数字[00,59]表示。
%p Locale’s equivalent of either AM or PM. (1)相当于AM或PM的语言环境。 (1)
%S Second as a decimal number [00,61]. (2) 第二个十进制数字[00,61]。 (2)
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53].
一年中的周号(星期日为一周的第一天),以十进制数[00,53]。
All days in a new year preceding the first Sunday are considered to be in week 0. (3)
第一个星期日之前的新年所有天数都视为在第0周。(3)
%w Weekday as a decimal number [0(Sunday),6]. 工作日为十进制数字[o(Sunday),6]。
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53].
一年中的周号(星期一为一周的第一天),以十进制数表示[00,53]。
All days in a new year preceding the first Monday are considered to be in week 0. (3)
在第一个星期一之前的新的AlL天被认为是在o周。 (3)
%x Locale’s appropriate date representation. 语言环境的适当日期表示形式。

%X Locale’s appropriate time representation. 语言环境的适当时间表示形式。
%y Year without century as a decimal number [00,99]. 没有世纪的年份作为十进制数字[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].
时区偏移量,表示与UTC / GMT的正或负时差,格式为+ HHMM或-HHMM,其中H代表十进制小时数字,M代表十进制分钟数字[-23:59,+23:59]。

%Z Time zone name (no characters if no time zone exists). 时区名称(如果不存在时区,则没有字符)。

%% A literal '%' character.文字的字符。

2.0 时间格式转换

其中计算机认识的时间只能是'时间戳'格式,而程序员可处理的或者说人类能看懂的时间有: '格式化的时间字符串','结构化的时间' ,
于是有了下图的转换关系



 1 #--------------------------按图1转换时间
 2 # localtime([secs])
 3 # 将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。
 4 time.localtime()
 5 time.localtime(1473525444.037215)
 6 
 7 # gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
 8 
 9 # mktime(t) : 将一个struct_time转化为时间戳。
10 print(time.mktime(time.localtime()))#1473525749.0
11 
12 
13 # strftime(format[, t]) : 把一个代表时间的元组或者struct_time(如由time.localtime()和
14 # time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个
15 # 元素越界,ValueError的错误将会被抛出。
16 print(time.strftime("%Y-%m-%d %X", time.localtime()))#2016-09-11 00:49:56
17 
18 # time.strptime(string[, format])
19 # 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
20 print(time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X'))
21 #time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6,
22 #  tm_wday=3, tm_yday=125, tm_isdst=-1)
23 #在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。

1 1 #--------------------------按图2转换时间
2 2 # asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。
3 3 # 如果没有参数,将会将time.localtime()作为参数传入。
4 4 print(time.asctime())#Sun Sep 11 00:43:43 2016
5 5 
6 6 # ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为
7 7 # None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
8 8 print(time.ctime())  # Sun Sep 11 00:46:38 2016
9 9 print(time.ctime(time.time()))  # Sun Sep 11 00:46:38 2016
 
原文地址:https://www.cnblogs.com/kwkk978113/p/13378807.html