1

i'm trying to convert any timezone time to uk time. i'm getting the value error in the time_format variable.

from datetime import datetime
from pytz import timezone

newyork_tz = timezone('America/New_York')
london_tz = timezone('Europe/London')

it is in type string and when i'm trying to convert to int it is giving me this error

ValueError: invalid literal for int() with base 10: '2022, 02, 02, 21, 32, 25'

expecting out like below with type int

2022, 02, 02, 21, 28, 15

i'm new to python plz guide me with possible solution

4
  • That's not a string, it's a tuple. A string would have quotes around it. Commented Feb 3, 2022 at 2:35
  • Also, your desired output 2022, 02, 02, 21, 28, 15 is not an int. Did you mean a list of ints? Commented Feb 3, 2022 at 2:36
  • @Barmar. i've update the code with complete example. plz check Commented Feb 3, 2022 at 2:44
  • @j1-lee i'm trying to pass 2022, 02, 02, 21, 28, 15 to time_format variable Commented Feb 3, 2022 at 2:45

1 Answer 1

1

Use strptime() to parse a datetime. It uses the same format string as strftime().

newyork = newyork_tz.localize(datetime.strptime(time_format, format))
Sign up to request clarification or add additional context in comments.

3 Comments

thanks it worked for me. i'm getting output as 2022-02-03 02:57:02+00:00. but can I get only the hours and minutes. something like 02:57
You can format the output however you like, using newyork.strftime('%H:%M').
thank you very much. I really appreciate you help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.