Reputation: 41
I am working on my python code to generate the date format so I can be able to search for the data into the database, example: 20180222010000
.
I have got a bit of trouble with the code that I am currently using because when I run the code, it should have adding the date to the next day date when I set the self.program_day
to day 1
from 0
when my current time show between 21:00 to 06:00. On my code it will only show the same day date, example: today date is 22 so if I set self.program_day
to 1 then it will still show today date 22 which it should have been 23. If I want to set the date to the next day, I would have to fire my code again so I can be able to add each day date to the 24, 25, 26 and so on after when I have to run my code twice.
Here is the code:
def get_next_day(self, time_string):
to_datetime = datetime.datetime.fromtimestamp(time.mktime(time.strptime(time_string, '%Y%m%d')))
nextday = to_datetime + datetime.timedelta(days = self.program_day)
return nextday.strftime('%Y%m%d')
next_day = time.strftime("%Y%m%d")
next_day = self.get_next_day(next_day)
get_minutes = time.strftime("%M")
next_day = str(next_day)
program_start_date = ''
if self.program_day == 5 and int(get_hour) >= 20 and int(get_hour) <= 23:
pass
else:
self.program_day += 1
for channels in channels_list:
if str(get_hour) == '00':
program_start_date = str(next_day + '00' + get_minutes + '00')
elif str(get_hour) == '01':
program_start_date = str(next_day + '01' + get_minutes + '00')
elif str(get_hour) == '02':
program_start_date = str(next_day + '02' + get_minutes + '00')
elif str(get_hour) == '03':
program_start_date = str(next_day + '03' + get_minutes + '00')
elif str(get_hour) == '04':
program_start_date = str(next_day + '04' + get_minutes + '00')
elif str(get_hour) == '05':
program_start_date = str(next_day + '05' + get_minutes + '00')
elif str(get_hour) == '06':
program_start_date = str(next_day + '06' + get_minutes + '00')
elif str(get_hour) == '07':
program_start_date = str(next_day + '07' + get_minutes + '00')
elif str(get_hour) == '08':
program_start_date = str(next_day + '08' + get_minutes + '00')
elif str(get_hour) == '09':
program_start_date = str(next_day + '09' + get_minutes + '00')
elif str(get_hour) == '10':
program_start_date = str(next_day + '10' + get_minutes + '00')
elif str(get_hour) == '11':
program_start_date = str(next_day + '11' + get_minutes + '00')
elif str(get_hour) == '12':
program_start_date = str(next_day + '12' + get_minutes + '00')
elif str(get_hour) == '13':
program_start_date = str(next_day + '13' + get_minutes + '00')
elif str(get_hour) == '14':
program_start_date = str(next_day + '14' + get_minutes + '00')
elif str(get_hour) == '15':
program_start_date = str(next_day + '15' + get_minutes + '00')
elif str(get_hour) == '16':
program_start_date = str(next_day + '16' + get_minutes + '00')
elif str(get_hour) == '17':
program_start_date = str(next_day + '17' + get_minutes + '00')
elif str(get_hour) == '18':
program_start_date = str(next_day + '18' + get_minutes + '00')
elif str(get_hour) == '19':
program_start_date = str(next_day + '19' + get_minutes + '00')
elif str(get_hour) == '20':
program_start_date = str(next_day + '20' + get_minutes + '00')
elif str(get_hour) == '21':
program_start_date = str(next_day + '21' + get_minutes + '00')
elif str(get_hour) == '22':
program_start_date = str(next_day + '22' + get_minutes + '00')
elif str(get_hour) == '23':
program_start_date = str(next_day + '23' + get_minutes + '00')
print "program_start_date for......................next_day"
print program_start_date
Here is the program_start_date:
program_start_date for......................next_day
20180222003000
program_start_date for......................next_day
20180222003000
program_start_date for......................next_day
20180222003000
program_start_date for......................next_day
20180222003000
program_start_date for......................next_day
20180222003000
program_start_date for......................next_day
20180222003000
program_start_date for......................next_day
20180222003000
What I am trying to achieve is I want to set up the next_day
date to something is like 20180223
. I have to use the if statements so I can set up the date, time and minutes to make it to show like 20180222010000
.
Can you please show me an example how I can set to the next day date when I am using with next_day
variable?
Upvotes: 1
Views: 1570
Reputation: 21
from datetime import datetime
from dateutil.relativedelta import relativedelta
date_now = datetime.now().date()
start_date = date_now + relativedelta(days=+1)
This will be allow and success. I test it in python3.
Hope its help you
Upvotes: 1