{"id":14891,"date":"2021-07-27T09:33:43","date_gmt":"2021-07-27T04:03:43","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=14891"},"modified":"2021-07-27T09:33:47","modified_gmt":"2021-07-27T04:03:47","slug":"python-time-difference-in-seconds","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/","title":{"rendered":"Different Methods in Python to Calculate Time Difference in Seconds"},"content":{"rendered":"\n<p>Here we are going to see different methods to calculate python time difference in seconds. We have so many ways, but here we are using straightforward ways to calculate the time difference. Python contains a standard library to calculate time, date, etc.<\/p>\n\n\n\n<p><strong>We are going to calculate the time difference using a module called &#8220;datetime.&#8221; This is the popular python module, and it is beneficial to get the date and time. Let us see here how to calculate the time difference in seconds.<\/strong><\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#What_is_mean_by_datetime_module\" >What is mean by datetime module?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#How_datetime_module_work\" >How datetime module work?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Benefits_of_datetime_module\" >Benefits of datetime module<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#What_are_the_syntax_parameters_and_return_type_to_get_the_current_date_and_time\" >What are the syntax, parameters, and return type to get the current date and time?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Syntax_to_get_current_date_and_time\" >Syntax to get current date and time<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Parameter\" >Parameter<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Returns\" >Returns<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Example\" >Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Output\" >Output<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#What_are_the_syntax_parameter_and_return_type_to_create_date_objects\" >What are the syntax, parameter, and return type to create date objects?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Syntax\" >Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Example-2\" >Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Output-2\" >Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Parameter-2\" >Parameter<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Returns-2\" >Returns<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#2_Examples_in_Python_to_Find_Time_Difference_in_Seconds\" >2 Examples in Python to Find Time Difference in Seconds<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Example_1\" >Example 1<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Example_2\" >Example 2<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Frequently_Asked_Questions_Related_to_Python_Time_Difference_in_Seconds\" >Frequently Asked Questions Related to Python Time Difference in Seconds<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#Trending_Right_Now\" >Trending Right Now<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-mean-by-datetime-module\"><span class=\"ez-toc-section\" id=\"What_is_mean_by_datetime_module\"><\/span>What is mean by datetime module?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The&nbsp;<strong>datetime<\/strong>&nbsp;module provides time objects that are similar to the Time objects. A date in Python is not an object, but we can import a module&nbsp;<strong>&#8220;datetime&#8221;<\/strong>&nbsp;to get a date and time. It is a built-in function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-datetime-module-work\"><span class=\"ez-toc-section\" id=\"How_datetime_module_work\"><\/span>How datetime module work?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>It combines date and time that contains some attributes like a year, month, date, hours, minutes, seconds, and milliseconds. It takes all the attributes as an integer. We can get today&#8217;s date or date objects and more using the datetime module.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-benefits-of-datetime-module\"><span class=\"ez-toc-section\" id=\"Benefits_of_datetime_module\"><\/span>Benefits of datetime module<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Calculate the difference between today and some other days. For example your birthday<\/li><li>Apps that send an SMS or email to the user based on the date.<\/li><li>Instagram or social media clone that can say that it was posted five days ago.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-are-the-syntax-parameters-and-return-type-to-get-the-current-date-and-time\"><span class=\"ez-toc-section\" id=\"What_are_the_syntax_parameters_and_return_type_to_get_the_current_date_and_time\"><\/span>What are the syntax, parameters, and return type to get the current date and time?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-syntax-to-get-current-date-and-time\"><span class=\"ez-toc-section\" id=\"Syntax_to_get_current_date_and_time\"><\/span>Syntax to get current date and time<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">import module\nvar=datetime.datetime.now()\nprint(var)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-parameter\"><span class=\"ez-toc-section\" id=\"Parameter\"><\/span>Parameter<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Current date and time<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-returns\"><span class=\"ez-toc-section\" id=\"Returns\"><\/span>Returns<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>print current date and time<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example\"><span class=\"ez-toc-section\" id=\"Example\"><\/span>Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport datetime\na=datetime.datetime.now()\nprint(&quot;Date and time is:&quot;,a)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-output\"><span class=\"ez-toc-section\" id=\"Output\"><\/span>Output<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">Date and time is: 2021-07-26 12:29:42.322270<\/pre>\n\n\n\n<p class=\"has-background\" style=\"background-color:#dde4ea;font-size:19px\"><strong>Must Read |<\/strong> <a href=\"http:\/\/www.pythonpool.com\/python-dateutil\/\" rel=\"noreferrer noopener\" target=\"_blank\"><span style=\"text-decoration: underline;\">Python dateutil Module: Explanation and Examples<\/span><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-are-the-syntax-parameter-and-return-type-to-create-date-objects\"><span class=\"ez-toc-section\" id=\"What_are_the_syntax_parameter_and_return_type_to_create_date_objects\"><\/span>What are the syntax, parameter, and return type to create date objects?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-syntax\"><span class=\"ez-toc-section\" id=\"Syntax\"><\/span>Syntax<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">import module\nvar=datetime.datetime(year,month,day)\nprint(var)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-1\"><span class=\"ez-toc-section\" id=\"Example-2\"><\/span>Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport datetime\na=datetime.datetime(2021,7,26)\nprint(a)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-output-1\"><span class=\"ez-toc-section\" id=\"Output-2\"><\/span>Output<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">2021-07-26 00:00:00<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-parameter-1\"><span class=\"ez-toc-section\" id=\"Parameter-2\"><\/span>Parameter<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong> year-month-date<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-returns-1\"><span class=\"ez-toc-section\" id=\"Returns-2\"><\/span>Returns<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">print the output(date)<\/pre>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-delta monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-icon\" style=\"\"><svg width=\"14\" height=\"19\" viewBox=\"0 0 14 19\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\"><path d=\"M7.875 0.899463C7.875 1.59183 8.0816 2.24711 8.49479 2.8653C8.93229 3.48349 9.44271 4.06458 10.026 4.60859C10.6337 5.15259 11.2292 5.73369 11.8125 6.35188C12.4201 6.97007 12.9306 7.76135 13.3438 8.72572C13.7812 9.66537 14 10.7163 14 11.8785C14 13.832 13.3073 15.5011 11.9219 16.8858C10.5608 18.2953 8.92014 19 7 19C5.07986 19 3.42708 18.2953 2.04167 16.8858C0.680556 15.5011 0 13.832 0 11.8785C0 9.94973 0.668403 8.28062 2.00521 6.87116C2.27257 6.57443 2.58854 6.50024 2.95312 6.64861C3.31771 6.79697 3.5 7.08134 3.5 7.50171V10.6545C3.5 11.3221 3.71875 11.8908 4.15625 12.3607C4.61806 12.8305 5.16493 13.0654 5.79688 13.0654C6.45312 13.0654 7.01215 12.8428 7.47396 12.3978C7.93576 11.9279 8.16667 11.3592 8.16667 10.6916C8.16667 10.2712 8.04514 9.86318 7.80208 9.46754C7.58333 9.0719 7.31597 8.71336 7 8.3919C6.68403 8.07044 6.34375 7.73662 5.97917 7.39043C5.63889 7.04425 5.34722 6.66097 5.10417 6.2406C4.88542 5.82024 4.73958 5.35041 4.66667 4.83114C4.59375 4.31186 4.67882 3.68131 4.92188 2.93948C5.18924 2.17293 5.63889 1.33219 6.27083 0.417277C6.51389 0.0463641 6.84201 -0.0772735 7.25521 0.0463641C7.6684 0.170002 7.875 0.454368 7.875 0.899463Z\" fill=\"#EB5757\"><\/path><\/svg><\/span><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\" style=\"font-size:18px;\" href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\">[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-2-examples-in-python-to-find-time-difference-in-seconds\"><span class=\"ez-toc-section\" id=\"2_Examples_in_Python_to_Find_Time_Difference_in_Seconds\"><\/span>2 Examples in Python to Find Time Difference in Seconds<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-1-1\"><span class=\"ez-toc-section\" id=\"Example_1\"><\/span>Example 1<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport datetime\na = datetime.datetime(2021,7,26,13,7,30)\nb = datetime.datetime(2021,7,25,11,28,30)\nc=a-b\nseconds = c.total_seconds() \nprint('Total difference in seconds: ', seconds)\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\" id=\"h-output-2\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Total difference in seconds:  93178.0<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation<\/strong><\/p>\n\n\n\n<p>First, importing a module datetime. Variable\u00a0<strong>&#8220;a&#8221;<\/strong>\u00a0holds the year, month, date, hours, minutes, seconds, and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Millisecond\" target=\"_blank\" rel=\"noreferrer noopener\">milliseconds<\/a>. Likewise, variable\u00a0<strong>&#8220;b&#8221;<\/strong>\u00a0holds some date and time. Now we are going to calculate the difference between the two values. <\/p>\n\n\n\n<p>For that, we are using a variable&nbsp;<strong>&#8220;c&#8221;<\/strong>. Variable&nbsp;<strong>c&nbsp;<\/strong>calculates the difference, and it holds the difference between both the values. Usually, the difference is in seconds only. <\/p>\n\n\n\n<p>Next to creating another variable,&nbsp;<strong>&#8220;seconds,&#8221;&nbsp;<\/strong>to get the difference between two times in seconds. And then printing the difference value as an output. This method is useful to get a difference in seconds only. To get an exact value, another method is useful.<\/p>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-kilo monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Popular now<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><span class=\"monsterinsights-inline-popular-posts-border-2\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\">[Fixed] nameerror: name Unicode is not defined<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-2\"><span class=\"ez-toc-section\" id=\"Example_2\"><\/span>Example 2<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom datetime import datetime\nval1=datetime.now()\nval2=datetime(2021,7,25,11,28)\ndifference=val1-val2\nprint(&quot;The time difference in seconds&quot;,difference.total_seconds())\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\" id=\"h-output-3\"><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The time difference in seconds 93178.482513<\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation<\/strong><\/p>\n\n\n\n<p>Here we are importing a module datetime to get the date and time. The difference compared to the previous one is that we are giving as&nbsp;<strong>&#8220;from datetime&#8221;&nbsp;<\/strong>to get the current date and time while importing the module. A variable&nbsp;<strong>val1&nbsp;<\/strong>&nbsp;holds the current date and time. <\/p>\n\n\n\n<p>Another variable,&nbsp;<strong>val2,&nbsp;<\/strong>holds the date and time, which we want to get the difference. A variable&nbsp;<strong>difference<\/strong>&nbsp;holds the difference between val1 and val2. Next, we are giving the print statement to get the difference in time. This method gives the exact output with milliseconds.<\/p>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-golf monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Popular now<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><span class=\"monsterinsights-inline-popular-posts-border-2\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\" style=\"font-size:19px;\" href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\">[Solved] runtimeerror: cuda error: invalid device ordinal<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-frequently-asked-questions-related-to-python-time-difference-in-seconds\"><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions_Related_to_Python_Time_Difference_in_Seconds\"><\/span>Frequently Asked Questions Related to Python Time Difference in Seconds<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1627293383783\"><strong class=\"schema-faq-question\">1. What module is used to get the date and time?<\/strong> <p class=\"schema-faq-answer\"><strong>datetime <\/strong>module is used to get the date and time.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1627293453965\"><strong class=\"schema-faq-question\">2. Is a date is an object in Python?<\/strong> <p class=\"schema-faq-answer\"> A date in Python is not an object, but we can import a module <strong>&#8220;datetime&#8221;<\/strong> to get a date and time.  <\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1627293532359\"><strong class=\"schema-faq-question\">3.Write an example program to get current date and time<\/strong> <p class=\"schema-faq-answer\">import datetime<br\/>a=datetime.datetime.now()<br\/>print(&#8220;Date and time is:&#8221;,a)<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1627293581591\"><strong class=\"schema-faq-question\">4.Write an example program to create date objects?<\/strong> <p class=\"schema-faq-answer\">import datetime<br\/>a=datetime.datetime(2021,7,26)<br\/>print(a)<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1627293644591\"><strong class=\"schema-faq-question\">5. Write an output for the following code<br\/>import datetime<br\/>a=datetime.datetime.now()<br\/>print(&#8220;Date and time is:&#8221;,a) <\/strong> <p class=\"schema-faq-answer\">The output for the following code is<br\/> Date and time is: 2021-07-26 12:29:42.322270 <\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1627296290511\"><strong class=\"schema-faq-question\">6.What is mean by datetime module?<\/strong> <p class=\"schema-faq-answer\"> It combines date and time that contains attributes like the year, month, date, hours, minutes, seconds, and milliseconds. <\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1627296366544\"><strong class=\"schema-faq-question\">7.Is it necessary to install the datetime module?<\/strong> <p class=\"schema-faq-answer\">No, the datetime module is a built-in function that is already available in python. So, it is not necessary to install a datetime module<\/p> <\/div> <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here we have seen different methods to calculate time differences in seconds. The above shown two methods are just an example. There is a lot of methods available to get the time difference in seconds. datetime module has a lot of applications that are useful in our day-to-day life. It is one of the popular modules.<\/p>\n\n\n<div class=\"monsterinsights-widget-popular-posts monsterinsights-widget-popular-posts-charlie monsterinsights-popular-posts-styled monsterinsights-widget-popular-posts-columns-1\"><h2 class=\"monsterinsights-widget-popular-posts-widget-title\"><span class=\"ez-toc-section\" id=\"Trending_Right_Now\"><\/span>Trending Right Now<span class=\"ez-toc-section-end\"><\/span><\/h2><ul class=\"monsterinsights-widget-popular-posts-list\"><li style=\"border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/google-api-client-python\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;font-size:18px;\">Exploring the Power of Google API Client in Python<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/python-genetic-algorithm\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;font-size:18px;\">Mastering Python Genetic Algorithms: A Complete Guide<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/aixi-python\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;font-size:18px;\">Implementation of AIXI in Python<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><li style=\"border-color:#CD3034;\"><a href=\"https:\/\/www.pythonpool.com\/counting-sort-in-python\/\"><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" style=\"color:#000000;font-size:18px;\">Learn How to Implement Counting Sort in Python<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><\/div><\/div><\/a><\/li><\/ul><\/div><p><\/p>","protected":false},"excerpt":{"rendered":"<p>Here we are going to see different methods to calculate python time difference in seconds. We have so many ways, but here we are using &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Different Methods in Python to Calculate Time Difference in Seconds\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#more-14891\" aria-label=\"More on Different Methods in Python to Calculate Time Difference in Seconds\">Read more<\/a><\/p>\n","protected":false},"author":22,"featured_media":14914,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[4368,4367,4369,4370],"class_list":["post-14891","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-python-calculate-time-difference-in-seconds","tag-python-function-to-get-time-difference-in-seconds","tag-python-handle-time-difference-in-seconds","tag-python-time-difference-in-hours-minutes-and-seconds","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Different Methods in Python to Calculate Time Difference in Seconds<\/title>\n<meta name=\"description\" content=\"Here we are going to see how to calculate python time difference in seconds by two different methods using the datetime module.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Different Methods in Python to Calculate Time Difference in Seconds\" \/>\n<meta property=\"og:description\" content=\"Here we are going to see different methods to calculate python time difference in seconds. We have so many ways, but here we are using straightforward\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-27T04:03:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-27T04:03:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Different-Methods-in-Python-to-Calculate-Time-Difference-in-Seconds.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Priyalakshmi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Priyalakshmi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/\"},\"author\":{\"name\":\"Priyalakshmi\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/83e6e22a6c461cc1a8445bb473db2f20\"},\"headline\":\"Different Methods in Python to Calculate Time Difference in Seconds\",\"datePublished\":\"2021-07-27T04:03:43+00:00\",\"dateModified\":\"2021-07-27T04:03:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/\"},\"wordCount\":764,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Different-Methods-in-Python-to-Calculate-Time-Difference-in-Seconds.webp\",\"keywords\":[\"python calculate time difference in seconds\",\"python function to get time difference in seconds\",\"python handle time difference in seconds\",\"python time difference in hours minutes and seconds\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/\",\"name\":\"Different Methods in Python to Calculate Time Difference in Seconds\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Different-Methods-in-Python-to-Calculate-Time-Difference-in-Seconds.webp\",\"datePublished\":\"2021-07-27T04:03:43+00:00\",\"dateModified\":\"2021-07-27T04:03:47+00:00\",\"description\":\"Here we are going to see how to calculate python time difference in seconds by two different methods using the datetime module.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293383783\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293453965\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293532359\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293581591\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293644591\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627296290511\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627296366544\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Different-Methods-in-Python-to-Calculate-Time-Difference-in-Seconds.webp\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Different-Methods-in-Python-to-Calculate-Time-Difference-in-Seconds.webp\",\"width\":1200,\"height\":628,\"caption\":\"python time difference in seconds\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Different Methods in Python to Calculate Time Difference in Seconds\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/83e6e22a6c461cc1a8445bb473db2f20\",\"name\":\"Priyalakshmi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g\",\"caption\":\"Priyalakshmi\"}},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293383783\",\"position\":1,\"url\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293383783\",\"name\":\"1. What module is used to get the date and time?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"<strong>datetime <\/strong>module is used to get the date and time.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293453965\",\"position\":2,\"url\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293453965\",\"name\":\"2. Is a date is an object in Python?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\" A date in Python is not an object, but we can import a module <strong>\\\"datetime\\\"<\/strong> to get a date and time.  \",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293532359\",\"position\":3,\"url\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293532359\",\"name\":\"3.Write an example program to get current date and time\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"import datetime<br \/>a=datetime.datetime.now()<br \/>print(\\\"Date and time is:\\\",a)\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293581591\",\"position\":4,\"url\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293581591\",\"name\":\"4.Write an example program to create date objects?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"import datetime<br \/>a=datetime.datetime(2021,7,26)<br \/>print(a)\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293644591\",\"position\":5,\"url\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293644591\",\"name\":\"5. Write an output for the following code import datetime a=datetime.datetime.now() print(\\\"Date and time is:\\\",a)\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"The output for the following code is<br \/> Date and time is: 2021-07-26 12:29:42.322270 \",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627296290511\",\"position\":6,\"url\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627296290511\",\"name\":\"6.What is mean by datetime module?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\" It combines date and time that contains attributes like the year, month, date, hours, minutes, seconds, and milliseconds. \",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627296366544\",\"position\":7,\"url\":\"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627296366544\",\"name\":\"7.Is it necessary to install the datetime module?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"No, the datetime module is a built-in function that is already available in python. So, it is not necessary to install a datetime module\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Different Methods in Python to Calculate Time Difference in Seconds","description":"Here we are going to see how to calculate python time difference in seconds by two different methods using the datetime module.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/","og_locale":"en_US","og_type":"article","og_title":"Different Methods in Python to Calculate Time Difference in Seconds","og_description":"Here we are going to see different methods to calculate python time difference in seconds. We have so many ways, but here we are using straightforward","og_url":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/","og_site_name":"Python Pool","article_published_time":"2021-07-27T04:03:43+00:00","article_modified_time":"2021-07-27T04:03:47+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Different-Methods-in-Python-to-Calculate-Time-Difference-in-Seconds.webp","type":"image\/webp"}],"author":"Priyalakshmi","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Priyalakshmi","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/"},"author":{"name":"Priyalakshmi","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/83e6e22a6c461cc1a8445bb473db2f20"},"headline":"Different Methods in Python to Calculate Time Difference in Seconds","datePublished":"2021-07-27T04:03:43+00:00","dateModified":"2021-07-27T04:03:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/"},"wordCount":764,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Different-Methods-in-Python-to-Calculate-Time-Difference-in-Seconds.webp","keywords":["python calculate time difference in seconds","python function to get time difference in seconds","python handle time difference in seconds","python time difference in hours minutes and seconds"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/","url":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/","name":"Different Methods in Python to Calculate Time Difference in Seconds","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Different-Methods-in-Python-to-Calculate-Time-Difference-in-Seconds.webp","datePublished":"2021-07-27T04:03:43+00:00","dateModified":"2021-07-27T04:03:47+00:00","description":"Here we are going to see how to calculate python time difference in seconds by two different methods using the datetime module.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293383783"},{"@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293453965"},{"@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293532359"},{"@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293581591"},{"@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293644591"},{"@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627296290511"},{"@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627296366544"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Different-Methods-in-Python-to-Calculate-Time-Difference-in-Seconds.webp","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/07\/Different-Methods-in-Python-to-Calculate-Time-Difference-in-Seconds.webp","width":1200,"height":628,"caption":"python time difference in seconds"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Different Methods in Python to Calculate Time Difference in Seconds"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/83e6e22a6c461cc1a8445bb473db2f20","name":"Priyalakshmi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8f1aadb3b77a89fe5776d41fb52aec63200d0e68e92c71cb9aafac7fb4456696?s=96&d=wavatar&r=g","caption":"Priyalakshmi"}},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293383783","position":1,"url":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293383783","name":"1. What module is used to get the date and time?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"<strong>datetime <\/strong>module is used to get the date and time.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293453965","position":2,"url":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293453965","name":"2. Is a date is an object in Python?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":" A date in Python is not an object, but we can import a module <strong>\"datetime\"<\/strong> to get a date and time.  ","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293532359","position":3,"url":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293532359","name":"3.Write an example program to get current date and time","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"import datetime<br \/>a=datetime.datetime.now()<br \/>print(\"Date and time is:\",a)","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293581591","position":4,"url":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293581591","name":"4.Write an example program to create date objects?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"import datetime<br \/>a=datetime.datetime(2021,7,26)<br \/>print(a)","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293644591","position":5,"url":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627293644591","name":"5. Write an output for the following code import datetime a=datetime.datetime.now() print(\"Date and time is:\",a)","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"The output for the following code is<br \/> Date and time is: 2021-07-26 12:29:42.322270 ","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627296290511","position":6,"url":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627296290511","name":"6.What is mean by datetime module?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":" It combines date and time that contains attributes like the year, month, date, hours, minutes, seconds, and milliseconds. ","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627296366544","position":7,"url":"https:\/\/www.pythonpool.com\/python-time-difference-in-seconds\/#faq-question-1627296366544","name":"7.Is it necessary to install the datetime module?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"No, the datetime module is a built-in function that is already available in python. So, it is not necessary to install a datetime module","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/14891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=14891"}],"version-history":[{"count":22,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/14891\/revisions"}],"predecessor-version":[{"id":14929,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/14891\/revisions\/14929"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/14914"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=14891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=14891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=14891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}