Python3 时间戳格式化和减法运算

复制代码
import datetime
import  time


# 获取当前时间(2023-02-16 16:41:36)
now_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# 将当前时间转换为秒级时间戳(1676536896.0)
now_date_seconds = time.mktime(time.strptime(now_date, '%Y-%m-%d %H:%M:%S'))
# 将当前时间转换为毫秒级(1673945635000)
now_date_millisecond = int(str(now_date_seconds*1000).split(".")[0])


# 30天之前(2023-01-17 16:49:52)
Thirty_days_ago_days = (datetime.datetime.now()-datetime.timedelta(days=30)).strftime('%Y-%m-%d %H:%M:%S')
# 30天之前转换为秒级时间戳(1673945523.0)
Thirty_days_ago_days_seconds = time.mktime(time.strptime(Thirty_days_ago_days, '%Y-%m-%d %H:%M:%S'))
# 30天之前转换为毫秒级(1673945635000)
Thirty_days_ago_days_millisecond = int(str(Thirty_days_ago_days_seconds*1000).split(".")[0])

print(now_date)
print(now_date_seconds)
print(now_date_millisecond)
print(Thirty_days_ago_days)
print(Thirty_days_ago_days_seconds)
print(Thirty_days_ago_days_millisecond)


# 输出效果
2023-02-16 17:06:33
1676538393.0
1676538393000
2023-01-17 17:06:33
1673946393.0
1673946393000
复制代码

 

posted @   Hello_worlds  阅读(120)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示