Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Python Date and Time

    Python Programming Tutorials


    There are various ways Python supplies date and time feature to add to the program. Python's Time and calendar module help in tracking date and time. Also, the 'DateTime' provides classes for controlling date and time in both simple and complex ways.

    There are two different date and time objects in Python. These are:
    • naive
    • aware

    The differences between 'naive' and 'aware' objects are:

    1. The 'aware' object holds the details about time-zone and daylight saving information, whereas naïve objects do not have that feature.
    2. Naive objects are easy to understand and to work with.

    Defining Tick

    As we all can make an idea that time intervals have to be represented in floating-point numbers. Tick signifies the floating-point numbers in units of seconds in Python. Particular instants of time are represented in seconds from 12:00 am, 1st of January, of the year 1990. A popular module name 'time' is available, which provides functions that let's programmer work with the time and made conversion between time-representation possible.

    The pre-defined function time.time() is used to return the current time of the system.

    The daytime module exports the constants listed below:

    • MINYEAR
    • MAXYEAR

    Two programs are shown showing the use of two different modules:

    Example:

    import time;
    
    ticktock = time.time();
    
    print("Number of ticks:", ticktock)

    Output:

    Number of ticks: 1465927104.951356

    Another program showing datetime module:

    Example:

    from datetime import datetime
    
    presentime = datetime.now()
    
    print(" NOW THE TIME IS:", presentime)
    
    print("Todays Date is:", presentime.strftime('%Y-%m-%d') )
    
    print("Year is:", presentime.year)
    
    print("MOnth is:", presentime.month)
    
    print("Day is:", presentime.day)

    Output:

    NOW THE TIME IS: 2016-06-14 18:01:25.831269
    Todays Date is: 2016-06-14
    Year is: 2016
    MOnth is: 6
    Day is: 14

    Timetuple In Python

    Python usually handles time's function as a tuple with nine numbers starting from zero (0) to eight (8).

    0 Four digit number (year) 2016
    1 Months 1 - 12
    2 Day 1 - 31
    3 Hour 0 - 23
    4 Minute 0 - 59
    5 Second 0 - 60
    6 Days of the week 0 - 6 (where Zero designates Monday)
    7 Days of a year 1 - 366
    8 Daylight saving -1, 0, 1, -1

    Timedelta Object

    It is used to represent duration, i.e., the difference between two dates and or times.  For invoking this, the syntax is:

    Syntax:

    datetime.timedelta([days, [,seconds [,microseconds [,milliseconds [, minutes [,hours [,weeks]]]]]])

    In the above scenario, all arguments are optional, and their default value is zero (0).

    Program To Get Current Time

    Example:

    import time;
    
    curtime = time.localtime(time.time())
    
    print("Current Time is:", curtime)

    Output:

    Current Time is: time.struct_time(tm_year=2016, tm_mon=6, tm_mday=14, tm_hour=0, tm_min=4, tm_sec=49, tm_wday=1, tm_yday=166, tm_isdst=0)

    Calendar Of A Month

    This facility is provided by the "calendar" module, which offers a wide range of methods to deal with monthly calendars. A simple program showing how to use:

    Example:

    import calendar
    
    calndr = calendar.month(2016, 6)
    
    print("The calendar for the month June of Year 2016 is:")
    
    print (calndr)

    Output:

    The calendar for the month June of Year 2016 is:
         June 2016
    Mo Tu We Th Fr Sa Su
           1  2  3  4  5
     6  7  8  9 10 11 12
    13 14 15 16 17 18 19
    20 21 22 23 24 25 26
    27 28 29 30


    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.