strftime() Method

Total Post:113

Points:791
 201  View(s)
Ratings:
Rate this:

How to use time and date function in python ?

  1. Post:140

    Points:984
    Re: strftime() Method

    Python Date: in python data type are not created but we can import a module with dates as date objects in that sequence.Import datetime module and display current date: 

    import datetime 
    x = datetime.datetime.now()
    print(x)
    2018-07-06 2:44:07.670539
    

    The date having year, month, day, hour, minute, second, microsecond. The datetime module has many methods to return information about the date object. Return the year and name of weekday: 

    import datetime 
    x = datetime.datetime.now()
    print(x.year)
    print(x.strftime("%A"))

    Creating Date Objects :

    you can use the datetime() class (constructor) of the datetime module for creating a date.  The datetime() class requires three parameters to create a date: year, month, day. 

    Create a date object: 

    import datetime 
    x = datetime.datetime(2020,6,20)
    print(x)

    The strftime() Method: 

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

    Display the name of the month: 

    import datetime 
    x = datetime.datetime(2018, 6, 1)
    print(x.strftime("%B"))

Answer