Python---time与datetime

import time

print(time.time())      # 获取当前的时间戳
# 1498488035.8299015

print(time.gmtime())    # 将时间戳转换成结构化时间元组(UTC时区),无参数表示将当前时间戳转换成时间的元组形式

# print(time.gmtime(1498488176.3209014))
# time.struct_time(tm_year=2017, tm_mon=6, tm_mday=26, tm_hour=14,
# tm_min=42, tm_sec=56, tm_wday=0, tm_yday=177, tm_isdst=0)

print(time.localtime())  # 将时间戳转换成结构化时间元组(UTC+8时区),无参数表示将当前时间戳转换成时间的元组形式

# time.struct_time(tm_year=2017, tm_mon=6, tm_mday=26, tm_hour=22, tm_min=48,
# tm_sec=7, tm_wday=0, tm_yday=177, tm_isdst=0)

t = time.localtime()
print(time.mktime(t))  # 将结构化时间元组形式转换成时间戳


print(time.strftime('%Y-%m-%d %H:%M:%S', t))   # 结构化时间元组转换成格式化字符串
# 2017-06-26 22:58:29

print(time.strftime('%Y-%m-%d %H:%M:%S'))   # 结构化的当前时间元组转换成格式化字符串
#2017-06-26 23:02:43

print(time.strptime('2017-06-26 22:58:29', '%Y-%m-%d %H:%M:%S'))  # 格式化字符串转换成结构化时间元组
# time.struct_time(tm_year=2017, tm_mon=6, tm_mday=26, tm_hour=22,
# tm_min=58, tm_sec=29, tm_wday=0, tm_yday=177, tm_isdst=-1)

print(time.strptime('2017-06-26 22:58:29', '%Y-%m-%d %H:%M:%S'))  # 格式化字符串转换成结构化时间元组


===========================================================
# 格式化
'''
    %Y  Year with century as a decimal number.
    %m  Month as a decimal number [01,12].
    %d  Day of the month as a decimal number [01,31].
    %H  Hour (24-hour clock) as a decimal number [00,23].
    %M  Minute as a decimal number [00,59].
    %S  Second as a decimal number [00,61].
    %z  Time zone offset from UTC.
    %a  Locale's abbreviated weekday name.
    %A  Locale's full weekday name.
    %b  Locale's abbreviated month name.
    %B  Locale's full month name.
    %c  Locale's appropriate date and time representation.
    %I  Hour (12-hour clock) as a decimal number [01,12].
    %p  Locale's equivalent of either AM or PM.
'''`
==========================================================

print(time.ctime())   # 时间戳转换成格式化字符串(%a %b %d %H:%M:%S %Y)
# Mon Jun 26 23:05:52 2017
print(time.asctime())  # 结构化时间元祖转换成格式化字符串(%a %b %d %H:%M:%S %Y)
# Mon Jun 26 23:07:22 2017
t1 = time.time()
t2 = time.localtime(t1)
print(time.ctime(t1))
# Mon Jun 26 23:09:52 2017
print(time.asctime(t2))
#Mon Jun 26 23:09:52 2017

time.sleep(3)          # 等待3秒
import datetime

print(datetime.datetime.now())
# 2017-06-26 23:22:25.128901

print(datetime.datetime.now() + datetime.timedelta(+3))  # 当前时间的3天后时间
# 2017-06-29 23:22:25.128901

print(datetime.datetime.now() + datetime.timedelta(-3))  # 当前时间的3天后时间
# 2017-06-23 23:22:25.128901

print(datetime.datetime.now() + datetime.timedelta(hours=3))  # 当前时间的3小时后时间
# 2017-06-27 02:22:25.128901

print(datetime.datetime.now() + datetime.timedelta(hours=-3))  # 当前时间的3小时前时间
# 2017-06-26 20:22:25.128901
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值