Find Monday's date with Python

前端 未结 8 1995
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 03:22

How do I find the previous Monday\'s date, based off of the current date using Python? I thought maybe I could use: datetime.weekday() to do it, but I am gettin

相关标签:
8条回答
  • 2020-12-13 03:51
    >>> import datetime
    >>> today = datetime.date.today()
    >>> today + datetime.timedelta(days=-today.weekday(), weeks=1)
    datetime.date(2009, 10, 26)
    

    Some words of explanation:

    Take todays date. Subtract the number of days which already passed this week (this gets you 'last' monday). Add one week.

    Edit: The above is for 'next monday', but since you were looking for 'last monday' you could use

    today - datetime.timedelta(days=today.weekday())
    
    0 讨论(0)
  • 2020-12-13 03:52

    I think the easiest way is using python-dateutil like this:

    from datetime import date
    from dateutil.relativedelta import relativedelta, MO
    
    today = date.today()
    last_monday = today + relativedelta(weekday=MO(-1))
    print last_monday
    
    0 讨论(0)
  • 2020-12-13 03:56

    You can use Natty. I tried parsedatetime and dateparser. Comparing these three, I think Natty is the best one.

    To get your result, use like this:

    >>> from natty import DateParser
    >>> dp = DateParser('last monday')
    >>> dp.result()
    [datetime.datetime(2016, 8, 1, 17, 35, 5, tzinfo=tzlocal())] #Today is 9th of August 2016 5.35 PM
    

    Github Link : https://github.com/eadmundo/python-natty

    Try it, It can do more!

    0 讨论(0)
  • 2020-12-13 03:57

    For future googlers who show up on this page looking for a way to get "the most recent Sunday", rather than "the most recent Monday", you need to do some additional math because datetime.weekday() treats Monday as the first day of the week:

    today = datetime.date.today()
    weekday = today.weekday() + 1
    start_day = today - datetime.timedelta(days=weekday % 7)
    

    If today is Tuesday, this sets start_day to last Sunday. If today is Sunday, this sets start_day to today. Take away the % 7 if you want "last Sunday" to be a week ago if it's currently Sunday.

    0 讨论(0)
  • 2020-12-13 03:59

    ChristopheD's post is close to what you want. I don't have enough rep to make a comment :(

    Instead of (which actually gives you the next upcoming monday):

    >>> today + datetime.timedelta(days=-today.weekday(), weeks=1)
    datetime.date(2009, 10, 26)
    

    I would say:

    >>> last_monday = today - datetime.timedelta(days=today.weekday())
    

    If you want the previous week, add the 'weeks=1' parameter.

    This makes the code more readable since you are subtracting a timedelta. This clears up any confusion caused by adding a timedelta that has negative and positive offsets.

    0 讨论(0)
  • 2020-12-13 04:00

    Using timedeltas and datetime module:

    import datetime
    datetime.date.today()+datetime.timedelta(days=-datetime.date.today().weekday())
    
    0 讨论(0)
提交回复
热议问题