{"id":10015,"date":"2021-07-17T12:36:38","date_gmt":"2021-07-17T07:06:38","guid":{"rendered":"https:\/\/pynative.com\/?p=10015"},"modified":"2021-12-05T10:20:28","modified_gmt":"2021-12-05T04:50:28","slug":"python-current-date-time","status":"publish","type":"post","link":"https:\/\/pynative.com\/python-current-date-time\/","title":{"rendered":"How to Get Current Date and Time in Python"},"content":{"rendered":"\n<p>You&#8217;ll learn how to get the current date and time in Python using the datetime and time module. <\/p>\n\n\n\n<p><strong>By the end of this article, you&#8217;ll learn<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>How to get current date and time in various formats<\/li><li>Get current time in seconds and miliseconds<\/li><li>Get current local time, UTC time, GMT time, ISO time.<\/li><li>How to get current time in the specific timezone<\/li><\/ul>\n\n\n\n<div class=\"wp-block-yoast-seo-table-of-contents yoast-table-of-contents\"><h2>Table of contents<\/h2><ul><li><a href=\"#h-steps-to-get-curent-date-and-time-in-python\" data-level=\"2\">Steps to Get Curent Date and Time in Python<\/a><ul><li><a href=\"#h-example-get-current-datetime-in-python\" data-level=\"3\">Example: Get Current DateTime in Python<\/a><\/li><li><a href=\"#h-extract-current-date-and-time-separately-from-a-datetime-object\" data-level=\"3\">Extract Current Date and Time Separately from a Datetime Object<\/a><\/li><\/ul><\/li><li><a href=\"#h-break-datetime-to-get-current-year-month-day-hour-minute-seconds\" data-level=\"2\">Break DateTime to Get Current Year, Month, Day, Hour, Minute, Seconds<\/a><\/li><li><a href=\"#h-get-current-date-using-the-date-class\" data-level=\"2\">Get Current Date using the Date class<\/a><\/li><li><a href=\"#h-get-current-time-in-python\" data-level=\"2\">Get Current Time in Python<\/a><ul><li><a href=\"#h-current-time-in-seconds-using-time-time\" data-level=\"3\">Current Time in Seconds Using time.time()<\/a><\/li><li><a href=\"#h-current-time-using-time-ctime\" data-level=\"3\">Current Time Using time.ctime()<\/a><\/li><li><a href=\"#h-current-time-using-time-localtime\" data-level=\"3\">Current Time Using time.localtime()<\/a><\/li><li><a href=\"#h-get-current-time-using-datetime-module\" data-level=\"3\">Get Current Time Using Datetime Module<\/a><\/li><\/ul><\/li><li><a href=\"#h-get-current-time-in-milliseconds\" data-level=\"2\">Get Current Time in Milliseconds<\/a><\/li><li><a href=\"#h-get-current-utc-time\" data-level=\"2\">Get Current UTC Time<\/a><\/li><li><a href=\"#h-get-current-time-in-a-specific-timezone\" data-level=\"2\">Get Current Time in a Specific Timezone<\/a><\/li><li><a href=\"#h-get-current-gmt-time\" data-level=\"2\">Get Current GMT Time<\/a><\/li><li><a href=\"#h-get-current-time-in-iso-format\" data-level=\"2\">Get Current Time in ISO Format<\/a><\/li><li><a href=\"#h-conclusion\" data-level=\"2\">Conclusion<\/a><\/li><\/ul><\/div>\n\n\n\n<h2 class=\"stepsh wp-block-heading\" id=\"h-steps-to-get-curent-date-and-time-in-python\">Steps to Get Curent Date and Time in Python<\/h2>\n\n\n\n<div class=\"schema-how-to wp-block-yoast-how-to-block\"><p class=\"schema-how-to-description\">There are many ways to get the current date and time in Python using the built-in and third-party modules. The below steps show how to get the current date and time using the datetime and time module.<\/p> <ol class=\"schema-how-to-steps\"><li class=\"schema-how-to-step\" id=\"how-to-step-1625388181208\"><strong class=\"schema-how-to-step-name\">Import datetime module<\/strong> <p class=\"schema-how-to-step-text\">Python&#8217;s <a href=\"https:\/\/pynative.com\/python\/datetime\/\">datetime module<\/a> provides functions that handle many complex functionalities involving the date and time. Import the <code>datetime<\/code> class using a <code>from datetime import datetime<\/code> statement.<\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1625388466084\"><strong class=\"schema-how-to-step-name\">Use the now() function of a datetime class<\/strong> <p class=\"schema-how-to-step-text\">The <code>datetime.now()<\/code> returns the current local date and time. By default, it represents datetime in <code>YYYY-mm-dd hh:mm:ss.microseconds<\/code> format. <strong>Note<\/strong>: The date and time values are stored as <code>datetime<\/code> objects, The <code>datetime<\/code> object represents both date and time<br\/><\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1625388613943\"><strong class=\"schema-how-to-step-name\">Use the today() function of a Date class<\/strong> <p class=\"schema-how-to-step-text\">Use this step if you want only the current date and not the time. The <code>today()<\/code> method of a date class returns the current local date <\/p> <\/li><li class=\"schema-how-to-step\" id=\"how-to-step-1626443593491\"><strong class=\"schema-how-to-step-name\">Use time module <\/strong> <p class=\"schema-how-to-step-text\">Use the <code>time.time()<\/code> function to get the current time in seconds since the epoch as a floating-point number<\/p> <\/li><\/ol><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-get-current-datetime-in-python\">Example: Get Current DateTime in Python<\/h3>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code1\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> datetime <span class=\"hljs-keyword\">import<\/span> datetime\n\nnow = datetime.now()\nprint(<span class=\"hljs-string\">'Current DateTime:'<\/span>, now)\nprint(<span class=\"hljs-string\">'Type:'<\/span>, type(now))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code1', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code1');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Current DateTime: 2021-07-16 19:17:20.536991\nType: &lt;class 'datetime.datetime'&gt;<\/pre>\n\n\n\n<p>As you can see in the output we got the current date and time in the following format.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">YYYY-MM-DD HH:MM:SS.MS<\/pre>\n\n\n\n<p>Refer to <strong><a href=\"https:\/\/pynative.com\/python-datetime-format-strftime\/\">Python DateTime Format Using Strftime()<\/a><\/strong> If you want to get the current date in various formats.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-extract-current-date-and-time-separately-from-a-datetime-object\">Extract Current Date and Time Separately from a Datetime Object<\/h3>\n\n\n\n<p><strong>Note<\/strong>: in Python, The date and datetime are objects. So when we are manipulating date and time, that means we are actually dealing with objects. <\/p>\n\n\n\n<p>For example, you can extract the current date and time separately from a <code>datetime<\/code> object<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>date()<\/code> function to get the date in <code>yyyy-mm-dd<\/code> format<\/li><li>Use the <code>time()<\/code> function to get the time in the <code>hours:minutes:seconds.microseconds<\/code> format.<\/li><\/ul>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code2\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># import only datetime class<\/span>\n<span class=\"hljs-keyword\">from<\/span> datetime <span class=\"hljs-keyword\">import<\/span> datetime\n\n<span class=\"hljs-comment\"># current datetime<\/span>\nnow = datetime.now()\n\ncurrent_date = now.date()\nprint(<span class=\"hljs-string\">'Date:'<\/span>, current_date)\nprint(type(current_date))\n\ncurrent_time = now.time()\nprint(<span class=\"hljs-string\">'Time'<\/span>, current_time)\nprint(type(current_time))<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code2', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code2');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Date: 2021-07-16\n&lt;class 'datetime.date'&gt;\n\nTime 08:25:05.282627\n&lt;class 'datetime.time'&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-break-datetime-to-get-current-year-month-day-hour-minute-seconds\">Break DateTime to Get Current Year, Month, Day, Hour, Minute, Seconds<\/h2>\n\n\n\n<p>The datetime module provides several attributes to access the induvial component such as a year, month, day, hour, minute, seconds.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n\n<p>In this example, we&#8217;ll break the current datetime and assign them into variables like the year, month, day, hour, minute, seconds, and microseconds.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code3\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> datetime <span class=\"hljs-keyword\">import<\/span> datetime\n\n<span class=\"hljs-comment\"># Get current date and time<\/span>\nnow = datetime.now()\n\n<span class=\"hljs-comment\"># extract attributes <\/span>\nprint(<span class=\"hljs-string\">\"Year:\"<\/span>, now.year)\nprint(<span class=\"hljs-string\">\"Month:\"<\/span>, now.month)\nprint(<span class=\"hljs-string\">\"Day =\"<\/span>, now.day)\n\nprint(<span class=\"hljs-string\">\"Hour:\"<\/span>, now.hour)\nprint(<span class=\"hljs-string\">\"Minute:\"<\/span>, now.minute)\nprint(<span class=\"hljs-string\">\"Second:\"<\/span>, now.second)\nprint(<span class=\"hljs-string\">\"Microsecond:\"<\/span>, now.microsecond)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code3', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code3');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Year: 2021\nMonth: 7\nDay = 16\nHour: 8\nMinute: 28\nSecond: 0\nMicrosecond: 619640<\/pre>\n\n\n\n<p><strong>Note<\/strong>: You can also use the <code>datetime.timetuple()<\/code> to break the datetime an get the induvial attribute for it. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-get-current-date-using-the-date-class\">Get Current Date using the Date class<\/h2>\n\n\n\n<p>Python Datetime module provides the Date class to represent and manipulate the dates. The Date class considers the Gregorian calendar.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Import the <code>date<\/code> class from the datetime module<\/li><li>Use the <code>date.today()<\/code> method to get the current date.<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code4\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> datetime <span class=\"hljs-keyword\">import<\/span> date\n\ntoday = date.today()\nprint(<span class=\"hljs-string\">'Current Date:'<\/span>, today)\n\n<span class=\"hljs-comment\"># Output 2021-07-16<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code4', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code4');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Note<\/strong>: The <code>datetime.today()<\/code> return the current date and time. This method is functionally equivalent to&nbsp;<code>now()<\/code>, but without timezone information.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-get-current-time-in-python\">Get Current Time in Python<\/h2>\n\n\n\n<p>There are many ways to get the current time in Python using the built-in and third-party modules. Python <a href=\"https:\/\/docs.python.org\/3\/library\/time.html\" target=\"_blank\" rel=\"noreferrer noopener\">time module<\/a> provides various functions to get the current time and perform time-related activities.  We will see each one by one<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-current-time-in-seconds-using-time-time\">Current Time in Seconds Using <code>time.time()<\/code><\/h3>\n\n\n\n<p>Use the <code>time.time()<\/code> function to get the current time in seconds since the epoch as a floating-point number.<\/p>\n\n\n\n<p>This method returns the current <a href=\"https:\/\/pynative.com\/python-timestamp\/\">timestamp<\/a> in a floating-point number that represents the number of seconds since Jan 1, 1970, 00:00:00.<\/p>\n\n\n\n<p>It returns the current time in <code>seconds.microseconds<\/code> format.<\/p>\n\n\n\n<p><strong>Example<\/strong>: <\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code5\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> time\n\n<span class=\"hljs-comment\"># get current time in seconds<\/span>\nt = time.time()\nprint(<span class=\"hljs-string\">'Time:'<\/span>, t)\n\n<span class=\"hljs-comment\"># Output 1626488018.0199707<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code5', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code5');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p>You can use this timestamp to <a href=\"https:\/\/pynative.com\/python-timestamp\/#h-convert-timestamp-to-datetime-format\">convert it into a datetime object<\/a> also.<\/p>\n\n\n\n<p>Current Time in MiliSeconds Using <code>time.time()<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-current-time-using-time-ctime\">Current Time Using <code>time.ctime()<\/code><\/h3>\n\n\n\n<p>Use the <code>time.ctime()<\/code> function to display the current time in a human-readable format. This function represents the current time in the operating system preferred way. The output may vary as per the operating system.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> time\n\n<span class=\"hljs-comment\"># get current time<\/span>\nprint(<span class=\"hljs-string\">'Current Time:'<\/span>, time.ctime(time.time()))\n\n<span class=\"hljs-comment\"># Output Sat Jul 17 07:07:09 2021<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-current-time-using-time-localtime\">Current Time Using <code>time.localtime()<\/code><\/h3>\n\n\n\n<p>Use the <code>time.localtime()<\/code> function to return the current time expressed in seconds since the epoch to a local time in the a&nbsp;<code>struct_time<\/code>&nbsp;format. <\/p>\n\n\n\n<p>You can access year, month, day, hour, minute, seconds, and microseconds from a <code>struct_time<\/code>.<\/p>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code6\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> time\n\n<span class=\"hljs-comment\"># get current local time<\/span>\nt = time.localtime(time.time())\n\nprint(<span class=\"hljs-string\">'Current Time:'<\/span>, t)\nprint(<span class=\"hljs-string\">'Year:'<\/span>, t.tm_year)\nprint(<span class=\"hljs-string\">'Month:'<\/span>, t.tm_mday)\nprint(<span class=\"hljs-string\">'Day:'<\/span>, t.tm_mday)\n\nprint(<span class=\"hljs-string\">'Minutes:'<\/span>, t.tm_min)\nprint(<span class=\"hljs-string\">'Hours:'<\/span>, t.tm_hour)\nprint(<span class=\"hljs-string\">'Seconds:'<\/span>, t.tm_sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code6', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code6');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Current Time: time.struct_time(tm_year=2021, tm_mon=7, tm_mday=17, tm_hour=7, tm_min=13, tm_sec=27, tm_wday=5, tm_yday=198, tm_isdst=0)\n\nYear: 2021\nMonth: 17\nDay: 17\nMinutes: 13\nHours: 7\nSeconds: 27<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-get-current-time-using-datetime-module\">Get Current Time Using Datetime Module<\/h3>\n\n\n\n<p>The <code>datetime.now()<\/code> method of a datetime class returns the current time in a human-readable format. It internally uses the <code>time.localtime()<\/code> without the timezone info (if not given).<\/p>\n\n\n\n<p>Also, you can access the individual attribute such as hour, minutes, seconds, and microseconds<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code7\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> datetime <span class=\"hljs-keyword\">import<\/span> datetime\n\nnow = datetime.now()\nprint(<span class=\"hljs-string\">'Current DateTime:'<\/span>, now)\n\nprint(<span class=\"hljs-string\">'Current Time:'<\/span>, now.time())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code7', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code7');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Current DateTime: 2021-07-17 07:23:29.454555\nCurrent Time: 07:23:29.454555<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-get-current-time-in-milliseconds\">Get Current Time in Milliseconds<\/h2>\n\n\n\n<p>There is no specific attribute or method in Python to get the current time in milliseconds. However, as milliseconds are three decimal places away from seconds, we can convert seconds to milliseconds by multiplying seconds by 1000.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>time.time()<\/code> to get the current time in seconds since the epoch as a floating point number<\/li><li>Multiply time by 1000 to get current time in milliseconds<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code8\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> time\n\nt = time.time()\nml = int(t * <span class=\"hljs-number\">1000<\/span>)\nprint(<span class=\"hljs-string\">'Current time in milliseconds:'<\/span>, ml)\n\n<span class=\"hljs-comment\"># Output 1626498089547<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code8', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code8');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-get-current-utc-time\">Get Current UTC Time<\/h2>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Coordinated_Universal_Time\" target=\"_blank\" rel=\"noreferrer noopener\">UTC<\/a> \u2013 Coordinated Universal Time is the common time standard across the world. So, in Python, to work with the timezone without any issues, it is recommended to use the UTC as your base timezone.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>datetime.now()<\/code> method to get the current time<\/li><li>Use the timezone class with UTC instance with a <code>now()<\/code> method to to get the current UTC time in Python<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code9\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> datetime <span class=\"hljs-keyword\">import<\/span> datetime, timezone\n\nnow = datetime.now(timezone.utc)\nprint(<span class=\"hljs-string\">'Current UTC Time:'<\/span>, now)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code9', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code9');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Current UTC Time: 2021-07-17 01:59:25.017849+00:00<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-get-current-time-in-a-specific-timezone\">Get Current Time in a Specific Timezone<\/h2>\n\n\n\n<p>Use the third-party <strong>pytz<\/strong> module to get the current time of any timezone.<\/p>\n\n\n\n<p><strong>Steps<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Install pytz module using the <code>pip install pytz<\/code><\/li><li>Use the&nbsp;<code>pytz.timezone('region_name')<\/code>&nbsp;function to create the timezone object<\/li><li>Use&nbsp;<code>datetime.now(timezone_obj)<\/code>&nbsp;function to get the current datetime with timezone<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n\n<p>Refer to our guide on <a href=\"https:\/\/pynative.com\/python-timezone\/\"><strong>working with timezones in Python<\/strong><\/a>.<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code10\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> datetime <span class=\"hljs-keyword\">import<\/span> datetime\n<span class=\"hljs-keyword\">import<\/span> pytz\n\ndt_us_central = datetime.now(pytz.timezone(<span class=\"hljs-string\">'US\/Central'<\/span>))\nprint(<span class=\"hljs-string\">\"US Central Current DateTime:\"<\/span>, dt_us_central.strftime(<span class=\"hljs-string\">\"%Y:%m:%d %H:%M:%S %Z %z\"<\/span>))\n\n<span class=\"hljs-comment\"># extract components<\/span>\nprint(<span class=\"hljs-string\">'TimeZone Name:'<\/span>, dt_us_central.tzname())\nprint(<span class=\"hljs-string\">'UTC offset:'<\/span>, dt_us_central.utcoffset())<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code10', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code10');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">US Central Current DateTime: 2021:07:16 21:06:18 CDT -0500\nTimeZone Name: CDT\nUTC offset: -1 day, 19:00:00<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-get-current-gmt-time\">Get Current GMT Time<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Greenwich Mean Time or GMT is clock time at the Royal Observatory in Greenwich, London. It is not affected by Summer Time (Daylight Saving Time) clock changes.<\/p><cite><a href=\"https:\/\/greenwichmeantime.com\/what-is-gmt\/\" target=\"_blank\" rel=\"noreferrer noopener\">GMT<\/a><\/cite><\/blockquote>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the time.gmtime() method to get the current GMT time in Python<\/li><li>Pass the time in seconds to this method to get the GMT representation of a time<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code11\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> time\n\n<span class=\"hljs-comment\"># current GMT Time<\/span>\ngmt = time.gmtime(time.time())\n\nprint(<span class=\"hljs-string\">'GMT Time:'<\/span>, gmt)\n<span class=\"hljs-comment\"># Hours:minutes:Seconds<\/span>\nprint(gmt.tm_hour, <span class=\"hljs-string\">':'<\/span>, gmt.tm_min, <span class=\"hljs-string\">':'<\/span>, gmt.tm_sec)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code11', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code11');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">GMT Time: time.struct_time(tm_year=2021, tm_mon=7, tm_mday=17, tm_hour=6, tm_min=51, tm_sec=57, tm_wday=5, tm_yday=198, tm_isdst=0)\n\n6:51:57<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-get-current-time-in-iso-format\">Get Current Time in ISO Format<\/h2>\n\n\n\n<p>Use the <code>datetime.isoformat()<\/code> method to get the current date and time in <a href=\"https:\/\/en.wikipedia.org\/wiki\/ISO_8601\" target=\"_blank\" rel=\"noreferrer noopener\">ISO format<\/a>.<\/p>\n\n\n\n<p>Use <code>isoformat()<\/code> method on a <code>datetime.now()<\/code> instance to get the current date and time in the following ISO 8601 format:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>YYYY-MM-DDTHH:MM:SS.ffffff<\/code>, if&nbsp;<code>microsecond<\/code>&nbsp;is not 0<\/li><li><code>YYYY-MM-DDTHH:MM:SS<\/code>, if&nbsp;<code>microsecond<\/code>&nbsp;is 0<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"hljstoolbar\"><pre id=\"code12\"  class=\"wp-block-code language-python\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> datetime <span class=\"hljs-keyword\">import<\/span> datetime\n\ndt_iso = datetime.now().isoformat()\nprint(<span class=\"hljs-string\">'Current DateTime in ISO:'<\/span>, dt_iso)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><button class=\"hljs-copy-button\" onclick=\"copy_code('code12', this);\"><i class=\"far fa-copy1\" aria-hidden=\"true\"><\/i><\/button><button class=\"hljs-run-button\" onclick=\"run_code('code12');\"><i class=\"far fa-play-circle\" aria-hidden=\"true\"><\/i> Run<\/button><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Current DateTime in ISO: 2021-07-17T10:42:44.106976<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<p>In this article, we learned to use the following Python functions to get the current date and time.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Function<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>datetime.now()<\/code><\/td><td>Get the current local datetime, with no timezone information<\/td><\/tr><tr><td><code>date.today()<\/code><\/td><td>Get the current date<\/td><\/tr><tr><td><code>time.time()<\/code><\/td><td>Get the current&nbsp;time in seconds.&nbsp;It returns the number of seconds since Jan 1, 1970, 00:00:00.<\/td><\/tr><tr><td><code>time.ctime()<\/code><\/td><td>Get the current time in a human-readable format<\/td><\/tr><tr><td><code>time.localtime()<\/code><\/td><td>Get the current time expressed in seconds since the epoch to a local time in the a&nbsp;<code>struct_time<\/code>&nbsp;format<\/td><\/tr><tr><td><code>int(time.time() *1000)<\/code><\/td><td>Get the current&nbsp;time in milliseconds<\/td><\/tr><tr><td><code>datetime.now(timezone.utc)<\/code><\/td><td>Get the current UTC time<\/td><\/tr><tr><td><code>time.gmtime(time.time())<\/code><\/td><td>Get the current GMT time<\/td><\/tr><tr><td><code>datetime.now(pytz.timezone('tz_name'))<\/code><\/td><td>Get the current time in a specific timezone<\/td><\/tr><tr><td><code>datetime.now().isoformat()<\/code><\/td><td>Get the current datetime in ISO format<\/td><\/tr><\/tbody><\/table><figcaption>Functions to get current date and time in Python<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;ll learn how to get the current date and time in Python using the datetime and time module. By the end of this article, you&#8217;ll learn How to get current date and time in various formats Get current time in seconds and miliseconds Get current local time, UTC time, GMT time, ISO time. How to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[22,35],"tags":[38],"class_list":{"0":"post-10015","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python","7":"category-datetime","8":"tag-date-time","9":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Python Get Current Date and Time [8 Ways] &#8211; PYnative<\/title>\n<meta name=\"description\" content=\"Get Current Date and Time in Python. Also, get the current local time, UTC time, GMT time, ISO time, and current time in specific timezone\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/pynative.com\/python-current-date-time\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Get Current Date and Time in Python\" \/>\n<meta property=\"og:description\" content=\"Get Current Date and Time in Python. Also, get the current local time, UTC time, GMT time, ISO time, and current time in specific timezone\" \/>\n<meta property=\"og:url\" content=\"https:\/\/pynative.com\/python-current-date-time\/\" \/>\n<meta property=\"og:site_name\" content=\"PYnative\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-17T07:06:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-05T04:50:28+00:00\" \/>\n<meta name=\"author\" content=\"Vishal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@PyNative\" \/>\n<meta name=\"twitter:site\" content=\"@PyNative\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/\"},\"author\":{\"name\":\"Vishal\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"headline\":\"How to Get Current Date and Time in Python\",\"datePublished\":\"2021-07-17T07:06:38+00:00\",\"dateModified\":\"2021-12-05T04:50:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/\"},\"wordCount\":1244,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"keywords\":[\"DateTime\"],\"articleSection\":[\"Python\",\"Python DateTime\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/#respond\"]}],\"accessibilityFeature\":[\"tableOfContents\"]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/\",\"name\":\"Python Get Current Date and Time [8 Ways] &#8211; PYnative\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\"},\"datePublished\":\"2021-07-17T07:06:38+00:00\",\"dateModified\":\"2021-12-05T04:50:28+00:00\",\"description\":\"Get Current Date and Time in Python. Also, get the current local time, UTC time, GMT time, ISO time, and current time in specific timezone\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/pynative.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\\\/\\\/pynative.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python DateTime\",\"item\":\"https:\\\/\\\/pynative.com\\\/python\\\/datetime\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"How to Get Current Date and Time in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/pynative.com\\\/#website\",\"url\":\"https:\\\/\\\/pynative.com\\\/\",\"name\":\"PYnative\",\"description\":\"Python Programming\",\"publisher\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/pynative.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/pynative.com\\\/#\\\/schema\\\/person\\\/64b55d5bde2265918c5a9931de4de71f\",\"name\":\"Vishal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"url\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"contentUrl\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\",\"width\":968,\"height\":1065,\"caption\":\"Vishal\"},\"logo\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/wp-content\\\/uploads\\\/2019\\\/01\\\/vishalHule.jpg\"},\"description\":\"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!\",\"sameAs\":[\"https:\\\/\\\/pynative.com\"]},{\"@type\":\"HowTo\",\"@id\":\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/#howto-1\",\"name\":\"How to Get Current Date and Time in Python\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/#article\"},\"description\":\"There are many ways to get the current date and time in Python using the built-in and third-party modules. The below steps show how to get the current date and time using the datetime and time module.\",\"step\":[{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/#how-to-step-1625388181208\",\"name\":\"Import datetime module\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"Python's datetime module provides functions that handle many complex functionalities involving the date and time. Import the datetime class using a from datetime import datetime statement.\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/#how-to-step-1625388466084\",\"name\":\"Use the now() function of a datetime class\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"The datetime.now() returns the current local date and time. By default, it represents datetime in YYYY-mm-dd hh:mm:ss.microseconds format. Note: The date and time values are stored as datetime objects, The datetime object represents both date and time\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/#how-to-step-1625388613943\",\"name\":\"Use the today() function of a Date class\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"Use this step if you want only the current date and not the time. The today() method of a date class returns the current local date\"}]},{\"@type\":\"HowToStep\",\"url\":\"https:\\\/\\\/pynative.com\\\/python-current-date-time\\\/#how-to-step-1626443593491\",\"name\":\"Use time module\",\"itemListElement\":[{\"@type\":\"HowToDirection\",\"text\":\"Use the time.time() function to get the current time in seconds since the epoch as a floating-point number\"}]}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Get Current Date and Time [8 Ways] &#8211; PYnative","description":"Get Current Date and Time in Python. Also, get the current local time, UTC time, GMT time, ISO time, and current time in specific timezone","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:\/\/pynative.com\/python-current-date-time\/","og_locale":"en_US","og_type":"article","og_title":"How to Get Current Date and Time in Python","og_description":"Get Current Date and Time in Python. Also, get the current local time, UTC time, GMT time, ISO time, and current time in specific timezone","og_url":"https:\/\/pynative.com\/python-current-date-time\/","og_site_name":"PYnative","article_published_time":"2021-07-17T07:06:38+00:00","article_modified_time":"2021-12-05T04:50:28+00:00","author":"Vishal","twitter_card":"summary_large_image","twitter_creator":"@PyNative","twitter_site":"@PyNative","twitter_misc":{"Written by":"Vishal","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/pynative.com\/python-current-date-time\/#article","isPartOf":{"@id":"https:\/\/pynative.com\/python-current-date-time\/"},"author":{"name":"Vishal","@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"headline":"How to Get Current Date and Time in Python","datePublished":"2021-07-17T07:06:38+00:00","dateModified":"2021-12-05T04:50:28+00:00","mainEntityOfPage":{"@id":"https:\/\/pynative.com\/python-current-date-time\/"},"wordCount":1244,"commentCount":2,"publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"keywords":["DateTime"],"articleSection":["Python","Python DateTime"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/pynative.com\/python-current-date-time\/#respond"]}],"accessibilityFeature":["tableOfContents"]},{"@type":"WebPage","@id":"https:\/\/pynative.com\/python-current-date-time\/","url":"https:\/\/pynative.com\/python-current-date-time\/","name":"Python Get Current Date and Time [8 Ways] &#8211; PYnative","isPartOf":{"@id":"https:\/\/pynative.com\/#website"},"datePublished":"2021-07-17T07:06:38+00:00","dateModified":"2021-12-05T04:50:28+00:00","description":"Get Current Date and Time in Python. Also, get the current local time, UTC time, GMT time, ISO time, and current time in specific timezone","breadcrumb":{"@id":"https:\/\/pynative.com\/python-current-date-time\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/pynative.com\/python-current-date-time\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/pynative.com\/python-current-date-time\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/pynative.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/pynative.com\/python\/"},{"@type":"ListItem","position":3,"name":"Python DateTime","item":"https:\/\/pynative.com\/python\/datetime\/"},{"@type":"ListItem","position":4,"name":"How to Get Current Date and Time in Python"}]},{"@type":"WebSite","@id":"https:\/\/pynative.com\/#website","url":"https:\/\/pynative.com\/","name":"PYnative","description":"Python Programming","publisher":{"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/pynative.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/pynative.com\/#\/schema\/person\/64b55d5bde2265918c5a9931de4de71f","name":"Vishal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","url":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","contentUrl":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg","width":968,"height":1065,"caption":"Vishal"},"logo":{"@id":"https:\/\/pynative.com\/wp-content\/uploads\/2019\/01\/vishalHule.jpg"},"description":"Founder of PYnative.com. I am a Python developer and I love to write articles to help developers. | All the best for your future Python endeavors!","sameAs":["https:\/\/pynative.com"]},{"@type":"HowTo","@id":"https:\/\/pynative.com\/python-current-date-time\/#howto-1","name":"How to Get Current Date and Time in Python","mainEntityOfPage":{"@id":"https:\/\/pynative.com\/python-current-date-time\/#article"},"description":"There are many ways to get the current date and time in Python using the built-in and third-party modules. The below steps show how to get the current date and time using the datetime and time module.","step":[{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-current-date-time\/#how-to-step-1625388181208","name":"Import datetime module","itemListElement":[{"@type":"HowToDirection","text":"Python's datetime module provides functions that handle many complex functionalities involving the date and time. Import the datetime class using a from datetime import datetime statement."}]},{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-current-date-time\/#how-to-step-1625388466084","name":"Use the now() function of a datetime class","itemListElement":[{"@type":"HowToDirection","text":"The datetime.now() returns the current local date and time. By default, it represents datetime in YYYY-mm-dd hh:mm:ss.microseconds format. Note: The date and time values are stored as datetime objects, The datetime object represents both date and time"}]},{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-current-date-time\/#how-to-step-1625388613943","name":"Use the today() function of a Date class","itemListElement":[{"@type":"HowToDirection","text":"Use this step if you want only the current date and not the time. The today() method of a date class returns the current local date"}]},{"@type":"HowToStep","url":"https:\/\/pynative.com\/python-current-date-time\/#how-to-step-1626443593491","name":"Use time module","itemListElement":[{"@type":"HowToDirection","text":"Use the time.time() function to get the current time in seconds since the epoch as a floating-point number"}]}],"inLanguage":"en-US"}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"Vishal","author_link":"https:\/\/pynative.com\/author\/vishal\/"},"_links":{"self":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/10015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/comments?post=10015"}],"version-history":[{"count":0,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/posts\/10015\/revisions"}],"wp:attachment":[{"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/media?parent=10015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/categories?post=10015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pynative.com\/wp-json\/wp\/v2\/tags?post=10015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}