目录
Commonly used format codes: 常用格式代码:
time模块
- 第一类时间:时间戳
# encoding: utf-8
'''
# @Author : ccq
# @File : 1_timestamp.py
# @Time : 2019/6/25 15:09
'''
# 时间戳
import time
a = time.time()
b = a / 60
c = b / 60
d = c / 24
e = d / 365
print("差了%s分,差了%s小时,差了%s天,差了%s年" % (b, c, d, e))
- 第二类时间:格式化的时间字符串
#形似:2019-06-25 15:28:06 这样的
- 第三类时间:元组(struct_time)
# encoding: utf-8
'''
# @Author : ccq
# @File : 2_timetuple.py
# @Time : 2019/6/25 15:32
'''
# 时间元组(struct_time)
import time
print(time.localtime()) # 当地时间
print(time.gmtime()) # UTC时区时间
显示的结果如下:
time.struct_time(tm_year=2019, tm_mon=6, tm_mday=25, tm_hour=15, tm_min=33, tm_sec=16, tm_wday=1, tm_yday=176, tm_isdst=0)
time.struct_time(tm_year=2019, tm_mon=6, tm_mday=26, tm_hour=3, tm_min=3, tm_sec=42, tm_wday=2, tm_yday=177, tm_isdst=0)
#括号内分别是:年份,月份,日期,小时,分钟,秒,一周的第几天,一年的第几天,是否是夏令时
#tm_wday=1表示周二,tm_wday=0表示周一,以此类推。
时间戳、struct_time和格式化字符串的相互转换:
时间戳→struct_time:time.gmtime() ; time.localtime()
struct_time→时间戳:time.mktime()
struct_time→格式化字符串:time.strftime()
格式化字符串→struct_time:time.strptime()
#具体用法如下:
# encoding: utf-8
'''
# @Author : ccq
# @File : 3_conversion.py
# @Time : 2019/6/26 11:23
'''
# 三者之间的转化
import time
timestamp = time.time() # 获取时间戳
print("时间戳:", timestamp)
# 时间戳转化成struct_time
structtime = time.localtime(timestamp) # 转化成当地时间(即UTC+8)
print("时间戳转化成struct_time:", structtime)
# struct_time转化成时间戳
timestamp2 = time.mktime(structtime)
print("struct_time转化成时间戳:", timestamp2)
# struct_time转化成格式化字符串
strtime = time.strftime("%Y--%m--%d %H:%M:%S", structtime)
print("struct_time转化成格式化字符串:", strtime)
# 格式化字符串转化成struct_time
strptime = time.strptime(strtime, "%Y--%m--%d %H:%M:%S")
print("格式化字符串转化成struct_time:", strptime)
#当然还有另外两个方法:time.asctime()和time.ctime() 用法如下:
# struct_time转化成“格式化字符串”
print(time.asctime(structtime))
# 时间戳转化成“格式化字符串”
print(time.ctime(timestamp))
#asctime()和ctime()转化成的格式化字符串是不用指定格式的,它默认的格式是:
形如:Thu Jun 27 11:31:56 2019
即:%a %b %d %H:%M:%S %Y
#具体的格式,及其含义如下(可以通过使用help进行检索):
Commonly used format codes: 常用格式代码:
%Y Year with century as a decimal number. 以世纪为十进制数的年份。
%m Month as a decimal number [01,12]. 月份作为十进制数字[01,12]。
%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]。
%M Minute as a decimal number [00,59]. 分钟为十进制数[00,59]。
%S Second as a decimal number [00,61]. 秒为是十进制数[00,61]。
%z Time zone offset from UTC. 时区与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]. 小时(12小时时钟)作为十进制数字[01,12]。
%p Locale's equivalent of either AM or PM. 区域设置等价于AM或PM。
#下划线部分为翻译。。
datetime模块
# encoding: utf-8
'''
# @Author : ccq
# @File : datetime.py
# @Time : 2019/6/27 13:00
'''
import datetime
print("当前时间",datetime.datetime.now())
print("三天前的时间",datetime.datetime.now()+datetime.timedelta(-3))
print("三天后的时间",datetime.datetime.now()+datetime.timedelta(-3))
print("三个小时后的时间",datetime.datetime.now()+datetime.timedelta(hours=3))
print("三个小时后的时间",datetime.datetime.now()+datetime.timedelta(hours=-3))
#datetime模块用法如上,奇怪的是我再pycharm里无法运行,有没有人懂得为啥……可以评论告诉我。
#已经找到原因,原因如下:
我的文件名叫做datetime,这于模块名重合。那么在导入的时候,导入的不再是内置的datetime模块,而是我自己写的datetime模块,因此出现这样的问题。关于为什么导入的是自己写的而不是内置的:可以通过sys.path去查看目录,会发现,自己写的datetime模块在前,而系统模块在后,因此导入的就是自己写的了。