forked from Chalarangelo/30-seconds-of-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |