如何获取上周三的 Python 日期对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2381786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:31:12  来源:igfitidea点击:

How to get the Python date object for last Wednesday

pythondatetimedate

提问by roktechie

Using Python I would like to find the date object for last Wednesday. I can figure out where today is on the calendar using isocalendar, and determine whether or not we need to go back a week to get to the previous Wednesday. However, I can't figure out how to create a new date object with that information. Essentially, I need to figure out how to create a date from an iso calendar tuple.

使用 Python 我想找到上周三的日期对象。我可以使用 isocalendar 找出今天在日历上的位置,并确定我们是否需要返回一周才能到达上一个星期三。但是,我无法弄清楚如何使用该信息创建新的日期对象。本质上,我需要弄清楚如何从 iso 日历元组创建日期。

from datetime import date
today = date.today()
if today.isocalendar()[2] > 3: #day of week starting with Monday
    #get date for Wednesday of last week
else:
    #get date for Wednesday of this current week

回答by DisplacedAussie

I think you want this. If the specified day is a Wednesday it will give you that day.

我想你想要这个。如果指定的日期是星期三,它会给你那一天。

from datetime import date
from datetime import timedelta

today = date.today()
offset = (today.weekday() - 2) % 7
last_wednesday = today - timedelta(days=offset)

Example, the last wednesday for every day in March:

例如,三月每一天的最后一个星期三:

for x in xrange(1, 32):
    today = date(year=2010, month=3, day=x)
    offset = (today.weekday() - 2) % 7
    last_wednesday = today - timedelta(days=offset)

    print last_wednesday

回答by John Machin

Assuming that "last Wednesday" can't be the same as "today", this shows how to do it for any day of the week:

假设“上周三”不能与“今天”相同,这显示了如何在一周中的任何一天执行此操作:

>>> from datetime import date
>>> from datetime import timedelta
>>>
>>> MON, TUE, WED, THU, FRI, SAT, SUN = range(7)
>>>
>>> def lastWday(adate, w):
...     """Mon:w=0, Sun:w=6"""
...     delta = (adate.weekday() + 6 - w) % 7 + 1
...     return adate - timedelta(days=delta)
...
>>> for x in range(8, 16):
...     start = date(year=2010, month=3, day=x)
...     prev = lastWday(start, WED)
...     print start, start.weekday(), prev, prev.weekday()
...
2010-03-08 0 2010-03-03 2
2010-03-09 1 2010-03-03 2
2010-03-10 2 2010-03-03 2
2010-03-11 3 2010-03-10 2
2010-03-12 4 2010-03-10 2
2010-03-13 5 2010-03-10 2
2010-03-14 6 2010-03-10 2
2010-03-15 0 2010-03-10 2

回答by Roman A. Taycher

read http://docs.python.org/library/datetime.html

阅读http://docs.python.org/library/datetime.html

Write your own function using date2 = date1 - timedelta(days=1) and date.isoweekday() iterating over previous days while isoweek is not equal to 3(Wednesday)

使用 date2 = date1 - timedelta(days=1) 和 date.isoweekday() 迭代前几天编写自己的函数,而 isoweek 不等于 3(Wednesday)

回答by Will McCutchen

I'm not sure if this meets your requirements, but it should get you the Wednesday closest to a given datethe Wednesday in the same week as the given date, assuming weeks start on Monday:

我不确定这是否符合您的要求,但它应该让您在与给定日期同一周的星期三最接近给定日期的星期三,假设几周从星期一开始:

import datetime

def find_closest_wednesday(date):
    WEDNESDAY = 3
    year, week, day  = date.isocalendar()
    delta = datetime.timedelta(days=WEDNESDAY-day)
    return date + delta

today = datetime.date.today()
print 'Today: ', today
print 'Closest Wendesday: ', find_closest_wednesday(today)