How To Do A Loop That Takes Into Account Each Day Of A Specific Month?
got a weird one i can not figure out how to solve basically i have to run a section of code, this code extracts data from a file, which name is the format year-month-day-hour-00-00
Solution 1:
Python has a datetime
module that will handle this for you. You make a datetime
object representing the date and time, and a timedelta
object to represent the addition of 1 hour. You can then, if you need to, check whether the new date is within the same day as the original date by comparing the .day
property of the datetime
object.
datetime
also has a convenient method strftime
for printing formatted output based on the date and time.
from datetime import datetime, timedelta
defcalculate_and_write_hsdir(h,d,m,y):
before = datetime(hour = h, day = d, month = m, year = y)
now = before + timedelta(hours = 1)
if before.day == now.day:
print'still the same day'# ... do stuff ...else:
print"now it's a different day"# ... do other stuff ... print now.strftime('%Y-%m-%d-%H-00-00-consensus')
h = 00#Hour
d = 01 #Day
m = 10#Month
y = 2013#Year
calculate_and_write_hsdir(h, d, m, y)
Post a Comment for "How To Do A Loop That Takes Into Account Each Day Of A Specific Month?"