python—日期和时间

日期和时间在python中主要分为time模块、datetime模块、calendar模块

time模块

import time

print("当前时间:", time.time())
print("时间戳格式化为本地时间:", time.localtime())
t = (2018, 7, 17, 17, 3, 1, 1, 1, 0)
print("接收struct_time对象作为参数,与localtime函数反向操作:", time.mktime(t))
print("将一个时间戳转化为0时区的struct_time:", time.gmtime())
print("接受时间元组转化为一个可读的形式:", time.asctime(t))
print("把一个时间戳(按秒计算的浮点数)转化为time.sactime():", time.ctime())
# 推迟调用线程的运行,用参数secs来表示挂起的时间:
print("起始时间:", time.ctime())
time.sleep(10)
print("末时间:", time.ctime())
#输出为
当前时间: 1657528491.2608168
时间戳格式化为本地时间: time.struct_time(tm_year=2022, tm_mon=7, tm_mday=11, tm_hour=16, tm_min=34, tm_sec=51, tm_wday=0, tm_yday=192, tm_isdst=0)
接收struct_time对象作为参数,与localtime函数反向操作: 1531818181.0
将一个时间戳转化为0时区的struct_time: time.struct_time(tm_year=2022, tm_mon=7, tm_mday=11, tm_hour=8, tm_min=34, tm_sec=51, tm_wday=0, tm_yday=192, tm_isdst=0)
接受时间元组转化为一个可读的形式: Tue Jul 17 17:03:01 2018
把一个时间戳(按秒计算的浮点数)转化为time.sactime(): Mon Jul 11 16:34:51 2022
起始时间: Mon Jul 11 16:34:51 2022
末时间: Mon Jul 11 16:35:01 2022

datatime模块:包含了日期和时间的所有信息,datatime模块里面还定义了两个常量为最大年份maxyear和最小年份minyear

import datetime

print(datetime.date(2022, 7, 11))  # 输出指定日历中的一个日期
# 2022-07-11
print(datetime.date.today())  # 返回当天日期
# 2022-07-11
print(datetime.date.weekday(datetime.date.today()))  # 返回当前星期数
# 0
date = datetime.date(2022, 7, 11)
print(date.isoformat())  # 返回日期为ISO格式
# 2022-07-11
print(date.strftime("%Y-%m-%d"))  # 格式化输出日期
# 2022-07-11
print(datetime.time())  # time对象表示一天中的时间与任何特定日期都无关
# 00:00:00
print(datetime.time(hour=8, second=7))  # 通过tzinfo对象进行时间上的调整
# 08:00:07

# time中有两个常量,分别为max和min
print(datetime.time.max)
print(datetime.time.min)
# 23:59:59.999999
# 00:00:00
# isoformat返回时间为ISO格式,即”HH:MM:SS"的字符串
t = datetime.time(hour=8, second=7)
print(t.isoformat())
print(t.strftime("%H:%M:%S"))  # 格式化输出时间
# 08:00:07
# 08:00:07
print(datetime.datetime(year=2022, month=7, day=11, hour=18, second=35))
# 2022-07-11 18:00:35
print(datetime.datetime.today())  # 返回一个表示当前本地时间datetime对象
# 2022-07-11 18:34:25.848556
print(datetime.datetime.now())  # 返回一个表示当前本地时间的datetime对象
# 2022-07-11 18:35:32.722819

# date方法获取date对象,time方法获取time对象
now = datetime.datetime.now()
print(now.date())
print(now.time())
# 2022-07-11
# 18:38:33.537847

calendar模块:这是一个与日历相关的模块,主要用于输出某月的字符月历。

import calendar

print(calendar.isleap(2022))  # 判断是否为闰年
#输出为
False
print(calendar.leapdays(2000, 2022))  # 返回两个年份之间的闰年总数
#输出为
6
print(calendar.month(2022, 7))  # 返回一个多行字符串格式的year年month月日历
#输出为
     July 2022
Mo Tu We Th Fr Sa Su
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
print(calendar.monthcalendar(2022, 7))  # 单层嵌套列表,每个子列表代表一个星期。
#输出为
[[0, 0, 0, 0, 1, 2, 3], [4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30, 31]]
print(calendar.monthrange(2022, 7))  # 返回两个整数组成的元组(星期几, 月数)
#输出为
(4, 31)
print(calendar.weekday(2022, 7, 11))  # 给出星期码(从0为星期一开始到6)
#输出为
0
print(calendar.calendar(2022))  # 输出2022年的日历
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白只对大佬的文章感兴趣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值