【问题标题】:How to generate even calendar dates?如何生成偶数日历日期?
【发布时间】:2018-11-01 00:12:57
【问题描述】:

有谁知道如何在 python(或其他平台)的日历中生成一个列表,其中包含从 2018 年到 2021 年的“偶数天”、月份和年份?

例子:

Sun, 02 Jan 2019
Tue, 04 Jan 2019
Thur, 06 Jan 2019
Sat, 08 Jan 2019
Sun, 10 Jan 2019
Tue, 12 Jan 2019
Thur, 14 Jan 2019
Sat, 16 Jan 2019
Sun, 18 Jan 2019
Tue, 20 Jan 2019
Thur, 22 Jan 2019

依此类推,直到 2021 年。

编辑:

如何在 python 中生成 2018 年到 2022 年之间的日历列表,有 2 种格式:

星期几,日期月年时间(时:分:秒)-年-月-日时间(时:分:秒)

注意: 日期:仅限同行日期 schedule:随机生成的时间表

例子:

Tue, 02 Jan 2018 00:59:23  -   2018-01-02  00:59:23
Thu, 04 Jan 2018 10:24:52  -   2018-01-04  10:24:52
Sat, 06 Jan 2018 04:11:09  -   2018-01-06  04:11:09
Mon, 08 Jan 2018 16:12:40  -   2018-01-08  16:12:40
Wed, 10 Jan 2018 10:08:15  -   2018-01-10  10:08:15
Fri, 12 Jan 2018 07:10:09  -   2018-01-12  07:10:09
Sun, 14 Jan 2018 11:50:10  -   2018-01-14  11:50:10
Tue, 16 Jan 2018 02:29:22  -   2018-01-16  02:29:22
Thu, 18 Jan 2018 19:07:20  -   2018-01-18  19:07:20
Sat, 20 Jan 2018 08:50:13  -   2018-01-20  08:50:13
Mon, 22 Jan 2018 02:40:02  -   2018-01-22  02:40:02

以此类推,直到 2022 年……

【问题讨论】:

  • 在编辑中的后续问题中,时间值的来源是什么——它来自哪里?

标签: python calendar


【解决方案1】:

这里有一个相当简单的方法,似乎可以工作并处理闰年:

from calendar import isleap
from datetime import date

# Days in each month (1-12).
MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

def dim(year, month):
    """ Number of days in month of the given year. """
    return MDAYS[month] + ((month == 2) and isleap(year))

start_year, end_year = 2018, 2021

for year in range(start_year, end_year+1):
    for month in range(1, 12+1):
        days = dim(year, month)
        for day in range(1, days+1):
            if day % 2 == 0:
                dt = date(year, month, day)
                print(dt.strftime('%a, %d %b %Y'))

输出:

Tue, 02 Jan 2018
Thu, 04 Jan 2018
Sat, 06 Jan 2018
Mon, 08 Jan 2018
Wed, 10 Jan 2018
Fri, 12 Jan 2018
Sun, 14 Jan 2018
Tue, 16 Jan 2018
...

编辑:

这是一种方法来做(我认为)您在后续问题中询问的方法:

from calendar import isleap
from datetime import date, datetime, time
from random import randrange

# Days in each month (1-12).
MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

def dim(year, month):
    """ Number of days in month of the given year. """
    return MDAYS[month] + ((month == 2) and isleap(year))

def whenever():
    """ Gets the time value. """
    # Currently just returns a randomly selected time of day.
    return time(*map(randrange, (24, 60, 60)))  # hour:minute:second

start_year, end_year = 2018, 2021

for year in range(start_year, end_year+1):
    for month in range(1, 12+1):
        days = dim(year, month)
        for day in range(1, days+1):
            if day % 2 == 0:
                dt, when = date(year, month, day), whenever()
                dttm = datetime.combine(dt, when)
                print(dt.strftime('%a, %d %b %Y'), when, '-', dttm)

输出:

Tue, 02 Jan 2018 00:54:02 - 2018-01-02 00:54:02
Thu, 04 Jan 2018 10:19:51 - 2018-01-04 10:19:51
Sat, 06 Jan 2018 22:48:09 - 2018-01-06 22:48:09
Mon, 08 Jan 2018 06:48:46 - 2018-01-08 06:48:46
Wed, 10 Jan 2018 14:01:54 - 2018-01-10 14:01:54
Fri, 12 Jan 2018 05:42:43 - 2018-01-12 05:42:43
Sun, 14 Jan 2018 21:42:37 - 2018-01-14 21:42:37
Tue, 16 Jan 2018 08:08:39 - 2018-01-16 08:08:39
...

【讨论】:

  • 嗨,谢谢你的帮助,这对我有用。现在,最后一个问题,但是您将如何以这种方式组织日历(日期/月/日(偶数)和时间(小时/分钟和秒以随机方式显示,随机时间)。示例: 2019-01-02 01:51:44 , 2019-01-04 17:23:20 , 2019-01-06 01:11:16 , 2019-01-08 20:15:11 等等跨度>
  • 对不起,我不太明白你的后续问题。无论如何,如果我的回答对您有所帮助,请考虑接受(或至少投票)它。见What should I do when someone answers my question?
  • @Luiz:谢谢。如果您可以详细说明您的后续问题,也许我也可以提供帮助。注意:您也可以将其添加到原始问题的末尾(并让我知道您已经这样做了)。
  • 您好,朋友,它成功了。我无法用言语来形容你的帮助。谢谢!
  • Luiz:这些话很好——很高兴能帮上忙。
【解决方案2】:

怎么样:

import datetime

d = datetime.date.today()                       # Define Start date
while d.year <= 2021:                           # This will go *through* 2012
        if d.day % 2 == 0:                      # Print if even date
                print(d.strftime('%a, %d %b %Y'))
        d += datetime.timedelta(days=1)         # Jump forward a day
2018 年 10 月 31 日,星期三 2018 年 11 月 2 日星期五 2018 年 11 月 4 日,星期日 2018 年 11 月 6 日,星期二 2018 年 11 月 8 日星期四 2018 年 11 月 10 日,星期六 2018 年 11 月 12 日,星期一 2018 年 11 月 14 日,星期三 2018 年 11 月 16 日星期五 2018 年 11 月 18 日,星期日 2018 年 11 月 20 日,星期二 2018 年 11 月 22 日星期四 ... 2021 年 12 月 24 日星期五 2021 年 12 月 26 日,星期日 2021 年 12 月 28 日,星期二 2021 年 12 月 30 日星期四

【讨论】:

  • 我认为这种策略可能会在闰年打破。 2 月 28 日 + 2 日(闰年)是 3 月 1 日。
  • 哦,那是甚至天,不是每隔一天——我误读了这个问题——我会更新
  • 嗨,谢谢你的帮助,这对我有用。现在,最后一个问题,但是您将如何以这种方式组织日历(日期/月/日(偶数)和时间(小时/分钟和秒以随机方式显示,随机时间)。示例: 2019-01-02 01:51:44 , 2019-01-04 17:23:20 , 2019-01-06 01:11:16 , 2019-01-08 20:15:11 等等.. .
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-19
  • 1970-01-01
  • 1970-01-01
  • 2019-07-07
  • 1970-01-01
相关资源
最近更新 更多