8000 Using timedelta objects · neryuuk/learning-python@789476d · GitHub
[go: up one dir, main page]

Skip to content

Commit 789476d

Browse files
committed
Using timedelta objects
1 parent b5017f7 commit 789476d

File tree

2 files changed

+23
-63
lines changed

2 files changed

+23
-63
lines changed

Ch4 - Dates and Times/timedeltas_finished.py

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
1-
#
2-
# Example file for working with timedelta objects
3-
# LinkedIn Learning Python course by Joe Marini
4-
#
5-
6-
7-
from datetime import date
8-
from datetime import time
9-
from datetime import datetime
1+
from datetime import date, time, datetime, timedelta
102

113

124
# TODO: construct a basic timedelta and print it
13-
5+
print(timedelta(days=365, hours=5, minutes=1))
146

157
# TODO: print today's date
16-
8+
now = datetime.now()
9+
print("Today is {}".format(now))
1710

1811
# TODO: print today's date one year from now
19-
12+
print("One year from now will be {}".format(now + timedelta(days=365)))
2013

2114
# TODO: create a timedelta that uses more than one argument
22-
15+
print("In two weeks and 3 days will be {}".format(
16+
now + timedelta(weeks=2, days=3)
17+
))
2318

2419
# TODO: calculate the date 1 week ago, formatted as a string
20+
t = datetime.now() - timedelta(weeks=1)
21+
print(t.strftime("One week ago it was %A %B %d, %Y"))
2522

26-
27-
### How many days until April Fools' Day?
28-
23+
# How many days until April Fools' Day?
24+
today = date.today()
25+
aprilFoolsDay = date(today.year, 4, 1)
2926

3027
# TODO: use date comparison to see if April Fool's has already gone for this year
3128
# if it has, use the replace() function to get the date for next year
32-
33-
34-
# TODO: Now calculate the amount of time until April Fool's Day
35-
29+
if aprilFoolsDay < today:
30+
print(
31+
"April fools' day already went by, {} day(s) ago"
32+
.format((today - aprilFoolsDay).days)
33+
)
34+
aprilFoolsDay = aprilFoolsDay.replace(year=today.year + 1)
35+
36+
# TODO: Now calculate the amount of time until April Fool's Day
37+
time_to_afd = aprilFoolsDay - today
38+
print("It is {} days until next april fools' day".format(time_to_afd.days))

0 commit comments

Comments
 (0)
0