From d6a889c5e13c1648119f289ae3635b6a745e2912 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Wed, 28 Oct 2020 16:20:27 +0200 Subject: [PATCH] Add is_weekend --- snippets/is_weekend.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 snippets/is_weekend.md diff --git a/snippets/is_weekend.md b/snippets/is_weekend.md new file mode 100644 index 000000000..f390693e5 --- /dev/null +++ b/snippets/is_weekend.md @@ -0,0 +1,24 @@ +--- +title: is_weekend +tags: date,beginner +--- + +Checks if the given date is a weekend. + +- Use `datetime.datetime.weekday()` to get the day of the week as an integer. +- Check if the day of the week is greater than `4`. +- Omit the second argument, `d`, to use a default value of `datetime.today()`. + +```py +from datetime import datetime + +def is_weekend(d = datetime.today()): + return d.weekday() > 4 +``` + +```py +from datetime import date + +is_weekend(date(2020,10,25)) # True +is_weekend(date(2020,10,28)) # False +```