“cara mengulangi tanggal dalam python” Kode Jawaban

Python iterate melalui rentang tanggal

from datetime import timedelta, date

def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)

start_date = date(2013, 1, 1)
end_date = date(2015, 6, 2)
for single_date in daterange(start_date, end_date):
    print(single_date.strftime("%Y-%m-%d"))
Testy Teira

cara mengulangi tanggal dalam python

import datetime

# The size of each step in days
day_delta = datetime.timedelta(days=1)

start_date = datetime.date.today()
end_date = start_date + 7*day_delta

for i in range((end_date - start_date).days):
    print(start_date + i*day_delta)
Modern Manx

cara mengulang semua tanggal dalam python

# this DOES account for leap years
years = ['2023', '2024', '2028']
months = ['01', '02', '03', '04', '05', '06',
              '07', '08', '09', '10', '11', '12']
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

for i in range(len(years)):
    for j in range(len(months)):
        if (((int(years[i]) % 4 == 0 and int(years[i]) % 100 != 0) or (int(years[i])% 400 == 0)) and months[j] == '02'):
            for k in range(1, days[j] + 2):
                print(years[i] + '-' + months[j] + '-' + str(k))
        else:
            for k in range(1, days[j] + 1):
                print(years[i] + '-' + months[j] + '-' + str(k))
            

#2023-01-1
#2023-01-2
#2023-01-3
#...
liamxaro

cara mengulang semua tanggal dalam python

#this does not account for leap years
years = [str(i) for i in range(2010, 2022)]
months = ['01', '02', '03', '04', '05', '06',
              '07', '08', '09', '10', '11', '12']
days = [31, 28 , 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

for i in range(len(years)):
  for j in range(len(months)):
    for k in range(1, days[j] + 1):
      print("The date is: " + years[i] + months[j] + str(k))
      



liamxaro

Jawaban yang mirip dengan “cara mengulangi tanggal dalam python”

Pertanyaan yang mirip dengan “cara mengulangi tanggal dalam python”

Lebih banyak jawaban terkait untuk “cara mengulangi tanggal dalam python” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya