栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

对年月日的操作
      • 一、显示今年的日历图。
      • 二、判断是不是闰年?
      • 三、显示任意这个月的日历图。
      • 四、这个月有几天? 9月1日是星期几?
      • 五、这个月的第 ?天
      • 六、获取当前时间
      • 七、1 分钟掌握 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)  

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/269479.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号