python模块分类
a 标准库
b 开源模块
c 自己写的函数,自定义模块
01 time 时间模想块 time detetime
查看帮助文档 help(time)
方法一 time.time()
#返回1970到此时间的时间蹉 返回格式类似于 14176788.5500 类似于这样子
验证时间方法:
x/3600/24/365 #返回1970到此数字的年份 如:45
为什么是1970年 ,ulinx操作系统延生时间开始
方法二 time.localtime()
返回本地时间 返回格式:
time.struct_time(tm_year=2018, tm_mon=9, tm_mday=8, tm_hour=7, tm_min=17, tm_sec=41, tm_wday=5, tm_yday=251, tm_isdst=0) #isdst 是否是夏今营时间 中国是utc加8 比标准utc早8小时
访问元组元素获得其中值的方法:
方法三:
time.timezone()
#返回标谁时间和本地时间之间的差值。。格式为-28800这样子
在中国相差8小时 验证28800/3600 = 8
方法四
time.altzone()
返回标准时和夏利时的差距,很多国家有夏令时,返回格式如-33200
方法五
time.dayliah()
返回是否使用夏令时:返时0:1
方法六:
time.sleep(2)
延时运行程序,单位秒:无返回值:
time.gmtime()
#返回时间点的元组时间格式 如:
time.struct_time(tm_year=2018, tm_mon=9, tm_mday=8, tm_hour=7, tm_min=17, tm_sec=41, tm_wday=5, tm_yday=251, tm_isdst=0)
可以不传参默认utc标准当时时间,
time.localtime()
返回本地时间:参数可以传入秒如 time.localtime(24521545454)
如何取基中数值:
x = time.localtime()
print(x.tm_year)
方法:time.mktime()
把节构化时间转成时间蹉形式的,秒形式的方法
x =time.struct_time(tm_year=2018, tm_mon=9, tm_mday=8, tm_hour=7, tm_min=17, tm_sec=41, tm_wday=5, tm_yday=251, tm_isdst=0)
time.mktime(x)
返回 45646446464
time.strftime()
按一个格式将元组转成时间格式 如 2016-01-02 08:30:15 这样子
其中参数说明:注意大小写
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%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.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
例如:
x = time.gmtime()
time.strftime('%Y-%m-%d %H:%M:%S' x)
#返回格式:201+-6-08-20 14:34:52
time.strptime() 第一个参数字符串,第二格式。返回是元组
例如:
time.strptime('2016-10-08 08:11-50',''%Y-%m-%d %H:%M:%S' ')
返回值:time.struct_time(tm_year=2018, tm_mon=9.........
help(time.asctime()) 使用方法:
Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
When the time tuple is not present, current time as returned by localtime()
is used. 传入元组,返回另一种格式的时间
例如:
import time
print(time.asctime())
返回:Sat Sep 8 09:01:09 2018 不传参以当前时间为准
方法:time. ctime()
将时间蹉转成 set aep 8 09:09 2018这样的格式
datetime模块
import datetime
datetime.datetime.now() 获取当前时间 返回 2017-09-08 09:11:38.632589
datetime.datetime.now()+datetime.timedelta(3) 返回三天后
datetime.datetime.now()+datetime.timedelta(hour = 3) 返回三小时后
时间替换:
time = datetime.datetime.new()
print(time.replace(minute =3,hour =2))