{"id":4804,"date":"2019-05-30T13:08:48","date_gmt":"2019-05-30T07:38:48","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=4804"},"modified":"2026-11-02T12:51:36","modified_gmt":"2026-11-02T07:21:36","slug":"python-date-time-strftime","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/python-date-time-strftime\/","title":{"rendered":"How to Use Python DateTime and strftime() to Format Date and Time"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Post Date: May 30, 2019<br>Last Updated: November 02, 2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python provides several built-in functions and modules to work with dates and times. The <code>datetime<\/code> module helps developers retrieve the current date and time, extract individual date and time values, and format them according to specific requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before working with date and time functions, you need to import the required classes from the <code>datetime<\/code> module.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we will learn how to import the <code>datetime<\/code> module, print the current date and time, retrieve the day of the week, and format date and time values using the <code>strftime()<\/code> method.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before getting started, you should have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Basic knowledge of Python programming.<\/li>\n\n\n\n<li>Python installed on your system.<\/li>\n\n\n\n<li>A code editor or Python IDE.<\/li>\n\n\n\n<li>Basic understanding of Python functions and modules.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Import the DateTime Module<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python provides the <code>datetime<\/code> module to work with dates and times. You can import individual classes depending on your requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Import the date class:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from datetime import date<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>date<\/code> class is used to work with dates, including the year, month, and day.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Import the time class:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">from datetime import time<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>time<\/code> class is used to work with time values, including hours, minutes, seconds, and microseconds.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Import the datetime class:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> from datetime import datetime<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>datetime<\/code> class combines date and time information and provides methods to retrieve the current date and time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Print Date and Time Using datetime<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>datetime.now()<\/code> method returns the current local date and time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the following example, we use the <code>datetime<\/code> module to retrieve the current date, current time, and day of the week.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Complete Python Code:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\nfrom datetime import datetime\n\ndef main():\n\n    # Print today's date and time\n    todaydate = datetime.now()\n    print(todaydate)\n\n    # Get the current time\n    current_time = datetime.now().time()\n    print(\"The current time is\", current_time)\n\n    # Get the weekday number\n    wd = todaydate.weekday()\n\n    # Monday = 0 and Sunday = 6\n    days = [\n        \"Monday\",\n        \"Tuesday\",\n        \"Wednesday\",\n        \"Thursday\",\n        \"Friday\",\n        \"Saturday\",\n        \"Sunday\"\n    ]\n\n    print(\"Today is day number %d\" % wd)\n    print(\"Which is a \" + days[wd])\n\n\nif __name__ == \"__main__\":\n    main()\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>datetime.now()<\/code> retrieves the current local date and time.<\/li>\n\n\n\n<li><code>datetime.now().time()<\/code> extracts the current time.<\/li>\n\n\n\n<li><code>todaydate.weekday()<\/code> returns the weekday number.<\/li>\n\n\n\n<li>The weekday numbering starts with Monday as <code>0<\/code> and ends with Sunday as <code>6<\/code>.<\/li>\n\n\n\n<li>The <code>days<\/code> list contains the weekday names, which are accessed using the returned weekday number.<\/li>\n\n\n\n<li>The <code>if __name__ == \"__main__\":<\/code> statement ensures that the <code>main()<\/code> function runs when the script is executed directly.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The output displays the current date, current time, and corresponding weekday. The exact values depend on when you execute the program.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-18.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"692\" height=\"145\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-18.png\" alt=\"Python datetime example displaying the current date, current time, and weekday using the datetime module.\" class=\"wp-image-12736\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-18.png 692w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-18-300x63.png 300w\" sizes=\"(max-width: 692px) 100vw, 692px\" \/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Format Date and Time Using strftime()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">So far, we have learned how to use the <code>datetime<\/code> object to retrieve date and time information.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let&#8217;s explore the <code>strftime()<\/code> method, which allows you to format date and time values into readable strings using specific format codes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>strftime()<\/code> method converts a date or time object into a formatted string.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Complete Python Code:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\nfrom datetime import datetime\n\ndef main():\n\n    # Get the current date and time\n    now = datetime.now()\n\n    # Display local date and time\n    print(now.strftime(\"%c\"))\n\n    # Display local date\n    print(now.strftime(\"%x\"))\n\n    # Display local time\n    print(now.strftime(\"%X\"))\n\n    # Display time in 12-hour format\n    print(now.strftime(\"%I:%M:%S %p\"))\n\n    # Display time in 24-hour format\n    print(now.strftime(\"%H:%M\"))\n\n\nif __name__ == \"__main__\":\n    main()\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation of strftime() Format Codes:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>%c<\/code> \u2013 Displays the local date and time.<\/li>\n\n\n\n<li><code>%x<\/code> \u2013 Displays the local date.<\/li>\n\n\n\n<li><code>%X<\/code> \u2013 Displays the local time.<\/li>\n\n\n\n<li><code>%I<\/code> \u2013 Represents the hour in 12-hour format.<\/li>\n\n\n\n<li><code>%H<\/code> \u2013 Represents the hour in 24-hour format.<\/li>\n\n\n\n<li><code>%M<\/code> \u2013 Represents minutes.<\/li>\n\n\n\n<li><code>%S<\/code> \u2013 Represents seconds.<\/li>\n\n\n\n<li><code>%p<\/code> \u2013 Displays the local AM or PM indicator.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For example, <code>\"%I:%M:%S %p\"<\/code> displays the time in a 12-hour format, while <code>\"%H:%M\"<\/code> displays it in a 24-hour format.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The program displays the current date and time in different formats. The exact output depends on your system&#8217;s local date and time settings.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-19.png\"><img decoding=\"async\" width=\"725\" height=\"150\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-19.png\" alt=\"Python strftime() example showing formatted local date, local time, and 12-hour and 24-hour time formats.\" class=\"wp-image-12737\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-19.png 725w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/image-19-300x62.png 300w\" sizes=\"(max-width: 725px) 100vw, 725px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python&#8217;s <code>datetime<\/code> module provides useful functionality for working with dates and times. Using methods such as <code>datetime.now()<\/code> and <code>strftime()<\/code>, developers can retrieve current date and time values and display them in different formats.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we covered importing the required datetime classes, retrieving the current date and time, identifying the weekday, and formatting date and time values using <code>strftime()<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These functions are useful when developing applications that require date and time handling, timestamps, scheduling, and formatted date displays.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions (FAQ)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. What is the datetime module in Python?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>datetime<\/code> module is a built-in Python module used to work with dates and times. It provides classes such as <code>date<\/code>, <code>time<\/code>, and <code>datetime<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. What is the purpose of strftime() in Python?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>strftime()<\/code> method converts date and time objects into formatted strings using format codes such as <code>%d<\/code>, <code>%m<\/code>, <code>%Y<\/code>, <code>%H<\/code>, and <code>%M<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. What is the difference between strftime() and strptime()?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>strftime()<\/code> converts a date or time object into a formatted string, whereas <code>strptime()<\/code> converts a string into a date or time object based on a specified format.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Links<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-install-python-from-source-code\/\">How to Install Python from Source Code<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Official Links<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.python.org\/3\/library\/datetime.html\" target=\"_blank\" rel=\"noopener\">Python Documentation \u2013 datetime Module<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.python.org\/3\/library\/datetime.html#strftime-and-strptime-behavior\" target=\"_blank\" rel=\"noopener\">Python Documentation \u2013 strftime() and strptime() Behavior<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to Our Experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking for help with Python development, application development, or automation solutions? Our team can help you explore the right technologies for your business requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Contact our experts: <a href=\"https:\/\/pheonixsolutions.com\/contact\">Click here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post Date: May 30, 2019Last Updated: November 02, 2026 Introduction Python provides several built-in functions and modules to work with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12738,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[671,672],"class_list":["post-4804","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-architecture","tag-date-time","tag-strftime","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1fu","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/Python-DateTime-and-strftime-Guide.png","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=4804"}],"version-history":[{"count":2,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4804\/revisions"}],"predecessor-version":[{"id":12739,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4804\/revisions\/12739"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media\/12738"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=4804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=4804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=4804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}