{"id":3241,"date":"2023-10-21T08:04:23","date_gmt":"2023-10-21T02:34:23","guid":{"rendered":"https:\/\/python-programs.com\/?p=3241"},"modified":"2023-11-10T11:52:58","modified_gmt":"2023-11-10T06:22:58","slug":"python-how-to-get-current-date-and-time-or-timestamp","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/","title":{"rendered":"Python : How to get Current date and time or timestamp ?"},"content":{"rendered":"

Timestamp is a order of characters or encrypted information point out when a certain event occurred, generally it gives date and time of day, sometimes exact to a small fraction of a second.<\/p>\n

Get current timestamp using Python<\/h3>\n

So here we will show you various methods to find out latest date ,time or timestamp in Python.We will import functions from modules time,<\/em> calendar and date.<\/p>\n

1. Using module time :<\/h3>\n

The function time, return the time in seconds since the date & time as a floating point number.<\/p>\n

#using time module\r\nimport time\r\n  \r\n# ts stores the time in seconds\r\nts = time.time()\r\n  \r\n# print the current timestamp\r\nprint(ts)\r\n<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/article2.py\r\n1618989907.6277814\r\n<\/pre>\n

 <\/p>\n

2. Using module datetime :<\/h3>\n

The datetime module arranges classes for manipulating dates and times. The function datetime.datetime.now which return number of seconds.<\/p>\n

# using datetime module\r\nimport datetime;\r\n  \r\n# ct stores current time\r\nct = datetime.datetime.now()\r\nprint(\"current time:-\", ct)\r\n  \r\n# ts store timestamp of current time\r\nts = ct.timestamp()\r\nprint(\"timestamp:-\", ts)<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/article2.py\r\ncurrent time:- 2021-04-21 13:21:06.256878\r\ntimestamp:- 1618991466.25687<\/pre>\n

3. Using module calendar :<\/h3>\n

We are able to understand timestamp by combining multiple functions by multiple modules.<\/p>\n

In this topic we are going to use function calendar.timegm to change tuple showing current time.<\/p>\n

# using calendar module\r\n# using time module\r\nimport calendar;\r\nimport time;\r\n  \r\n# gmt stores current gmtime\r\ngmt = time.gmtime()\r\nprint(\"gmt:-\", gmt)\r\n  \r\n# ts stores timestamp\r\nts = calendar.timegm(gmt)\r\nprint(\"timestamp:-\", ts)<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/article2.py\r\ngmt:- time.struct_time(tm_year=2021, tm_mon=4, tm_mday=21, tm_hour=8, tm_min=18, tm_sec=37, tm_wday=2, tm_yday=111, tm_isdst=0)\r\ntimestamp:- 1618993117\r\n<\/pre>\n

For Current Date only:<\/h3>\n

Let assume we do not want complete current timestamp, we\u00a0 just want\u00a0 current date only. Then what to do ?<\/p>\n

datetime class consists of\u00a0 2 other classes date & time . We can get date object from a datetime object.<\/p>\n

from datetime import datetime \r\ndateTimeObj = datetime.now() \r\ndateObj = dateTimeObj.date()\r\nprint(dateObj)<\/pre>\n

Output:<\/h3>\n
RESTART: C:\/Users\/HP\/Desktop\/article2.py\r\n2021-04-21\r\n\r\n<\/pre>\n
\n
\n
\n
\n
\n
\n
\n
\n

For current Time only:<\/h3>\n

So here is the process if you want to know only current time.<\/p>\n

As datetime module provides a datetime.time class too. We can get time object from a datetime object i.e.<\/p>\n<\/div>\n<\/div>\n<\/article>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n

from datetime import datetime\r\nimport time\r\n\r\ndateTimeObj = datetime.now()\r\n# get the time object from datetime object\r\ntimeObj = dateTimeObj.time()\r\n\r\nprint(timeObj)\r\n<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/article2.py\r\n18:14:51.276231\r\n<\/pre>\n

For Current Timestamp using time.time():<\/h3>\n

Python gives us a module time & it also\u00a0 has a function time() that returns the number of seconds.<\/p>\n

from datetime import datetime\r\nimport time\r\ndateTimeObj = datetime.now()\r\n# get the time object from datetime object\r\ntimeObj = dateTimeObj.time()\r\nsecondsSinceEpoch = time.time()\r\ntimeObj = time.localtime(secondsSinceEpoch)\r\n# get the current timestamp elements from struct_time object i.e.\r\nprint('Current TimeStamp is : %d-%d-%d %d:%d:%d' % (\r\ntimeObj.tm_mday, timeObj.tm_mon, timeObj.tm_year, timeObj.tm_hour, timeObj.tm_min, timeObj.tm_sec))\r\n\r\n<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/article2.py\r\nCurrent TimeStamp is : 21-4-2021 18:37:31\r\n<\/pre>\n

Get Current Timestamp using time.ctime():<\/h3>\n

Time module has some other function time.ctime() .<\/p>\n

from datetime import datetime\r\nimport time\r\ndef ctime(seconds=None):\r\n timeStr = time.ctime()\r\n print('Current Timestamp : ', timeStr)\r\nprint(ctime())<\/pre>\n

Output:<\/strong><\/p>\n

RESTART: C:\/Users\/HP\/Desktop\/article2.py\r\nCurrent Timestamp : Wed Apr 21 18:46:44 2021\r\n<\/pre>\n

Conclusion:<\/h3>\n

So In this article we have seen multiple ways to find out current Date & Time or Timestamp.We have also seen how to use ct.time() and time.time().<\/p>\n","protected":false},"excerpt":{"rendered":"

Timestamp is a order of characters or encrypted information point out when a certain event occurred, generally it gives date and time of day, sometimes exact to a small fraction of a second. Get current timestamp using Python So here we will show you various methods to find out latest date ,time or timestamp in …<\/p>\n

Python : How to get Current date and time or timestamp ?<\/span> Read More »<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython : How to get Current date and time or timestamp ? - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python : How to get Current date and time or timestamp ? - Python Programs\" \/>\n<meta property=\"og:description\" content=\"Timestamp is a order of characters or encrypted information point out when a certain event occurred, generally it gives date and time of day, sometimes exact to a small fraction of a second. Get current timestamp using Python So here we will show you various methods to find out latest date ,time or timestamp in … Python : How to get Current date and time or timestamp ? Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-21T02:34:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:22:58+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shikha Mishra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/\",\"name\":\"Python : How to get Current date and time or timestamp ? - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-21T02:34:23+00:00\",\"dateModified\":\"2023-11-10T06:22:58+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python : How to get Current date and time or timestamp ?\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/7a690ac49394cc96d2e839bf9a746594\"},\"headline\":\"Python : How to get Current date and time or timestamp ?\",\"datePublished\":\"2023-10-21T02:34:23+00:00\",\"dateModified\":\"2023-11-10T06:22:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/#webpage\"},\"wordCount\":309,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/7a690ac49394cc96d2e839bf9a746594\",\"name\":\"Shikha Mishra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/10a27cfafdf21564c686b80411336ece?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/10a27cfafdf21564c686b80411336ece?s=96&d=mm&r=g\",\"caption\":\"Shikha Mishra\"},\"url\":\"https:\/\/python-programs.com\/author\/shikha\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python : How to get Current date and time or timestamp ? - Python Programs","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:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/","og_locale":"en_US","og_type":"article","og_title":"Python : How to get Current date and time or timestamp ? - Python Programs","og_description":"Timestamp is a order of characters or encrypted information point out when a certain event occurred, generally it gives date and time of day, sometimes exact to a small fraction of a second. Get current timestamp using Python So here we will show you various methods to find out latest date ,time or timestamp in … Python : How to get Current date and time or timestamp ? Read More »","og_url":"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-21T02:34:23+00:00","article_modified_time":"2023-11-10T06:22:58+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Shikha Mishra","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/#webpage","url":"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/","name":"Python : How to get Current date and time or timestamp ? - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-21T02:34:23+00:00","dateModified":"2023-11-10T06:22:58+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python : How to get Current date and time or timestamp ?"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/7a690ac49394cc96d2e839bf9a746594"},"headline":"Python : How to get Current date and time or timestamp ?","datePublished":"2023-10-21T02:34:23+00:00","dateModified":"2023-11-10T06:22:58+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/#webpage"},"wordCount":309,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-how-to-get-current-date-and-time-or-timestamp\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/7a690ac49394cc96d2e839bf9a746594","name":"Shikha Mishra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/10a27cfafdf21564c686b80411336ece?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/10a27cfafdf21564c686b80411336ece?s=96&d=mm&r=g","caption":"Shikha Mishra"},"url":"https:\/\/python-programs.com\/author\/shikha\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3241"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=3241"}],"version-history":[{"count":2,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3241\/revisions"}],"predecessor-version":[{"id":3300,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3241\/revisions\/3300"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}