How could I get the local time, like right now its 9:06 For my friends, Its 6:06. How could I get that time In python. I have tried using DateTime but have had no luck finding a way to do this.
Asked
Active
Viewed 171 times
1
-
Does this answer your question? [How can I convert from UTC time to local time in python?](https://stackoverflow.com/questions/68664644/how-can-i-convert-from-utc-time-to-local-time-in-python) – jezza_99 Jan 17 '22 at 00:13
-
Welcome back to Stack Overflow. " I have tried using DateTime but have had no luck finding a way to do this." Did you try putting `python datetime local time` [into a search engine](https://duckduckgo.com/?q=python+datetime+local+time)? Since you want the current time, how about if you [add `current` to that](https://duckduckgo.com/?q=python+datetime+current+local+time)? When you looked at those search results, what code did you try writing? What went wrong with it? – Karl Knechtel Jan 17 '22 at 00:14
5 Answers
0
You can use pytz library.
from datetime import datetime
import pytz
datetime_NewYork = datetime.now(pytz.timezone('America/New_York'))
print("NY time:", datetime_NewYork.strftime("%H:%M:%S"))
datetime_London = datetime.now(pytz.timezone('Europe/London'))
print("London time:", datetime_London.strftime("%H:%M:%S"))
William
- 424
- 2
- 16
0
Are you trying to get your current time, or your friend's current time, if you want to get your current time try:
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time is :", current_time)
Just a word of advice, it took me 2 seconds to Google this, I understand you're new, though this kind of question may get you in trouble.
Callum
- 11
- 6
0
You can use time library, and localtime()/asctime() function.
import time
# This will give you o/p in the format of
# time.struct_time(tm_year=2022, tm_mon=1, tm_mday=16, tm_hour=19, tm_min=14, tm_sec=39, tm_wday=6, tm_yday=16, tm_isdst=0)
print(time.localtime())
# This will give you o/p in more simpler manner
# 'Sun Jan 16 19:15:01 2022'
print(time.asctime())
You can also use time.time(), but you have to do conversion afterwards.
Shashikant Dhuppe
- 55
- 1
- 9
-1
May be this could help.
from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
Kaka
- 1
- 1