This page demonstrates Python tips and tricks that I use in my everyday programming as an atmospheric science graduate student.
-Brian Blaylock

Friday, January 9, 2015

Loop through dates

I often want to loop through set of dates.

#Loop through a set of dates

#Import datetime and time functions
from datetime import datetime, timedelta

#Start Date
current_time = datetime(2015,01,01)

#Loop until a specified date is reached
while current_time < datetime(2015,01,30):
     #Do whatever you want to after getting the date
     string_time = current_time.strftime("%Y%m%d_%H%M")
     print string_time
     #add one minute and do the next date

     current_time = current_time+timedelta(minutes=1)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.