Date and Time Module: ===================== module: datetime provides different classes to deal with date and time. Classes: date datetime time Classes ==> methods to deal with dates, times, and time intervals. Two objects: Date and DateTime Generate the date: ================== 1) we should import "date" class. Syntax: from datetime import date 2) create an object for the date class. Syntax: object-name = date(year, month, date) Here: year > 0 month ==> 1 to 12 date ==> 1 to 31 from datetime import date dt = date(1995, 6, 30) print(dt) dt1 = date(2024,11,29) print(dt1) dt2 = date(2050, 6,20) print(dt2) ============================================ Getting of the Current date: ============================ today(): ======= ==> this is a static method because it need to access/invoke with the class name. Syntax: object = date.today() from datetime import date today = date.today() # Static method can always call through the class name print(today) ==> date class provided some attributes/properties: year ==> can return the year month ==> can return the month number day ==> can return the day number Syntax: object-name.year object-name.month object-name.day Note: year, month and day are the instance variables/properties/attributes for the class "date" from datetime import date today = date.today() # Static method can always call through the class name print(today) print("The Year = ",today.year) print("The Month = ",today.month) print("The Day = ",today.day) ================================================== Timestamp: ========== fromtimestamp(): ================ ==> static method Syntax: datetime.fromtimestamp(number/Integer) Here: parameter ==> value in sec Note: fromtimestamp() can generate the time stamp from the year 1970 and time 5.30 am by default. from datetime import datetime timestamp = datetime.fromtimestamp(19876594801) print(timestamp) ================================================================= Date to string: =============== two methods: 1) isoformat() 2) strftime() isoformat() =========== ==> Static method Syntax: date.isoformat(date-object) strftime() ========== ==> Instance Method Syntax: datetime-object.strftime("%Y-%m-%d %H : %M : %S") from datetime import datetime today = date.today() now = datetime.now() print(type(today)) s = date.isoformat(today) print("The Date in string format = ",s) print(type(s)) # s1 = date.strftime(today) s1 = today.strftime("%Y-%m-%d %H:%M:%S") print("The date in string = ",s1) print(type(s1)) s2 = now.strftime("%Y/%m/%d %H : %M : %S") print(s2) print(type(s2)) =================================== Time Class: =========== from datetime import time myTime = time(16,55,56) print(myTime) t1 = time(minute = 55) print(t1) t2 = time() print(t2) t3 = time(22) print(t3) t4 = time(22,55) print(t4) t5 = time(hour = 22) print(t5) t6 = time(second = 55) print(t6) Time class properties: ====================== time-class-object.hour time-class-object.minute time-class-object.second from datetime import time Time = time(11,23,35) print(Time) print("Hours = ",Time.hour) print("Minutes = ",Time.minute) print("Seconds = ",Time.second)