1
+ # Date and Time functionality
2
+ """
3
+ Farhan Javed - 11-19-2019
4
+ Date and time basic functionality code. Also includes working with Calendars.
5
+ Feel free to reach out : farhan.javed47@gmail.com
6
+ """
7
+ from datetime import date
8
+ from datetime import datetime
9
+ from datetime import timedelta
10
+ import calendar
11
+
12
+
13
+ def main ():
14
+
15
+ today = date .today ()
16
+ print ("Today's date = " , today )
17
+ print ("Today's date = " + str (today ))
18
+ print ("Today's date = " + today .strftime ("%d/%m/%Y" ))
19
+ print ("Today's date = " + today .strftime ("%A-%B-%Y" ))
20
+ # the strftime(format) function is used to format the date and print it out.
21
+ # more information here : https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior
22
+ # use a comma ( , ) instead of plus ( + ) to print the date object without having to use the str() function.
23
+
24
+ print ("Printing individual components : " , today .day , today .month , today .year )
25
+ print ("Printing individual components : " + str (today .day ) + str (today .month ) + str (today .year ))
26
+
27
+ # also notice that the comma operator in the print function automatically put a space when printing out objects
28
+ # which is not done by the plus operator since plus concatenates the two strings.
29
+
30
+ print ("Today's week day number is : " , today .weekday ())
31
+ # Weekday function returns the day of the week as an integer starting from Monday = 0 uptil Sunday = 6.
32
+ # This can be used as a list iterator as well in functions.
33
+
34
+ days = ["Mon" , "Tue" , "Wed" , "Thurs" , "Fri" , "Sat" , "Sun" ]
35
+ print ("Today's day : " + days [today .weekday ()])
36
+ datetime_now = datetime .now ()
37
+ print ("Current date and time : " , datetime_now )
38
+
39
+ t = datetime .time (datetime_now )
40
+ print ("Current Time : " , t )
41
+ print (datetime_now .strftime ("%a, %d %B, %y" ))
42
+
43
+ # localized version of date and time
44
+ print (datetime_now .strftime ("Locale date and time : %c" ))
45
+ print (datetime_now .strftime ("Locale date: %x" ))
46
+ print (datetime_now .strftime ("Local time : %X" ))
47
+
48
+ # formatted time
49
+
50
+ print (datetime_now .strftime ("Current time : %I:%M:%S %p" ))
51
+ print (datetime_now .strftime ("Current time : %H:%M" ))
52
+
53
+ # mathematical operations on the date and time objects. We can use the timedelta class
54
+ # we need to import the timedelta package from datetime
55
+
56
+ print (timedelta (days = 365 , hours = 5 , minutes = 1 ))
57
+ print ("Current date and time : " , datetime_now )
58
+ print ("One year later : " + str (datetime_now + timedelta (days = 365 )))
59
+ print ("3 weeks and 2 days later : " + str (datetime_now + timedelta (weeks = 3 , days = 2 )))
60
+
61
+ t = datetime .now () - timedelta (weeks = 1 )
62
+ print ("One week ago it was : " + t .strftime ("%A %B %d, %Y" ))
63
+
64
+ april_fools_day = date (today .year , 4 , 1 )
65
+
66
+ if april_fools_day < today :
67
+ print ("April Fools day was %d days ago" % (today - april_fools_day ).days )
68
+ april_fools_day = april_fools_day .replace (year = today .year + 1 )
69
+ print ("Days remaining in next April fools day : " , (april_fools_day - today ).days )
70
+
71
+ # to work with calendars we need to import the calendar package
72
+
73
+ cal = calendar .TextCalendar (calendar .SUNDAY ) # tells the first day of the week
74
+ formatted_cal = cal .formatmonth (2019 , 11 , 0 , 0 )
75
+
76
+ html_cal = calendar .HTMLCalendar (calendar .SUNDAY )
77
+ html_code = html_cal .formatmonth (2019 , 11 )
78
+
79
+ # for i in cal.itermonthdays(2019, 11): # 0s represent that those days belong to the previous/next month
80
+ # print(i)
81
+
82
+ # For locale based month and day names
83
+ for month in calendar .month_name :
84
+ print (month )
85
+
86
+ for day in calendar .day_name :
87
+ print (day )
88
+
89
+ # for example if we want to have a team meeting on the first Friday of every month in 2020
90
+ # we can use the following piece of code.
91
+
92
+ print ("Team meeting in 2020 will be on : " )
93
+ for meeting in range (1 ,13 ): # 13 is non inclusive
94
+ cal = calendar .monthcalendar (2020 , meeting )
95
+ week_one = cal [0 ]
96
+ week_two = cal [1 ]
97
+
98
+ if week_one [calendar .FRIDAY ] != 0 :
99
+ meet_day = week_one [calendar .FRIDAY ]
100
+ else :
101
+ meet_day = week_two [calendar .FRIDAY ]
102
+
103
+ print ("%10s %2d" % (calendar .month_name [meeting ], meet_day ))
104
+
105
+
106
+ main ()
0 commit comments