Learn to Code via Tutorials on Repl.it!

← Back to all posts
How To Get The Time! ⏰ [python]
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:


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 :/
Voters
11irishjs
PrimeCoder
DavidIdris
jyuvero007
Leroy01010
wantT0befriends
Wilke000
SelenaYang1
EpicGamer007
upa5527
Comments
hotnewtop
BobTheTomatoPie

AWESOME

Bookie0

thanks! =) @BobTheTomatoPie

firefish

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

btw, time.time() is an option

Bookie0

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

Bookie0

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

firefish

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

firefish

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

Bookie0

wow nice @johnstev111

Bookie0

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

firefish

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

Bookie0
adl212

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

Bookie0

ok, thanks! @adl212

CodingRobot12

This is cool.

Bookie0
CodingRobot12

no problem @Bookie0

DamienKilduff

Very Cool! I will definitely use it!

Bookie0

cool thanks!
@DamienKilduff

DavidLuzzatto

@DamienKilduff hum fayot

Bookie0
Wilke000

Wow:

Bookie0
Wilke000

@Bookie0

AWESOME!!!!!

Bookie0
Wilke000
Wilke000

Philadelphia dosn't work!! :/

Bookie0

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

Wilke000
EpicGamer007

Good tutorial. Congrats on 3000 Cycles :D.

Bookie0

thanks lol! @AbhayBhat

Bookie0

thanks! lol @AbhayBhat

EpicGamer007

@Bookie0, Why did it send twice lol

Bookie0

idk lol @AbhayBhat

Lancexix
Bookie0

lol thanks! @F0XB0I

Bookie0

wow nice video! @F0XB0I

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

Bookie0

oh ok thanks for the feedback! =)
@F0XB0I

eddyth0420

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

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

DEMONul1234

Looks cool, my dude ^^

Bookie0
gibbsfreenergy

This is so cool!

Bookie0
gibbsfreenergy

Who else just coming here because they broke their watch?
hehe

Bookie0

lol thanks! @JosephSanthosh

CodingGoose

Nice

Bookie0
SixBeeps

its time for you to get a watch

Bookie0

Lol thanks! @SixBeeps

zplusfour


Good!

Bookie0

thanks! @ZDev1

zplusfour

@Bookie0 np :D

JBloves27

Nice! I like it!

AND IM GONNA SECRETLY USE IT!!!!

Bookie0

thanks! sure, you dont have to secretly use it. @JBYT27

[deleted]

Hey bookie! Nice post! I do have one small complaint though, could you calm down on the y's a tiny bit? Its a little troublesome when AllAwesome497's bot auto reports this for spam when you do that. Thanks in advance! (Also interesting use of datetime)

Bookie0

oh yes im sorry about that, I will change it immediately lol.

glad you enjoyed this! =) @eekboi

DynamicSquid

@eekboi wait wat bot?

CodingCactus

@DynamicSquid mod/report/spam check bot

[deleted]

@Bookie0 No problem! Thanks for listening.

[deleted]

@DynamicSquid @ReplModBot is the bot lol

Bookie0

sure @eekboi