Python之日期与时间处理模块(date和datetime)

Python之日期与时间处理模块(date和datetime)

https://www.cnblogs.com/yyds/p/6369211.html

https://blog.csdn.net/u013317445/article/details/86412417

https://blog.csdn.net/lc11535/article/details/103114310

https://blog.csdn.net/qq_18299691/article/details/82941947

https://blog.csdn.net/x18835129278/article/details/89955801

https://www.cnblogs.com/mashuqi/p/11576705.html

2、自己写得代码:

import time
import datetime

获得两端日期的天数

start_time = ‘2021-03-08’
end_time = ‘2021-06-15’
start = time.mktime(time.strptime(start_time, ‘%Y-%m-%d’))
end = time.mktime(time.strptime(end_time, ‘%Y-%m-%d’))
count_days = int((end - start) / (24 * 60 * 60))
print(count_days)

获得当前时间及输出时间字符串:

print(time.strftime("%Y-%m-%d", time.localtime()))
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
print(time.strftime("%Y%m%d%H%M%S", time.localtime()))

#求时间,间隔方法1
starttime = datetime.datetime.now()

long running

time.sleep(2)
endtime = datetime.datetime.now()
duringtime = endtime - starttime
print(duringtime.seconds)

#datetime的输出看看有什么内容。
print(datetime.datetime.now())
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print(datetime.datetime.now().strftime("%Y%m%d%H%M%S"))
print(datetime.datetime.now().strftime("%Y%m%d%H%M%S%f"))

#求时间,间隔方法2
a = time.time()

short running

time.sleep(3)
b = time.time()
seconds = b - a
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print("%02d:%02d:%02d" % (h, m, s))
#自己输出的
print(f"{h}:{m}:{s}".replace(".", “”))

时间戳的获取

t = time.time()
print (t) #原始时间数据
print (int(t)) #秒级时间戳
print (int(round(t * 1000))) #毫秒级时间戳
print (int(round(t * 1000000))) #微秒级时间戳
print(int(round(time.time() * 10000000000))) #时间戳(乘10个零)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值