You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何以YYYY-MM-DD格式打印10个从今日起间隔两周的日期

Generate 10 Dates Spaced Two Weeks Apart (Starting from Today)

To generate 10 dates starting from the current date, with each date two weeks (14 days) apart and formatted as YYYY-MM-DD, you can use Python's datetime module. Here's the corrected and complete code:

import datetime

# Get today's date (without time component)
current_date = datetime.date.today()

# Generate and print 10 dates, each 2 weeks apart
for i in range(10):
    # Calculate the date by adding i * 14 days to today's date
    target_date = current_date + datetime.timedelta(days=i * 14)
    # Print the date in YYYY-MM-DD format
    print(target_date)

How this works:

  • datetime.date.today() gives us the current date (e.g., 2024-05-20) without any time information, which is exactly what we need for the YYYY-MM-DD format.
  • datetime.timedelta(days=i * 14) creates a time interval of i multiplied by 14 days. For i=0, this adds 0 days (so we get today's date), i=1 adds 14 days, up to i=9 which adds 126 days (9 weeks later).
  • Printing the target_date object directly outputs it in the YYYY-MM-DD format since that's the default string representation of a date object in Python.

If you prefer to start from the first date two weeks from today instead of including today, just adjust the loop to start at 1 instead of 0:

for i in range(1, 11):
    target_date = current_date + datetime.timedelta(days=i * 14)
    print(target_date)

内容的提问来源于stack exchange,提问作者Nitish Attri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:10:37