1

I have two dates, start date and end date, where both needs to used in google tag manager in EST timezone using python.

Currently when I fetch the dates from jira using api, I am getting in the UTC format for start date, but for end date, getting only the date value, that too in class str type.

This is what I got from jira
start date: 2021-09-20T07:16:08.000+0000
end date: 2021-09-21

Now I need to convert these and create a tag using python gtm api, where the start and end date should be in EST timezone.

Time in start date should be 00:00:00 and in end date, it should be 23:59:59 (both the time in EST).

Anyone please help me on this?

Beginner
  • 143
  • 1
  • 12
  • basically, you convert string to datetime, make sure UTC is set as tzinfo and then convert to desired time zone using `astimezone`. but how is "*Time in start date should be 00:00:00 and in end date, it should be 23:59:59*" related to this? seems like a second question to me – FObersteiner Sep 22 '21 at 12:42
  • @MrFuppes basically in gtm on tag creation, start date and end date should be this value, for example , if the start date is 23-09-2021 and the end date is 24-09-2021, then the tag should be valid from 23-09-2021 00:00:00 till 24-09-2021 23:59:59. Using GTM api, we can pass the value in milliseconds only, so using python, I need to have the above datetime and then convert the same to milliseconds. – Beginner Sep 22 '21 at 13:01
  • Ok and by *time in milliseconds* you mean Unix time, i.e. ms since 1970-01-01 ? – FObersteiner Sep 22 '21 at 13:10
  • Please clarify via edits, not comments. – philipxy Sep 25 '21 at 19:58

1 Answers1

2

If im underderstanding correctly, it are both strings that you are getting back like this?

startdate = "start date: 2021-09-20T07:16:08.000+0000"
enddate = "end date: 2021-09-21"

Then first what you want to do, is split on the spaces and select the last item

justStartDatetimeString = startdate.split(" ")[-1]
justEndDatetimeString = enddate.split(" ")[-1]

If you just get the datetime as string like this, ignore part above:

"2021-09-20T07:16:08.000+0000"

Now just parse it towards a datetime by using the dateutil.parser

from datetime import datetime, time, timezone
import dateutil.parser 
startdateDateTime = dateutil.parser.isoparse(justStartDatetimeString)
startdateDateTime = startdateDateTime.replace(tzinfo=timezone.utc).astimezone(tz=dateutil.tz.gettz('US/Eastern'))
startdateDateTime = startdateDateTime.replace(hour=0, minute=0, second=0)

For the enddate string

enddateDateTime = dateutil.parser.isoparse(justEndDatetimeString)
enddateDateTime = enddateDateTime.replace(tzinfo=dateutil.tz.gettz('US/Eastern'))astimezone(tz=dateutil.tz.gettz('US/Eastern'))
enddateDateTime = enddateDateTime.replace(hour=23, minute=59, second=59)
daydraze
  • 36
  • 4