-
Notifications
You must be signed in to change notification settings - Fork 0
/
timedeltas
40 lines (31 loc) · 1.28 KB
/
timedeltas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#
# Exmaple file for working with timedeltaobjects
#
from datetime import date
from datetime import time
from datetime import datetime
from datetime import timedelta
# construct a basic timedelta and print it
print(timedelta(days=365, hours=5, minutes=1))
# print today's date
now = datetime.now()
print("Today is: " + str(now))
# print today's date one year from now
print("One year from now it will be: " + str(now + timedelta(days=365)))
# create a timedelta that uses more than one argument
print("In 2 days and 3 weeks, it will be " + str(now + timedelta(days=2, weeks=3)))
#calculate the date 1 week ago, formatted as a string
t = datetime.now() - timedelta(weeks=1)
s = t.strftime("%A %a %d, %Y")
print("One week ago it was: ", s)
### How many days until April Fool's Day?
today = date.today()
afd = date(today.year, 4, 1)
# use date comparison to see if April Fool's has already gone for this year
# if it has, use the replace() function to get the date for next year
if afd < today:
print("April fool's day already went by %d days ago" % ((today-afd).days))
afd = afd.replace(year = today.year+1)
# now calculate the amount of time until April Fool's day
time_to_afd = afd-today
print("It's just", time_to_afd.days, "days until April Fool's Day")