0

I have a datetime filed in my models.py and another field that saves the selected timezone as well (like Asia/Tehran). I need to append the the utc info of this timezone to my datetime object like this: '20/4/2021 18:33:00 Gmt+4:30 How can i do that?

zoldxk
  • 2,632
  • 1
  • 7
  • 29
Niloofar
  • 240
  • 1
  • 14

1 Answers1

1

Use this code, replace the numbers with your specified date in your GMT+4.5 timezone.

import datetime
timezone_diff = datetime.timedelta(hours=4.5)   
GMT_timezone = datetime.timezone(timezone_diff, name="GMT")
GMT_time = datetime.datetime(2017,2,14,12,15,1,99,GMT_timezone,fold=1)

print('{0:%d}/{0:%m}/{0:%Y} {0:%H:%M:%S} {0:%Z}{0:%z}'.format(GMT_time))

See results: enter image description here

Eddy Piedad
  • 336
  • 1
  • 8
  • Tnx but how can create that GMT+1 according to my timezone field?this field can get different timezones – Niloofar Apr 28 '21 at 16:54
  • First, know that GMT and UTC are the same, they differ on some application. See: https://stackoverflow.com/questions/48942916/difference-between-utc-and-gmt If you plan to convert your timezone, in Tehran, you are GMT+4:30, you should make some adjustment. Let me see if I can code for you. – Eddy Piedad Apr 28 '21 at 17:15
  • Modified the code with your preferred conversion. – Eddy Piedad Apr 28 '21 at 17:31
  • Your welcome. Don't forget to upvote the answer. Thanks as well. – Eddy Piedad Apr 29 '21 at 10:15