-1

I have some date in a .ini "yyyymmdd" format.

my .ini files details:

[global]
date_with_interval = 20210311:3, 20200228:4

In this above variable, I have two values with colon-separated 1st one is the date and 2nd interval so based on the interval value in need to fetch a sequence of date like

#I need the output in this format

output = [[20210311,20210312,20210312],[20200228,20200229,20200301,20200302]]

I have tried in this way but miss some points.

import configparser
config_file= "file1.ini"
con.read(config_file)

def read(name):
    return con.get('global', name)

valid_date = []
data_date = read("date_with_interval")
date_info = data_date.split(",")
for date in date_info:
    date_format,interval = date.split(":")
    print("Date is:",date_format)
    print("Interval",interval)
    year,month,dd = date_format[0:4],date_format[4:6],date_format[6:8]
    print("year",year)
    print("month",month)
    print("date", dd)
abhi spdy
  • 25
  • 5

1 Answers1

0

Probably want to use the datetime module to add timedeltas as such:

import datetime
date_with_interval = ['20210311:3', '20200228:4']


output = []
for x in date_with_interval:
    t = []
    d, n = x.split(':')
    n = int(n)
    for i in range(n):
        t.append(datetime.datetime.strftime(datetime.datetime.strptime(d, "%Y%m%d")+datetime.timedelta(days=i), "%Y%m%d"))
    output.append(t)

Output

[['20210311', '20210312', '20210313'],
 ['20200228', '20200229', '20200301', '20200302']]
Chris
  • 15,819
  • 3
  • 24
  • 37