6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Python】指定間隔を5分区切りにする【datetime】

Last updated at Posted at 2017-07-09

指定間隔の時間を5分区切りにする


from datetime import datetime, timedelta

def split_span_by_5min(start_time, end_time):
    """
    start_time, end_time 間を5分毎に区切ったリストにして返す
    (end_timeは含まない)
    param start_time: datetime.datetime
    param end_time:   datetime.datetime
    return list = [datetime.datetime,..]
    """
    span_list = list()
    handle_time = start_time
    while(True):
        span_list.append(handle_time)
        next_time = handle_time + timedelta(minutes=5)
        if next_time < end_time:
            handle_time = next_time
        else:
            break
    
    return span_list
6
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?