-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWORKINGWITHDATES.py
More file actions
111 lines (57 loc) · 2.8 KB
/
Copy pathWORKINGWITHDATES.py
File metadata and controls
111 lines (57 loc) · 2.8 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""importing the datetime module"""
import datetime
#Problem1
# defining the function
def days_in_month(year, month):
"""functiion will return the number of days in a particular month"""
if month < 12:
return (datetime.date(year, month+1, 1)) - (datetime.date(year, month, 1))
else:
return (datetime.date(year + 1, 1, 1) - datetime.date(year, month, 1))
#functiion will return the number of days in a particular month
print(days_in_month(2020, 11))
#Problem2
#defining the function
def date_is_valid(year, month, day):
"""the function will determine if a date is valid or not"""
if (datetime.date(year, month <= 12, day <= 31)):
return True
else:
return False
#the function will print "True" if the date is vaid and "False" if otherwise
print(date_is_valid(1995, 12, 15))
#Problem3
#defining the function 'days between':the number of days from one date to another.
def days_in_between(year1, month1, day1, year2, month2, day2):
"""function will return the number of days between two dates"""
if (datetime.date(year2, month2, day2)) > (datetime.date(year1, month1, day1)):
return (datetime.date(year2, month2, day2)) - datetime.date(year1, month1, day1)
elif (datetime.date(year1, month1, day1)) != \
(datetime.date(datetime.MAXYEAR - datetime.MINYEAR, month1 <= 12, day1 <= 31)):
return 0
elif (datetime.date(year2, month2, day2)) != \
(datetime.date(datetime.MAXYEAR - datetime.MINYEAR, month2 <= 12, day2 <= 31)):
return 0
elif (datetime.date(year2, month2, day2)) < (datetime.date(year1, month1, day1)):
return 0
else:
return None
# the function will:
# (1)return the number of days between two given dates,
# (2)return 0 if a given date is invalid
# (3)return 0 if the second date is earier than the first date.
print(days_in_between(2008, 10, 25, 2021, 2, 25))
#Problem4
#defining the function 'age in days': number of days from a past date to today's date.
def age_in_days(year, month, day):
"""function will print the number of days between a past date and today's date"""
if (datetime.date(datetime.MAXYEAR - datetime.MINYEAR, month <= 12, day <= 31)):
return (datetime.date.today()) - (datetime.date(year, month, day))
elif (datetime.date(year, month, day)) != \
(datetime.date(datetime.MAXYEAR - datetime.MINYEAR, month <= 12, day <= 31)):
return 0
elif (datetime.date(year, month, day)) > (datetime.date.today()):
return 0
else:
return 0
print(age_in_days(2021, 11, 15))