第11章 时间和日期

时间和日期

在Python中处理时间相关的标准库,常用的有三个:time、datetime、calendar,它们分别负责不同的功能;

  • time:  提供时间和日期访问和转换的 函数。
  • datetime:   用于处理日期和时间计算相关的类。
  • calendar: 提供日历相关的   函数。

time使用

import time

#返回当前时间的时间戳(格林尼治时间1970年01月01日00时00分00秒起至现在的总秒数)
now = time.time()
print("当前的时间戳是:%f" % now)

#将时间戳格式化为本地时间,返回struct_time对象(参数用于接受时间戳,默认为当前时间戳)
print("当前时间", time.localtime())
print("0时间戳对应的时间", time.localtime(0))

#时间元组
t = (2018, 7, 17, 17, 3, 1, 1, 1, 0)
#mktime返回用秒数来表示时间的浮点数,参数可以是struct_time实例,也可以是九位元组元素
secs = time.mktime(t)
print("time.mktime(t) : %f" %  secs)
print("time.mktime(time.localtime(secs)): %f" % time.mktime(time.localtime(secs)))

#gmtime将一个时间戳转换为UTC时区的struct_time
print("time.gmtime():", time.gmtime())
print("time.gmtime(0):", time.gmtime(0))

#接受时间元组并返回一个可读的形式为:Sun May 21 22:13:29 2023,这个格式化可认为是国际读法
t = (2018, 7, 17, 17, 3, 1, 1, 1, 0)
print("time.asctime(t)", time.asctime(t))
print("time.asctime(time.localtime())", time.asctime(time.localtime()))

#相当于执行asctime(time.localtime(sec))
print("time.ctime() : %s" % time.ctime())
print("time.ctime(0) : %s" % time.ctime(0))

#sleep函数推迟调用线程的运行,可通过参数secs指秒数
time.sleep(5)

# 使用time.strftime格式化时间
t = (2018, 7, 17, 17, 3, 1, 1, 1, 0)
t = time.mktime(t)
print(time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t)))
struct_time = time.strptime("Jul 17 2018 09:03:01", "%b %d %Y %H:%M:%S")
print("返回的元组: ", struct_time)

1. 格式化符号说明

  • %y : 两位数的年份表示(00-99)
  • %Y : 四位数的年份表示(000-9999)
  • %m : 月份(01-12)
  • %d : 月内中的一天(0-31)
  • %H : 24小时制小时数(0-23)
  • %I : 12小时制小时数(01-12)
  • %M : 分钟数(00-59)
  • %S : 秒(00-59)
  • %a : 本地简化星期名称
  • %A : 本地完整星期名称
  • %b : 本地简化的月份名称
  • %B : 本地完整的月份名称
  • %c : 本地相应的日期表示和时间表示
  • %j : 年内的一天(001-366)
  • %p : 本地A.M.或P.M.的等价符
  • %U : 一年中的星期数(00-53)星期天为星期的开始
  • %w : 星期(0-6),星期天为星期的开始
  • %W : 一年中的星期数(00-53)星期一为星期的开始
  • %x : 本地相应的日期表示
  • %X : 本地相应的时间表示
  • %Z : 当前时区的名称
  • %% : %号本身

datetime使用

官网的对这个模块的介绍: 在支持日期时间数学运算的同时,实现的关注点更着重于如何能够更有效地解析其属性用于格式化输出和数据操作。

import datetime

date = datetime.date(2018, 7, 1)
print(date)

today = datetime.date.today()
print(today)
#返回当前星期数,星期一返回0,星期二返回1,以此类推
today = datetime.date.today()
print(today.weekday())
#返回当前星期数,与weekday类似,星期一返回1,星期二返回2,以此类推
today = datetime.date.today()
print(today.isoweekday())

date = datetime.date(2018, 7, 1)
print(date.isoformat())

date = datetime.date(2018, 7, 1)
print(date.strftime("%Y-%m-%d"))
print(date.strftime("%y-%b-%d"))

time1 = datetime.time()
print(time1)
time2 = datetime.time(hour=8, second=7)
print(time2)

print(datetime.time.min)
print(datetime.time.max)

t1 = datetime.time(hour=8, second=7)
print(t1.isoformat())

time = datetime.time(hour=16, second=7, microsecond=123)
print(time.strftime("%H:%M:%S"))
print(time.strftime("%p %I:%M:%S:%f"))

dt = datetime.datetime(year=2018, month=7, day=1, hour=16, second=10)
print(dt)

today = datetime.datetime.today()
print(today)

now = datetime.datetime.now()
print(now)

t1 = datetime.datetime.fromtimestamp(time.time()-86400)
print(t1)

date = datetime.date(2018, 7, 1)
time = datetime.time(8, 15, 10)
dt = datetime.datetime.combine(date, time)
print(dt)

dt1 = datetime.datetime(2018, 7, 1, 16, 15, 10)
dt2 = dt1 + datetime.timedelta(weeks=-2)
print(dt1)
print(dt2)
print(dt1 - dt2)
print(dt2 - dt1)

from datetime import datetime, timedelta
currentTime = datetime.now()
print("当前时间: ", currentTime.strftime("%Y-%m-%d %H:%M:%S"))
# 3秒前
print("3秒前: ", currentTime - timedelta(seconds=3))
# 5分钟前
print("5分钟前: ", currentTime - timedelta(minutes=5))
# 2小时前
print("2小时前: ", currentTime - timedelta(hours=2))
# 3天前
print("3天前: ", currentTime - timedelta(days=3))
# 4周前
print("4周前: ", currentTime - timedelta(weeks=4))

calendar使用

这个库主要提供日历相关的实用函数,该库工作中使用的不是很频繁,直接列出几个常用的函数示例:

import calendar
import datetime

if __name__ == '__main__':
    # 指定日期星期几,返回[0-6]
    print("-------------------------指定日期星期几--------------------------")
    print("2023-08-22 星期几?", calendar.weekday(2023, 8, 22) + 1)
    print("-------------------------判断是否是闰年--------------------------")
    print("判断2023是闰年吗?", calendar.isleap(2023))
    print("-------------------------判断有多少闰年--------------------------")
    print("判断2000-2023年有多少闰年?", calendar.leapdays(2000, 2023))

    print("-------------------------一个月的日历矩阵-------------------------")
    # 返回一个月的日历矩阵
    print(calendar.monthcalendar(2023, 8))
    print("-------------------------日期转换为时间戳--------------------------")
    # 将日期转换为时间戳
    print(calendar.timegm(datetime.datetime(2023, 8, 22).timetuple()))

    print("-------------------------使用prmonth打印指定月日历------------------")
    # 打印月日历
    print(calendar.prmonth(2023, 8))
    print("-------------------------使用month打印指定月日历--------------------")
    print(calendar.month(2023, 8))
    print("---------------------------打印整年日历----------------------------")
    # 返回整年日历
    print(calendar.prcal(2023))

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值