Skip to content

Instantly share code, notes, and snippets.

@rbw
Last active Sep 14, 2021
Embed
What would you like to do?
Get part of day (morning, afternoon, evening, night) in Python3.6+
#!/usr/bin/env python3
def get_part_of_day(h):
return (
"morning"
if 5 <= h <= 11
else "afternoon"
if 12 <= h <= 17
else "evening"
if 18 <= h <= 22
else "night"
)
# To use current hour:
# from datetime import datetime
# part = get_part_of_day(datetime.now().hour)
# print(f"Have a good {part}!")
for hour in range(0, 24):
part = get_part_of_day(hour)
print(f"hour {hour} is {part}")
@matabares

This comment has been minimized.

Copy link

@matabares matabares commented Jun 25, 2020

Thanks. Usefull!.

@crukundo

This comment has been minimized.

Copy link

@crukundo crukundo commented Jun 26, 2020

Thank you.

@renich

This comment has been minimized.

Copy link

@renich renich commented Oct 2, 2020

I think the official time to say "evening" in this part of the world is 19:00. Just an opinion.

@renich

This comment has been minimized.

Copy link

@renich renich commented Oct 2, 2020

Then again, this varies a lot it seems: https://en.wikipedia.org/wiki/Evening

@Messiw2003

This comment has been minimized.

Copy link

@Messiw2003 Messiw2003 commented Apr 4, 2021

very helpful

@saipajju

This comment has been minimized.

Copy link

@saipajju saipajju commented May 9, 2021

Thanks it's really helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment