Skip to content
Sign UpLog In
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
39

How To Get The Time! ⏰ [python]

Bookie0
Bookie0

Heyyy everyone! xD

here is a tutorial for adding the time of multiple cities in your programs, using datetime and pytz.

I have included two versions; one of them where you have to manually change it in the code, and another where you ask the user!

1st method:

from datetime import datetime import pytz tz_city = pytz.timezone('country/city') # replace 'country' and 'city' with your choice # for cities with two words, add an underscore in between them datetime_city = datetime.now(tz_city) # prints the time, in hours, minutes, and seconds. print("Time:", datetime_city.strftime("%H:%M:%S"))

2nd method

the second method is a bit more complex, its the repl attached below. there are many comments to guide you.

it includes:

  • a input to ask the user the continent/city of their choice
  • tells you if its PM or AM
  • has an error message if the user puts a typo/not existing/etc. (using try and except)

anyways, if you have any questions, please ask them and I will try to answer them.

feel free to comment suggestions, tips, chatter, etc.

And if it helped you, Upvoting is Caring! ;)

Have a super-duper day!

disclaimer: not all cities are displayed, but many are (major cities, capitals, high population, etc.)
and oceania doesnt work :/
2 years ago

Voters

Comments

TopNew
3
1
Bookie0
Bookie0

thanks! =)

@BobTheTomatoPie

2 years ago
3
firefish
firefish

Surprisingly accurate! (Note the time in the bottom left corner)
image


btw, time.time() is an option

2 years ago
2
Bookie0
Bookie0

thanks! also i think you meant bottom -right_ corner xD)

@johnstev111

2 years ago
1
Bookie0
Bookie0

also i didnt know of time.time(), but can it find the time of many different cities?

@johnstev111

2 years ago
1
firefish
firefish

@Bookie0
I do... LOL!
grilling apricots on the barbecue

2 years ago
1
firefish
firefish

@Bookie0
time.time() gets the currrent unix epoch timestamp in UTC

2 years ago
2
Bookie0
Bookie0

ah, so unlike what i did not, not loads of cities just one ok

@johnstev111

2 years ago
1
firefish
firefish

@Bookie0
It's UTC, which is GMT, which is British time when daylight savings isn't on

2 years ago
2
adl212
adl212

Nice! I have never really needed time in my program, but now I might add it to my programs!

2 years ago
1
Wilke000
Wilke000

Philadelphia dosn't work!! :/

2 years ago
1
Bookie0
Bookie0

hum idk not all cities are listed but many are sorry about that :/

@Wilke000

2 years ago
1
EpicGamer007
EpicGamer007

Good tutorial. Congrats on 3000 Cycles :D.

2 years ago
2
Lancexix
Lancexix

Thanks :) I really like this, very good job there was a slight issue with europe york but I can understand why (It's not York's timezone, it's London's)

@Bookie0

2 years ago
1
Bookie0
Bookie0

oh ok thanks for the feedback! =)


@F0XB0I

2 years ago
1
eddyth0420
eddyth0420

im korean. i want to learn this/ but it so hard. who can learn this to me?

2 years ago
2
Bookie0
Bookie0

well first of all, you can look at my code, the comments will tell you exactly what to do. then, you can try to incorporate it into one of your own projects. ;)

@eddyth0420

2 years ago
Load more