Python:年月日日历图的显示操作、time模块常用法

一、显示今年的日历图。

1、导入calendar,datetime的time 模块;
2、显示今天的日期 date.today();
3、显示日历图 calendar.calendar(2021)

import calendar
from datetime import date
mydate = date.today()
print("今天的日期:", mydate)
year_calendar = calendar.calendar(2021)
print(f"{mydate.year}年的日历图:{year_calendar}\n")

在这里插入图片描述

二、判断是不是闰年?

import calendar
from datetime import date
mydate = date.today()
print("今天的日期:", mydate)

is_leap = calendar.isleap(mydate.year)
print(is_leap)
print_leap_str = "%s年是闰年" if is_leap else "%s年不是闰年\n"
print(print_leap_str % mydate.year)

在这里插入图片描述

三、显示任意这个月的日历图。

import calendar
from datetime import date
mydate = date.today()
print("今天的日期:", mydate)
month_calendar = calendar.month(mydate.year, mydate.month) # 显示月的日历图
print(f"{mydate.year}年-{mydate.month}月的日历图:\n{month_calendar}\n")

在这里插入图片描述

四、这个月有几天? 9月1日是星期几?

# 这个月有几天
weekday, days = calendar.monthrange(mydate.year, mydate.month)
print(weekday) # 9月1日是星期二
print(days)
print(f'{mydate.year}年-{mydate.month}月的第一天是一周的第{weekday}天\n')
print(f'{mydate.year}年-{mydate.month}月共有{days}天\n')

在这里插入图片描述

五、这个月的第 ?天

# 月第一天
month_first_day = date(mydate.year, mydate.month, 1)
print(f"当月第一天:{month_first_day}\n")

# 月最后一天
month_last_day = date(mydate.year, mydate.month, days)
print(f"当月最后一天:{month_last_day}\n")

在这里插入图片描述

六、获取当前时间

from datetime import date, datetime
from time import localtime,strftime

date_today = date.today()
print("当前日期:",date_today)

time_today = datetime.today()
print("当前时间:",time_today)

time_local = localtime() #本地时间
print(time_local)
# 转化为"年-月-日 时:分:秒"的格式
print("本地时间:",strftime("%Y-%m-%d %H:%M:%S", time_local))

在这里插入图片描述
导入 from time import localtime,strptime 可以逆转时间格式。

from datetime import date, datetime
from time import localtime,strptime
struct_time = strptime('2021-09-23 12:45:45', "%Y-%m-%d %H:%M:%S") 
print("逆转时间格式:",struct_time) # struct_time类型就是time中的一个类

在这里插入图片描述

七、1 分钟掌握 time 模块

Python的 time 模块提供时间相关的类和函数。下面介绍五个最常用的time函数。

%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.

代码理解:

import time

# 1、此时此刻时间浮点数
s = time.time()
print("时间浮点数:",s)

# 2、时间数组
local_time = time.localtime(s)
print("时间数组:",local_time)

# 3、时间字符串
str_time = time.asctime(local_time)
print("时间字符串:",str_time)

# 4、格式化时间字符串
format_time = time.strftime('%Y-%m-%d %H:%M:%S', local_time)
print("格式化时间:",format_time)

# 5、格式化时间反转为时间数组
str_to_struct = time.strptime(format_time,'%Y-%m-%d %H:%M:%S')
print("反转时间数组:",str_to_struct)  

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

唐樽

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

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

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

打赏作者

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

抵扣说明:

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

余额充值