python time模块计算时间之间的差距(练习题) 目录 python time模块计算时间之间的差距 1. 当前月1号对应的0点的时间戳 2. n1的时间 n2的时间 n2 - n1的时间经历里多少年 月 日 时 分 秒 补充:python-time模块计算时间差 python time模块计算时间之间的差距 练习
目录
- python time模块计算时间之间的差距
- 1. 当前月1号对应的0点的时间戳
- 2. n1的时间 n2的时间 n2 - n1的时间经历里多少年 月 日 时 分 秒
- 补充:python-time模块计算时间差
python time模块计算时间之间的差距
练习题
1. 当前月1号对应的0点的时间戳
# 定义一个当前月分的一号0点字符串格式的时间
now_time = time.strftime('%Y-%m-01 00:00:00')
# 将格式化时间转换为结构化时间
jiegou = time.strptime(now_time, '%Y-%m-%d %H:%M:%S')
# 将结构化时间转换为对应的时间戳
shijiancuo = time.mktime(jiegou)
print('%s对应的时间戳为%s'%(now_time,shijiancuo))
2. n1的时间 n2的时间 n2 - n1的时间经历里多少年 月 日 时 分 秒
思想:需要首先将两个字符串时间转换为时间戳格式,然后相减,再转换为结构化时间,然后减去时间戳最开始时间(伦敦时间:1970/01/01 00:00:00)
import time
n1 = '2019-07-18 20:07:56'
n2 = '2019-07-19 22:03:12'
# 格式化时间转换为结构化时间
struct_time1, struct_time2 = time.strptime(n1, '%Y-%m-%d %H:%M:%S'), time.strptime(n2, '%Y-%m-%d %H:%M:%S')
# 结构化时间转换为时间戳格式
struct_time1, struct_time2 = time.mktime(struct_time1), time.mktime(struct_time2)
# 差的时间戳
diff_time = struct_time2 - struct_time1
# 将计算出来的时间戳转换为结构化时间
struct_time = time.gmtime(diff_time)
# 减去时间戳最开始的时间 并格式化输出
print('过去了{0}年{1}月{2}日{3}小时{4}分钟{5}秒'.format(
struct_time.tm_year-1970,
struct_time.tm_mon-1,
struct_time.tm_mday-1,
struct_time.tm_hour,
struct_time.tm_min,
struct_time.tm_sec
))
补充:python-time模块计算时间差
import time
# t = time.time()
# print(t)
# z = time.strftime('%Y-%m-%d %H:%M:%S')
# print(z)
#
# a = time.localtime(time.time())
# print(a)
nowtime = time.time()
longtime = time.strptime('2018-10-17 6:0:0','%Y-%m-%d %H:%M:%S')
print(longtime)
d = time.mktime(longtime)
print(d)
new = nowtime - d
print(new)
s = time.localtime(new)
ss = time.strftime('%H:%M:%S',time.localtime(new))
print(ss)
到此这篇关于python time模块计算时间之间的差距的文章就介绍到这了,更多相关python time模块计算时间内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
沃梦达教程
本文标题为:python time模块计算时间之间的差距(练习题)
猜你喜欢
- Python 保存数据的方法(4种方法) 2023-09-04
- 在centos6.4下安装python3.5 2023-09-04
- Python Pandas如何获取和修改任意位置的值(at,iat,loc,iloc) 2023-08-04
- CentOS7 安装 Python3.6 2023-09-04
- windows安装python2.7.12和pycharm2018教程 2023-09-03
- python线程池ThreadPoolExecutor与进程池ProcessPoolExecutor 2023-09-04
- Python实现将DNA序列存储为tfr文件并读取流程介绍 2022-10-20
- python中defaultdict用法实例详解 2022-10-20
- python中列表添加元素的几种方式(+、append()、ext 2022-09-02
- Python之路-Python中的线程与进程 2023-09-04