Python Date And Time Function

DateTime Definition

The datetime module supplies classes for manipulating dates and times in both simple and complex ways

The datetime object has a method for formatting date objects into readable strings.

The method is called strftime(), and takes one parameter, format, to specify the format of the returned string:


Example:

 import datetime

 x = datetime.datetime.now()

 print(x)

To disply the current date and time we must Import the datetime module :

Output:

 2019-09-25 14:19:55.496410

The date contains year, month, day, hour, minute, second, and microsecond.

The datetime module has many other methods which returns information about the date object.


Example 2:

This example would return the year and name of weekday:

 import datetime

 a = datetime.datetime.now()

 print(a.year)
 print(a.strftime("%B"))

Output:

 2019
 September

Printing the calendar of whole year

 import calendar  
      
     #printing the calendar of the full year 2020  
 calendar.prcal(2020)  


Output:

                                  2020

       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                      1  2                         1
  6  7  8  9 10 11 12       3  4  5  6  7  8  9       2  3  4  5  6  7  8
 13 14 15 16 17 18 19      10 11 12 13 14 15 16       9 10 11 12 13 14 15
 20 21 22 23 24 25 26      17 18 19 20 21 22 23      16 17 18 19 20 21 22
 27 28 29 30 31            24 25 26 27 28 29         23 24 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                   1  2  3       1  2  3  4  5  6  7
  6  7  8  9 10 11 12       4  5  6  7  8  9 10       8  9 10 11 12 13 14
 13 14 15 16 17 18 19      11 12 13 14 15 16 17      15 16 17 18 19 20 21
 20 21 22 23 24 25 26      18 19 20 21 22 23 24      22 23 24 25 26 27 28
 27 28 29 30               25 26 27 28 29 30 31      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                      1  2          1  2  3  4  5  6
  6  7  8  9 10 11 12       3  4  5  6  7  8  9       7  8  9 10 11 12 13
 13 14 15 16 17 18 19      10 11 12 13 14 15 16      14 15 16 17 18 19 20
 20 21 22 23 24 25 26      17 18 19 20 21 22 23      21 22 23 24 25 26 27
 27 28 29 30 31            24 25 26 27 28 29 30      28 29 30
                          31

       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                         1          1  2  3  4  5  6
  5  6  7  8  9 10 11       2  3  4  5  6  7  8       7  8  9 10 11 12 13
 12 13 14 15 16 17 18       9 10 11 12 13 14 15      14 15 16 17 18 19 20
 19 20 21 22 23 24 25      16 17 18 19 20 21 22      21 22 23 24 25 26 27
 26 27 28 29 30 31         23 24 25 26 27 28 29      28 29 30 31
   


                        30