Python time&datetime

Python time&datetime 简单使用

time

import time

print(time.time())
print(time.localtime())
print(time.localtime(time.time()))
a = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print("格式当前时间戳:{}".format(a))

b = time.strptime(a, '%Y-%m-%d %H:%M:%S')
print("时间字符串转为struct_time类型:{}".format(b))

c = time.mktime(b)
print("struct_time类型转为时间戳:{}".format(c))

1559117392.3259501
time.struct_time(tm_year=2019, tm_mon=5, tm_mday=29, tm_hour=16, tm_min=9, tm_sec=52, tm_wday=2, tm_yday=149, tm_isdst=0)
time.struct_time(tm_year=2019, tm_mon=5, tm_mday=29, tm_hour=16, tm_min=9, tm_sec=52, tm_wday=2, tm_yday=149, tm_isdst=0)
格式当前时间戳:2019-05-29 16:09:52
时间字符串转为struct_time类型:time.struct_time(tm_year=2019, tm_mon=5, tm_mday=29, tm_hour=16, tm_min=9, tm_sec=52, tm_wday=2, tm_yday=149, tm_isdst=-1)
struct_time类型转为时间戳:1559117392.0

datetime

import datetime
i = datetime.datetime.now()
print ("当前的日期和时间是 %s" % i)
print ("ISO格式的日期和时间是 %s" % i.isoformat() )
print ("当前的年份是 %s" %i.year)
print ("当前的月份是 %s" %i.month)
print ("当前的日期是  %s" %i.day)
print ("dd/mm/yyyy 格式是  %s/%s/%s" % (i.day, i.month, i.year) )
print ("当前小时是 %s" %i.hour)
print ("当前分钟是 %s" %i.minute)
print ("当前秒是  %s" %i.second)
当前的日期和时间是 2018-11-26 17:46:58.499843
ISO格式的日期和时间是 2018-11-26T17:46:58.499843
当前的年份是 2018
当前的月份是 11
当前的日期是  26
dd/mm/yyyy 格式是  26/11/2018
当前小时是 17
当前分钟是 46
当前秒是  58
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值