我是靠谱客的博主 霸气乐曲,最近开发中收集的这篇文章主要介绍Python获取时间Python获取昨天,今天,明天,本周,上周,本月,上月,本季,本年,去年时间,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Python获取昨天,今天,明天,本周,上周,本月,上月,本季,本年,去年时间

import datetime
from datetime import timedelta
  
now = datetime.datetime.now()
  
#今天
today = now
  
#昨天
yesterday = now - timedelta(days=1)
  
#明天
tomorrow = now + timedelta(days=1)<br><br>#当前季度
now_quarter = now.month / 3 if now.month % 3 == 0 else now.month / 3 + 1
#本周第一天和最后一天
this_week_start = now - timedelta(days=now.weekday())
this_week_end = now + timedelta(days=6-now.weekday())
  
#上周第一天和最后一天
last_week_start = now - timedelta(days=now.weekday()+7)
last_week_end = now - timedelta(days=now.weekday()+1)
  
#本月第一天和最后一天
this_month_start = datetime.datetime(now.year, now.month, 1)
this_month_end = datetime.datetime(now.year, now.month + 1, 1) - timedelta(days=1)
  
#上月第一天和最后一天
last_month_end = this_month_start - timedelta(days=1)
last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1)
  
#本季第一天和最后一天
month = (now.month - 1) - (now.month - 1) % 3 + 1
this_quarter_start = datetime.datetime(now.year, month, 1)
this_quarter_end = datetime.datetime(now.year, month + 3, 1) - timedelta(days=1)
  
#上季第一天和最后一天
last_quarter_end = this_quarter_start - timedelta(days=1)
last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1)
  
#本年第一天和最后一天
this_year_start = datetime.datetime(now.year, 1, 1)
this_year_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(days=1)
  
#去年第一天和最后一天
last_year_end = this_year_start - timedelta(days=1)
last_year_start = datetime.datetime(last_year_end.year, 1, 1)

最后

以上就是霸气乐曲为你收集整理的Python获取时间Python获取昨天,今天,明天,本周,上周,本月,上月,本季,本年,去年时间的全部内容,希望文章能够帮你解决Python获取时间Python获取昨天,今天,明天,本周,上周,本月,上月,本季,本年,去年时间所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(93)

评论列表共有 0 条评论

立即
投稿
返回
顶部