Python Date and timePython provides the datetime module work with real dates and times. In real-world applications, we need to work with the date and time. Python enables us to schedule our Python script to run at a particular timing. In Python, the date is not a data type, but we can work with the date objects by importing the module named with datetime, time, and calendar. In this section of the tutorial, we will discuss how to work with the date and time objects in Python. The datetime classes are classified in the six main classes.
TickIn Python, the time instants are counted since 12 AM, 1st January 1970. The function time() of the module time returns the total number of ticks spent since 12 AM, 1st January 1970. A tick can be seen as the smallest unit to measure the time. Consider the following example Output: 1585928913.6519969 How to get the current time?The localtime() functions of the time module are used to get the current time tuple. Consider the following example. Example Output: time.struct_time(tm_year=2020, tm_mon=4, tm_mday=3, tm_hour=21, tm_min=21, tm_sec=40, tm_wday=4, tm_yday=94, tm_isdst=0) Time tupleThe time is treated as the tuple of 9 numbers. Let's look at the members of the time tuple.
Getting formatted timeThe time can be formatted by using the asctime() function of the time module. It returns the formatted time for the time tuple being passed. Example Output: Tue Dec 18 15:31:39 2018 Python sleep timeThe sleep() method of time module is used to stop the execution of the script for a given amount of time. The output will be delayed for the number of seconds provided as the float. Consider the following example. Example Output: 0 1 2 3 4 The datetime ModuleThe datetime module enables us to create the custom date objects, perform various operations on dates like the comparison, etc. To work with dates as date objects, we have to import the datetime module into the python source code. Consider the following example to get the datetime object representation for the current time. Example Output: 2020-04-04 13:18:35.252578 Creating date objectsWe can create the date objects bypassing the desired date in the datetime constructor for which the date objects are to be created. Consider the following example. Example Output: 2020-04-04 00:00:00 We can also specify the time along with the date to create the datetime object. Consider the following example. Example Output: 2020-04-04 01:26:40 In the above code, we have passed in datetime() function year, month, day, hour, minute, and millisecond attributes in a sequential manner. Comparison of two datesWe can compare two dates by using the comparison operators like >, >=, <, and <=. Consider the following example. Example Output: fun hours The calendar modulePython provides a calendar object that contains various methods to work with the calendars. Consider the following example to print the calendar for the last month of 2018. Example Output: March 2020 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 31 Printing the calendar of whole yearThe prcal() method of calendar module is used to print the calendar of the entire year. The year of which the calendar is to be printed must be passed into this method. Example Output: Next TopicPython Regular Expressions |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India