Python Date and Time

Python provides several modules to work with date and time. The most commonly used module is datetime, which allows you to manipulate dates and times with ease. You can get the current date and time, format dates, perform arithmetic operations on dates, and much more.

Getting the Current Date and Time

The datetime module provides the datetime.now() method to get the current local date and time.

1. Get the Current Date and Time


from datetime import datetime

current_datetime = datetime.now()
print("Current Date and Time:", current_datetime)
            

Output

Current Date and Time: 2024-11-13 14:23:45.678901

Formatting Dates

You can format dates using the strftime() method, which allows you to convert a datetime object to a formatted string.

2.


formatted_date = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
print("Formatted Date and Time:", formatted_date)
            

Output

Formatted Date and Time: 2024-11-13 14:23:45

Date Arithmetic

The timedelta class from the datetime module allows you to perform arithmetic operations on dates. You can add or subtract days, weeks, hours, etc., to a date.

3. Adding 7 days to the current date


from datetime import timedelta

new_date = current_datetime + timedelta(days=7)
print("Date after 7 days:", new_date)
            

Output

Date after 7 days: 2024-11-20 14:23:45.678901

Printing the Calendar of the Whole Year

Python provides a built-in calendar module that allows you to print a calendar for any given year. The calendar.TextCalendar().formatyear() method can be used to print the calendar of the entire year.


import calendar

year = 2024
full_calendar = calendar.TextCalendar().formatyear(year)
print(full_calendar)
            

Output

                                  2024

      January                   February                   March
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7                1  2  3  4                  1  2  3
 8  9 10 11 12 13 14       5  6  7  8  9 10 11       4  5  6  7  8  9 10
15 16 17 18 19 20 21      12 13 14 15 16 17 18      11 12 13 14 15 16 17
22 23 24 25 26 27 28      19 20 21 22 23 24 25      18 19 20 21 22 23 24
29 30 31                  26 27 28 29               25 26 27 28 29 30 31

       April                      May                        June
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7             1  2  3  4  5                      1  2
 8  9 10 11 12 13 14       6  7  8  9 10 11 12       3  4  5  6  7  8  9
15 16 17 18 19 20 21      13 14 15 16 17 18 19      10 11 12 13 14 15 16
22 23 24 25 26 27 28      20 21 22 23 24 25 26      17 18 19 20 21 22 23
29 30                     27 28 29 30 31            24 25 26 27 28 29 30

        July                     August                   September
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7               1  2  3  4                          1
 8  9 10 11 12 13 14       5  6  7  8  9 10 11       2  3  4  5  6  7  8
15 16 17 18 19 20 21      12 13 14 15 16 17 18       9 10 11 12 13 14 15
22 23 24 25 26 27 28      19 20 21 22 23 24 25      16 17 18 19 20 21 22
29 30 31                  26 27 28 29 30 31         23 24 25 26 27 28 29
                                                    30

      October                   November                  December
Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
    1  2  3  4  5  6                  1  2  3                          1
 7  8  9 10 11 12 13       4  5  6  7  8  9 10       2  3  4  5  6  7  8
14 15 16 17 18 19 20      11 12 13 14 15 16 17       9 10 11 12 13 14 15
21 22 23 24 25 26 27      18 19 20 21 22 23 24      16 17 18 19 20 21 22
28 29 30 31               25 26 27 28 29 30         23 24 25 26 27 28 29
                                                    30 31
            

Python's datetime and calendar modules provide powerful tools for working with dates, times, and calendars. You can get the current date and time, format dates, perform arithmetic, print calendars, and parse date strings.