|
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 |
10 | 2 |
|
11 | 3 |
|
12 | 4 | # TODO: construct a basic timedelta and print it
|
13 |
| - |
| 5 | +print(timedelta(days=365, hours=5, minutes=1)) |
14 | 6 |
|
15 | 7 | # TODO: print today's date
|
16 |
| - |
| 8 | +now = datetime.now() |
| 9 | +print("Today is {}".format(now)) |
17 | 10 |
|
18 | 11 | # TODO: print today's date one year from now
|
19 |
| - |
| 12 | +print("One year from now will be {}".format(now + timedelta(days=365))) |
20 | 13 |
|
21 | 14 | # 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 | +)) |
23 | 18 |
|
24 | 19 | # 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")) |
25 | 22 |
|
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) |
29 | 26 |
|
30 | 27 | # TODO: use date comparison to see if April Fool's has already gone for this year
|
31 | 28 | # 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