Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have a list of hours between two dates in python

I have two times and I want to make a list of all the hours between them using the same format in Python

from= '2016-12-02T11:00:00.000Z'
to= '2017-06-06T07:00:00.000Z'
hours=to-from

so the result will be something like this 2016-12-02T11:00:00.000Z 2016-12-02T12:00:00.000Z 2016-12-02T13:00:00.000Z ..... and so on How can I so this and what kind of plugin should I use?

like image 782
M.Alsioufi Avatar asked Dec 08 '22 17:12

M.Alsioufi


1 Answers

If possible I would recommend using pandas.

import pandas
time_range = pandas.date_range('2016-12-02T11:00:00.000Z', '2017-06-06T07:00:00.000Z', freq='H')

If you need strings then use the following:

timestamps = [str(x) + 'Z' for x in time_range]
# Output
# ['2016-12-02 11:00:00+00:00Z',
#  '2016-12-02 12:00:00+00:00Z',
#  '2016-12-02 13:00:00+00:00Z',
#  '2016-12-02 14:00:00+00:00Z',
#  '2016-12-02 15:00:00+00:00Z',
#  '2016-12-02 16:00:00+00:00Z',
#  ...]
like image 152
aquil.abdullah Avatar answered Jan 05 '23 02:01

aquil.abdullah