美文网首页
Python-时间及日期-06-获取年周次星期

Python-时间及日期-06-获取年周次星期

作者: Data_Python_VBA | 来源:发表于2020-04-21 20:03 被阅读0次

微信公众号原文

系统:Windows 7
语言版本:Anaconda3-4.3.0.1-Windows-x86_64
编辑器:pycharm-community-2016.3.2
Python:3.6.0

  • 这个系列讲讲Python对时间及日期的操作
  • 今天讲讲获取当前的年度、周次、星期信息
  • 涉及模块:datetime

Part 1:实现功能

  1. 获取当前日期时间信息
  2. 计算今天的年、周次、星期
  3. 计算3天后的年、周次、星期
  4. 计算3天前的年、周次、星期

Part 2:代码

import datetime

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

n = 3
time_delta = datetime.timedelta(days=n)
other_time_1 = current_time + time_delta
other_time_2 = current_time - time_delta

# 获取年、周次、星期几
y, w, d = current_time.isocalendar()
print("今天对应的年,周次,星期几:", y, w, d)

y, w, d = other_time_1.isocalendar()
print("3天后对应的年,周次,星期几:", y, w, d)

y, w, d = other_time_2.isocalendar()
print("3天前对应的年,周次,星期几:", y, w, d)



代码截图

1.png

运行结果

2.png

Part 3:部分代码解读

  1. current_time.isocalendar(),返回一个元组,分别为年、周次、星期
x = current_time.isocalendar()
print(x)
print(type(x))

运行结果

3.png

本文为原创作品,欢迎分享朋友圈

长按图片识别二维码,关注本公众号
Python 优雅 帅气


12x0.8.jpg

相关文章

网友评论

      本文标题:Python-时间及日期-06-获取年周次星期

      本文链接:https://www.haomeiwen.com/subject/pwgpihtx.html