I have an array of time in UTC as follows:
Array = [0,6,12,18,24,30,36,42,48,54,60,66,72,78,84,90,96,102,108,114,120,126]
I want corresponding date and time in string format.
For example: If starting date(say) is 28-04-2019
, the date and time for 0, 6, 12, 18
will be 28-04-2019:0, 28-04-2019:6,28-04-2019:12 and 28-04-2019:18
. And similarly for 24, 30,36 and 42
should be 29-04-2019:00, 29-04-2019:06, 29-04-2019:12, 30-04-2019:18
and so on.
Given you are new to SO, I'm cutting you some slack. But To start I would like to point out a couple of issues with your question and what you have shown.
Okay now for a solution:
import datetime as dt
from dateutil import parser, relativedelta
from dateutil.utils import default_tzinfo
def gen_date(stDate: str, ofst: int) -> str:
std = parser.parse(stDate + ' 00:00 UTC') #Makes TZ aware timestamp
newdt = std + relativedelta.relativedelta(hours=ofst)
return newdt.strftime("%m-%d-%Y:%H %Z") #Returns TZ aware string
offset_lst = [0,6,12,18,24,30,36,42,48,54,60,66,72,78,84,90,96,102,108,114,120,126]
std = '28-04-2019'
for ofst in offset_lst:
print(f"Start Date {std} + Offset {ofst} hours -> {gen_date(std, ofst)}")
The above yields:
Start Date 28-04-2019 + Offset 0 hours -> 04-28-2019:00 UTC
Start Date 28-04-2019 + Offset 6 hours -> 04-28-2019:06 UTC
Start Date 28-04-2019 + Offset 12 hours -> 04-28-2019:12 UTC
Start Date 28-04-2019 + Offset 18 hours -> 04-28-2019:18 UTC
Start Date 28-04-2019 + Offset 24 hours -> 04-29-2019:00 UTC
Start Date 28-04-2019 + Offset 30 hours -> 04-29-2019:06 UTC
Start Date 28-04-2019 + Offset 36 hours -> 04-29-2019:12 UTC
Start Date 28-04-2019 + Offset 42 hours -> 04-29-2019:18 UTC
Start Date 28-04-2019 + Offset 48 hours -> 04-30-2019:00 UTC
Start Date 28-04-2019 + Offset 54 hours -> 04-30-2019:06 UTC
Start Date 28-04-2019 + Offset 60 hours -> 04-30-2019:12 UTC
Start Date 28-04-2019 + Offset 66 hours -> 04-30-2019:18 UTC
Start Date 28-04-2019 + Offset 72 hours -> 05-01-2019:00 UTC
Start Date 28-04-2019 + Offset 78 hours -> 05-01-2019:06 UTC
Start Date 28-04-2019 + Offset 84 hours -> 05-01-2019:12 UTC
Start Date 28-04-2019 + Offset 90 hours -> 05-01-2019:18 UTC
Start Date 28-04-2019 + Offset 96 hours -> 05-02-2019:00 UTC
Start Date 28-04-2019 + Offset 102 hours -> 05-02-2019:06 UTC
Start Date 28-04-2019 + Offset 108 hours -> 05-02-2019:12 UTC
Start Date 28-04-2019 + Offset 114 hours -> 05-02-2019:18 UTC
Start Date 28-04-2019 + Offset 120 hours -> 05-03-2019:00 UTC
Start Date 28-04-2019 + Offset 126 hours -> 05-03-2019:06 UTC
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.