Python函数式编程17 日期、时间操作

主要涉及一些时间处理,格式输出,时间戳转换

time

import time

now = time.time()       #获取当前时间戳,float

now_tuple = time.localtime(now)
print(now_tuple)
#格式化显示

now_str = time.strftime('%Y-%m-%d %H:%M:%S %p',now_tuple)
print(now_str)
now_str1 = time.strftime('%Y-%m-%d %H:%M:%S %p')
print(now_str1)
print(time.asctime(now_tuple))

#时间元组转化为时间戳
time_tuple = (2020,3,5,12,25,33,0,0,0)
print(time.mktime(time_tuple))

datetime

import datetime

#获取当前时间
print(datetime.datetime.today())
print(datetime.datetime.now())

#日期转时间戳
now_str = datetime.datetime.now()
now = now_str.timestamp()
print(now)
#时间戳转日期
time1 = 1676287632
t = datetime.datetime.fromtimestamp(time1)  #时间戳转日期时间
print(t)
#自定义格式化展示时间
now_time = datetime.datetime.today()
now_str = now_time.strftime('%Y-%m-%d %H:%M:%S %p')
print(now_str)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值