Python日期范围基于当前日期

时间:2017-05-26 18:31:15

标签: python-3.x date

我希望根据日期的用户输入搜索文件。例如,我正在尝试编写脚本以询问用户搜索范围(月到日,最后一周或特定日)。

最后一整周需要倒退,到最后一整周 - 所以如果今天是星期三,剧本应该回到前一个星期日(星期日)作为刚开始的星期六的开始范围,同时还考虑到目前的哪一天:

太阳(开始)--- ---星期一星期二星期三--- --- ---星期四星期五---的周六(结束) - -Sun --- Mon ---星期二--- 星期三(今天)

然而,它还需要考虑与上述相关的日期,这意味着无论“今天”是什么,搜索标准总是落后一整周(如果是星期日,它只会持续到最后一周)星期天到'昨天,星期六')

从尝试类似事情的一些示例中我看到herehere,我试图在过去几天加入,修改和添加:

import datetime
import os

import dateutil.relativedelta

import timedelta


class AuditFileCheck():
    """File Compliance Checker."""

    def datechoice(self):
        """Select date."""
        print("Checking the Audit Files for compliance.")
        print("Today is", datetime.date.today().strftime(" %A."))
        print("\nI will check either for file compliances."
              "\nSearch criteria is either by MONTH to date, last full WEEK, "
              "or individual DAY: [M/W/D]")

        print(now.strftime('Week of %Y%m%d'))
        weekbefore = now - timedelta(days=6)
        print(
            "Week of {weekbefore:%A, %m-%d-%Y} to {now:%A, %m-%d-%Y}").format(**vars())

    input_search = input(
        "Enter search range: Month to date, Prior Week, or by day [M/W/D]")

    def search_month(d, w, m, weekday, month):
        """Establish search from month start, or prior month if today is first of current month."""
        if input_search.lower() == "m":
            current_month = datetime(today.month, 1, today.year)
            if current_month == datetime.today():
                current_month == dateutil.relativedelta.relativedelta(
                    months=-1)
            return m - datetime.timedelta(current_month)
        m = current_month()
        print(current_month)
        # TODO ensure the prior month is used if 'today' is before the end of
        # first full week in current month
        if input_search.lower() == "w":
            prior_week = weekday + d.weekday()
            if prior_week >= 0:  # Target day already happened this week
                prior_week -= 6
            return d - datetime.timedelta(prior_week)
        d = datetime.date.today()
        # 6 = Sunday, 0 = Monday, 1 = Tuesday...
        previous_monday = previous_weekday(d, 6)
        print(previous_monday)
        # TODO search files
        if input_search.lower() == "d":
            day_search = input(
                "Enter a specific date within to search [YYYYMMDD]")
            return d
        print("Searching through...")
        # TODO search files from set_day

这一位:

previous_sunday = previous_weekday(d, 8)

调整整数会调整它看起来的距离。

我在使其正常运行方面遇到了一些麻烦。我在这做错了什么?我试图玩它越多,我变得越困惑,它就越少......

0 个答案:

没有答案