
Today we will learn Python date, time, calender.
In Python, date, time and datetime classes provides a number of function to deal with dates, times and time intervals. Date and datetime are an object in Python, so when you manipulate them, you are actually manipulating objects and not string or timestamps. Whenever you manipulate dates or time, you need to import datetime function.
What is TimeTuple?
Many of Python’s time functions handle time as a tuple of 9 numbers, as shown below −
| Index | Field | Values |
|---|---|---|
| 0 | 4-digit year | 2008 |
| 1 | Month | 1 to 12 |
| 2 | Day | 1 to 31 |
| 3 | Hour | 0 to 23 |
| 4 | Minute | 0 to 59 |
| 5 | Second | 0 to 61 (60 or 61 are leap-seconds) |
| 6 | Day of Week | 0 to 6 (0 is Monday) |
| 7 | Day of year | 1 to 366 (Julian day) |
| 8 | Daylight savings | -1, 0, 1, -1 means library determines DST |
The above tuple is equivalent to struct_time structure. This structure has following attributes −
| Index | Attributes | Values |
|---|---|---|
| 0 | tm_year | 2008 |
| 1 | tm_mon | 1 to 12 |
| 2 | tm_mday | 1 to 31 |
| 3 | tm_hour | 0 to 23 |
| 4 | tm_min | 0 to 59 |
| 5 | tm_sec | 0 to 61 (60 or 61 are leap-seconds) |
| 6 | tm_wday | 0 to 6 (0 is Monday) |
| 7 | tm_yday | 1 to 366 (Julian day) |
| 8 | tm_isdst | -1, 0, 1, -1 means library determines DST |
For manipulating dates and times in both simple and complex ways datetime module supplies different classes or categories like :
- date – Manipulate just date ( Month, day, year)
- time – Time independent of the day (Hour, minute, second, microsecond)
- datetime – Combination of time and date (Month, day, year, hour, second, microsecond)
- timedelta— A duration of time used for manipulating dates
- tzinfo— An abstract class for dealing with timezones
Using datetime objects
- Importing datetime objects before executing the code is mandatory
- Using date.today function for printing individual date/month/year as well as indexing the day
- Using date.time object to get time in hours, minutes, seconds and milliseconds
Formatting Time-Out with “str f time function”
- Use “str f time function” to change the format of the year
- Print day, date, month and year separately,
- Call out time for any format 12 hrs or 24 hrs
Timedelta Objects
- With timedelta objects, you can estimate the time for both future and the past
- Calculate the total days left for the special day(birthday) from the current time
- Calculate the total days passed for special day(birthday) from the current time
Exception Handling in Python:
An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program’s instructions. In general, when a Python script encounters a situation that it cannot cope with, it raises an exception. An exception is a Python object that represents an error.
When a Python script raises an exception, it must either handle the exception immediately otherwise it terminates and quits.
Handling an exception:
If you have some suspicious code that may raise an exception, you can defend your program by placing the suspicious code in a try: block. After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible.
Syntax
Here is simple syntax of try….except…else blocks −
try: You do your operations here; ...................... except ExceptionI: If there is ExceptionI, then execute this block. except ExceptionII: If there is ExceptionII, then execute this block. ...................... else: If there is no exception then execute this block.
- A single try statement can have multiple except statements. This is useful when the try block contains statements that may throw different types of exceptions.
- You can also provide a generic except clause, which handles any exception.
- After the except clause(s), you can include an else-clause. The code in the else-block executes if the code in the try: block does not raise an exception.
- The else-block is a good place for code that does not need the try: block’s protection.
Try below assignment for practice: 1.Python program to print the calendar of a given month and year. 2.Program to calculate number of days between two dates 3.Program for Check if all digits of a number divide it 4.Program to find the most occurring character and its count Click here for solution...


Leave a comment