python time和datetime

说明:
datetime 比 time 高级了不少,可以理解为 datetime 基于 time 进行了封装,提供了更多实用的函数。

在实际实用中,用得比较多的是 datetime.datetime 和 datetime.timedelta ,另外两个 datetime.date 和 datetime.time 实际使用和 datetime.datetime 并无太大差别。
参考:
参考1:python datetime和time模块的区别
参考2:Python中time和datetime的区别与联系

一、time

1、time()

说明:time()返回当前时间的时间戳,无参数,返回类型为float


# time()返回当前时间的时间戳,无参数,返回类型为float
print(time.time(),type(time.time()))

结果:

1602649543.5181773 <class 'float'>

2、strftime()

说明: strftime()接收时间元组(9位固定元组,或struct_time对象)并返回以可读字符串表示的当地时间,格式由参数 format 决定。
参数format 格式,可选参数t(struct_time对象,或9位时间元组),返回类型为str

# strftime()接收时间元组(9位固定元组,或struct_time对象)并返回以可读字符串表示的当地时间,格式由参数 format 决定。
# 参数format 格式,可选参数t(struct_time对象,或9位时间元组),返回类型为str
print(time.strftime('%Y-%d'), type(time.strftime("%Y-%d")))

结果:

2020-14 <class 'str'>

3、strptime()

说明:strptime()把时间字符串转化为时间元组,参数1时间字符串,参数2forma格式化字符串,返回类型struct_time

# strptime()把时间字符串转化为时间元组,参数1时间字符串,参数2forma格式化字符串,返回类型struct_time
a = '20-11'
print(time.strptime(a, "%y-%m"),type(time.strptime(a, "%y-%m")))

结果:

time.struct_time(tm_year=2020, tm_mon=11, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=306, tm_isdst=-1) <class 'time.struct_time'>

4、ctime()

说明:ctime()把时间戳转化为英文时间类型,参数为数字即秒数(不输入默认传入time.time())返回类型为str

# ctime()把时间戳转化为英文时间类型,参数为数字即秒数(不输入默认传入time.time())返回类型为str
print(time.ctime(), type(time.ctime()))

结果:

Wed Oct 14 12:34:43 2020 <class 'str'>

5、localtime()

说明:localtime()格式化时间戳为本地的时间,参数为数字即秒数(不输入默认传入time.time()),返回类型struct_time

# localtime()格式化时间戳为本地的时间,参数为数字即秒数(不输入默认传入time.time()),返回类型struct_time
print(time.localtime(), type(time.localtime()))

结果:

time.struct_time(tm_year=2020, tm_mon=10, tm_mday=14, tm_hour=12, tm_min=34, tm_sec=43, tm_wday=2, tm_yday=288, tm_isdst=0) <class 'time.struct_time'>

6、mktime()

说明:mktime()把结构化的时间或者完整的9位元组元素转化为时间戳(float),参数为struct_time对象或9位时间元组,返回类型为float

# mktime()把结构化的时间或者完整的9位元组元素转化为时间戳(float),参数为struct_time对象或9位时间元组,返回类型为float
time_tuple = (2020, 11, 5, 13, 51, 18, 2, 317, 0)
print(time.mktime(time.strptime(a, "%y-%m")))
print(time.mktime(time_tuple))

结果:

1604160000.0
1604555478.0

二、datetime模块

1、创建(获取)datetime实例对象

import datetime

# 创建一个datetime实例对象
# 方法一:datetime.now()  方法二:datetime(年,月,日) 年月日三个参数是必需参数,其他是可选参数
print(datetime.datetime.now(), type(datetime.datetime.now()))
print(datetime.datetime(2019, 7, 18), type(datetime.datetime(2019, 7, 18)))
# strptime()以时间字符串创建一个datetime实例对象
a = '2019-11'
print(datetime.datetime.strptime(a, '%Y-%m'), type(datetime.datetime.strptime(a, '%Y-%m')))

结果:

2020-10-14 15:59:51.142798 <class 'datetime.datetime'>
2019-07-18 00:00:00 <class 'datetime.datetime'>
2019-11-01 00:00:00 <class 'datetime.datetime'>

2、datetime实例对象常用方法

2.1时间按格式输出

datetime.strftime()方法

a = datetime.datetime.now()   # 获取datetime实例对象
print(a.strftime('%d-%y'), type(a.strftime('%d-%y')))  # strftime()实例方法按格式输出日期,输出类型str

结果

14-20 <class 'str'>
2.2返回time.struct_time对象

datetime.timetuple()

a = datetime.datetime.now()   # 获取datetime实例对象
print(a.timetuple(), type(a.timetuple()))  # 返回一个time.struct_time对象

结果:

time.struct_time(tm_year=2020, tm_mon=10, tm_mday=14, tm_hour=16, tm_min=10, tm_sec=0, tm_wday=2, tm_yday=288, tm_isdst=-1) <class 'time.struct_time'>
2.3获取属性值
a = datetime.datetime.now()   # 获取datetime实例对象
print(a.timetuple(), type(a.timetuple()))  # 返回一个time.struct_time对象
# 获取属性值
print(a.year)
print(a.month)
print(a.hour)
print(a.minute)

结果:

2020
10
16
23
2.4时间加减法

datetime.timedelta()
使用timedelta可以很方便的在日期上做天days,小时hour,分钟,秒,毫秒,微妙的时间计算,如果要计算月份则需要另外的办法

# 获取后一天
a = datetime.datetime(2019,11,20)   # 获取datetime实例对象
b = a + datetime.timedelta(days=1)
print(b)

结果:日期已经变为21号

2019-11-21 00:00:00
2.4.1时间加减法年份和月份:
a = time.localtime()
print(list(a))   
b = list(a)   # 把time.struct_time对象转化成列表
b[1] = b[1] - 1  # 修改列表元素
print(time.strftime('%y-%m-%d',tuple(b))) # 再把列表转化成元组格式化输出时间

结果:

[2020, 10, 14, 16, 45, 22, 2, 288, 0]
20-09-14
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值