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

#coding=utf-8

  

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 + 11- 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 + 31- 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 - 21)

  

#本年第一天和最后一天

this_year_start = datetime.datetime(now.year, 11)

this_year_end = datetime.datetime(now.year + 111- timedelta(days=1)

  

#去年第一天和最后一天

last_year_end = this_year_start - timedelta(days=1)

last_year_start = datetime.datetime(last_year_end.year, 11)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值