8000 completed dates and times · RobACurtis/Learning-Python@a99a86a · GitHub
[go: up one dir, main page]

Skip to content

Commit a99a86a

Browse files
committed
completed dates and times
1 parent 034f702 commit a99a86a

File tree

5 files changed

+113
-28
lines changed

5 files changed

+113
-28
lines changed

Ch4 - Dates and Times/calendars_start.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,43 @@
55

66

77
# TODO: import the calendar module
8-
8+
import calendar
99

1010
# TODO: create a plain text calendar
11-
11+
c = calendar.TextCalendar(calendar.SUNDAY)
12+
# str = c.formatmonth(2022, 1, 0, 0)
13+
# print(str)
1214

1315
# TODO: create an HTML formatted calendar
14-
15-
16+
# hc = calendar.HTMLCalendar(calendar.SUNDAY)
17+
# str = hc.formatmonth(2022, 1)
18+
# print(str)
1619
# TODO: loop over the days of a month
1720
# zeroes mean that the day of the week is in an overlapping month
21+
# for i in c.itermonthdays(2022, 8):
22+
# print(i)
1823

19-
2024
# TODO: The Calendar module provides useful utilities for the given locale,
2125
# such as the names of days and months in both full and abbreviated forms
26+
for name in calendar.month_name:
27+
print(name)
2228

29+
for day in calendar.day_name:
30+
print(day)
2331

2432
# TODO: Calculate days based on a rule: For example, consider
2533
# a team meeting on the first Friday of every month.
2634
# To figure out what days that would be for each month,
2735
# we can use this script:
2836

37+
print("Team meetings will be on:")
38+
for m in range(1, 13):
39+
cal = calendar.monthcalendar(2022, m)
40+
weekone = cal[0]
41+
weektwo = cal[1]
42+
if weekone[calendar.FRIDAY] != 0:
43+
meetday = weekone[calendar.FRIDAY]
44+
else:
45+
meetday = weektwo[calendar.FRIDAY]
46+
47+
print(calendar.month_name[m], meetday)
Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,51 @@
11
# Start file for programming challenge for Learning Python course
22
# LinkedIn Learning Python course by Joe Marini
3-
#
3+
# print to the terminal, which day of the week do you want to count?
4+
# then give a list of days with the indexes
5+
# if they type exit, then quit the program
6+
# if they type a valid number then ask them what month
7+
# then what year
8+
# when they type the year, there needs to be an equation to count the number of days in the month of that year
9+
410

511
import calendar
12+
13+
def main():
14+
15+
daysinmonth = 0
16+
dayoftheweek = 0
17+
month = 0
18+
year = 0
19+
20+
dayoftheweek = input("Which day of the week do you want to count? \n 0: Monday \n 1: Tuesday \n 2: Wednesday \n 3: Thursday \n 4: Friday \n 5: Saturday \n 6: Sunday \n or 'exit' to quit \n? " )
21+
if dayoftheweek == 'exit':
22+
return
23+
elif dayoftheweek.isnumeric() == False:
24+
print("Value must be a positive integer!")
25+
main()
26+
27+
month = input("Enter Month: ")
28+
if month == 'exit':
29+
return
30+
elif month.isnumeric() == False:
31+
print("Value must be a positive integer!")
32+
main()
33+
34+
year = input("Enter Year: ")
35+
if year == 'exit':
36+
return
37+
elif year.isnumeric() == False:
38+
print("Value must be a positive integer!")
39+
main()
40+
41+
c = calendar.monthcalendar(int(year), int(month))
42+
for i in c:
43+
if i[int(dayoftheweek)] != 0:
44+
daysinmonth +=1
45+
46+
47+
48+
print('There are '+ str(daysinmonth) + ' days in the month of ' + str(month) +', ' + str(year))
49+
main()
50+
if __name__ == "__main__":
51+
main()

Ch4 - Dates and Times/dates_start.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,37 @@
33
# LinkedIn Learning Python course by Joe Marini
44
#
55

6+
from datetime import date
7+
from datetime import time
8+
from datetime import datetime
69

710

811
def main():
912
## DATE OBJECTS
1013
# TODO: Get today's date from the simple today() method from the date class
11-
14+
today = date.today()
15+
print(today)
1216

1317
# TODO: print out the date's individual components
18+
print("Date Components:", today.day, today.month, today.year)
1419

15-
1620
# TODO: retrieve today's weekday (0=Monday, 6=Sunday)
21+
print("Today's weekday number is", today.weekday())
22+
days = ["mon", "tues", "wed", "thurs", "fri", "sat", "sun"]
23+
print("Which is a ", days[today.weekday()])
1724

18-
1925
## DATETIME OBJECTS
2026
# TODO: Get today's date from the datetime class
27+
today = datetime.now()
28+
print ("The current date and time is ", today)
2129

22-
2330
# TODO: Get the current time
2431

25-
32+
t = datetime.time(datetime.now())
33+
print(t)
34+
35+
36+
2637

27-
2838
if __name__ == "__main__":
2939
main()
30-

Ch4 - Dates and Times/formatting_start.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,26 @@
88

99
def main():
1010
# Times and dates can be formatted using a set of predefined string
11-
# control codes
11+
# control codes
12+
13+
now = datetime.now()
1214

13-
1415
#### Date Formatting ####
15-
16-
# %y/%Y - Year, %a/%A - weekday, %b/%B - month, %d - day of month
1716

17+
# %y/%Y - Year, %a/%A - weekday, %b/%B - month, %d - day of month
18+
print(now.strftime('The Current year is: %Y'))
19+
print(now.strftime('%a %d, %B, %Y'))
1820

1921
# %c - locale's date and time, %x - locale's date, %X - locale's time
20-
22+
print(now.strftime("Local date and time: %c"))
23+
print(now.strftime("Local date and time: %c"))
24+
print(now.strftime("Local Time: %X"))
2125

2226
#### Time Formatting ####
23-
27+
2428
# %I/%H - 12/24 Hour, %M - minute, %S - second, %p - locale's AM/PM
25-
29+
print(now.strftime("Current Time: %I:%M:%S %p"))
30+
2631

2732
if __name__ == "__main__":
2833
main()

Ch4 - Dates and Times/timedeltas_start.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,35 @@
77
from datetime import date
88
from datetime import time
99
from datetime import datetime
10-
10+
from datetime import timedelta
1111

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

1515
# TODO: print today's date
16+
now = datetime.now()
17+
print ("today is: " + str(now))
1618

1719

1820
# TODO: print today's date one year from now
19-
21+
print ("one year from now it will be: " + str(now + timedelta(days=365)))
2022

2123
# TODO: create a timedelta that uses more than one argument
22-
24+
print ("in four weeks and 3 days it will be: " + str(now + timedelta(weeks=4, days=3)))
2325

2426
# TODO: calculate the date 1 week ago, formatted as a string
2527

2628

2729
### How many days until April Fools' Day?
28-
30+
today = date.today()
31+
xmas = date(today.year, 12, 25)
2932

3033
# TODO: use date comparison to see if April Fool's has already gone for this year
3134
# if it has, use the replace() function to get the date for next year
35+
if xmas < today:
36+
print ("Christmas already went by %d days ago" % ((today-xmas).days))
37+
xmas = xmas.replace(year=today.year + 1)
3238

33-
34-
# TODO: Now calculate the amount of time until April Fool's Day
35-
39+
# TODO: Now calculate the amount of time until April Fool's Day
40+
time_to_xmas = xmas - today
41+
print ("It's just", time_to_xmas.days, "days until Christmas!")

0 commit comments

Comments
 (0)
0