-1

Looking forward to convert this date "2020-07-23 23:00:00.000"

First to ISO format and then to UTC

This is something i tried to convert this to ISO format, looking to convert this to UTC

def get_date_in_iso_utc(date_str):
    date_time_obj = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S.%f')    
    return date_time_obj.isoformat()
Roim
  • 2,986
  • 2
  • 10
  • 25
Cork Kochi
  • 1,783
  • 6
  • 29
  • 45
  • Could you please clarify: Which time zone does your input string represent? What is you desired output, a tz aware datetime object? Another string (in which format?)? – FObersteiner Jul 24 '20 at 05:05

2 Answers2

2

Use the pytz module, which comes with a full list of time zones + UTC. Figure out what the local timezone is, construct a timezone object from it, and manipulate and attach it to the iso_format datetime.

Source code, using local timezone Asia/Kolkata, for the string 2020-7-23 10:11:12:

import pytz, datetime

time_zone = pytz.timezone("Asia/Kolkata")
iso_format = datetime.datetime.strptime("2020-7-23 10:11:12", "%Y-%m-%d %H:%M:%S")
local_date_time = time_zone.localize(iso_format, is_dst=None)
utc_date_time = local_date_time.astimezone(pytz.utc)

To convert it to UTC timing, you need to do this:

final_data = utc_date_time.strftime("%Y-%m-%d %H:%M:%S")
print(final_data)

# OUTPUT
# >>> 2020-07-23 04:41:12

ADD

To find your specified timezone, you can do this via pytz only. Just use the below code

>>> pytz.all_timezones
>>> ['Africa/Abidjan',
 'Africa/Accra',
 'Africa/Addis_Ababa',
 ...]

Hope that answers your question

Alok
  • 8,452
  • 13
  • 55
  • 93
  • why would you suggest something that is a) not needed to answer the question and b) is likely to be deprecated in the future? - Python 3.9 will have [zoneinfo](https://docs.python.org/3.9/library/zoneinfo.html) and right now, [dateutil](https://dateutil.readthedocs.io/en/stable/) as fewer pitfalls, e.g. you don't need `localize`... – FObersteiner Jul 23 '20 at 18:44
  • 1
    Thanks @MrFuppes, noted your suggestions. Thanks for the inputs. It was my finding to be the answer for this question. Here, at SO we are free to give inputs, and the person will go with the best choices only. And since, I was not updated with the future thing, I found this as my best solution. That is how we learn :) – Alok Jul 23 '20 at 19:13
0

Converting to ISO format: Python - Convert string representation of date to ISO 8601

Convert from ISO to UTC: How do I translate an ISO 8601 datetime string into a Python datetime object? to convert to datetime and How to convert local time string to UTC? to convert datetime to UTC.

(I didn't mark it as duplicate as there is no single post that answeres both question. Please notice you could easily google the answer)

Roim
  • 2,986
  • 2
  • 10
  • 25
  • although I really like your *multi-dupe-referencing-answer* I think it is important to notice that the linked questions (their answers) show multiple approaches that can be confusing if you start out working with date and time in Python. Here, I think all you need is `datetime`. – FObersteiner Jul 23 '20 at 18:48
  • 1
    @MrFuppes Well then I'm glad we have all kind of answers here - a simple approach like in your answer and "just google it" like my answer. I think "first search, then ask" approach is best one when dealing with `datetime` :) – Roim Jul 23 '20 at 18:54