Quick Contact

    Python Date and Time

    In Python programming language, can handle date and time in the following ways.

    Tick

    Tick in Python would be the instance if time measured in seconds since January 1, 1970, 12:00. Python has a time module which has functions to work with time.

    Python Date and Time
    TimeTuple

    In Python language, many time functions handle time as a tuple of 9 numbers as indicated in the below table. This tuple is equivalent to struct_time structure shown in the attribute column.

    Index Field Values Attributes
    0 4-digit year 2008 tm year
    1 Month 1 to 12 tm_mon
    2 Day 1 to 31 tm_mday
    3 Hour 0 to 23 tm_hour
    4 Minute 0 to 59 tm_min
    5 Second 0 tohttp://Python Date and Time 61 (60 or 61 are leap seconds) tm_sec
    6 Day of Week 0 to 6 (0 is Monday) tm_wday
    7 Day of Year 1 to 366 (Julian day) tm_yday
    8 Daylight Savings -1, 0, 1, -1) means library determines DST tm_isdst
    Python code example for TimeTuple

    Getting current local time: To get the current local time in TimeTuple format use the function as time.localtime(time.time()), this function will translate the tick seconds into struct_time structure as a tuple of 9 numbers as discussed above.

    Python Date and Time

    Formatting current local time: To get the current local readable format use the function as time.asctime () as shown in the below example.

    Python Date and Time

    Print calendar for a month: Python has a calendar module which gives a wide range of methods to work with monthly and yearly calendars. In the following example, we are going to print a calendar for a Feb 2016 month (leap year) using the function calendar.month (year, month).

    Python Date and Time
    Functions in Time Module

    Following are the functions available in the Calendar module.

    Time Functions Descriptions
    time.altzone This is to be used only when Only use this if daylight is nonzero. It is positive if the offset of the local DST time zone is west of UTC.
    This is negative if the local DST time zone is east of UTC (as in Western Europe, including the UK). All in seconds.
    time.asctime([tupletime]) This function of time module accepts a time-tuple and returns a readable 24- character string such as ‘Tue Apr 26 19:09:19 2016’.
    time.clock( ) This function of time module returns the current CPU time as a floating-point number of seconds.
    time.ctime([seconds]) This function of time module is just like function asctime (localtime (seconds)) and without arguments is like asctime( ).
    time.gmtime([seconds]) This function of time module accepts an instant expressed in seconds since the epoch and returns a time-tuple xyz with the UTC time. It is to be noted that zyz.tm_isdst is always 0.
    time.mktime(tupletime) This function of time module accepts an instant expressed as a time-tuple in local time. It returns a floating-point value with the instant expressed in seconds since the epoch.
    time.sleep(secs) This function of time module suspends the calling thread for secs seconds.
    time.strftime(fmt[,tupletime]) This function of time module accepts an instant expressed as a time-tuple in local time. It returns a string describing the instant as specified by string fmt.
    time.strptime(strg,format=’%a %b %d %H:%M:%S %Y’) This function of time module parses strg according to format string format. It returns the instant in timetuple format.
    time.time( ) This function of time module returns the current time instant, a floating-point number of seconds since the epoch.
    time.tzset() This function of time module resets the time conversion rules used by the library routines.

    Enroll Yourself: Python Training In Delhi

    Copyright 1999- Ducat Creative, All rights reserved.