{"id":4804,"date":"2019-05-30T13:08:48","date_gmt":"2019-05-30T07:38:48","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=4804"},"modified":"2019-05-30T17:38:51","modified_gmt":"2019-05-30T12:08:51","slug":"python-date-time-strftime","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/python-date-time-strftime\/","title":{"rendered":"PYTHON : Date Time, Strftime"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">HOW TO USE PYTHON : date time, Strftime<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Date : 30\/05\/2019<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">INTRODUCTION<br><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">     Python date time provides a number of function to deal with date and time&nbsp;.<br>     Before you run the code for datetime, it is important that you import the date time modules.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">IMPORT DATE TIME<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">import date function from date time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">     =&gt; from datetime import date<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import time function from date time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">     =&gt; from datetime import time<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import datetime from datetime.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">     =&gt; from datetime import datetime<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">PRINT DATE AND TIME USING datetime<\/h3>\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\nfrom datetime import time\nfrom datetime import datetime\ndef main():\n    #print today date\n    todaydate=datetime.now()\n    print (todaydate)\n    # Get the current time\n    t = datetime.time(datetime.now())\n    print \"The current time is\", t\n    \n    wd=date.weekday(todaydate)\n    \n    days= [\"sunday\",\"monday\",\"tuesday\",\"wednesday\",\"thursday\",\"friday\",\"saturday\"]\n    print(\"Today is day number %d\" % wd)\n    print(\"which is a \" + days[wd])\n\nif __name__== \"__main__\":\n    main()<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">OUTPUT:<\/h3>\n\n\n\n<ul class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\"><li class=\"blocks-gallery-item\"><figure><img fetchpriority=\"high\" decoding=\"async\" width=\"687\" height=\"115\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/DATE.png\" alt=\"\" data-id=\"4806\" data-link=\"https:\/\/pheonixsolutions.com\/blog\/?attachment_id=4806\" class=\"wp-image-4806\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/DATE.png 687w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/DATE-300x50.png 300w\" sizes=\"(max-width: 687px) 100vw, 687px\" \/><\/figure><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Format Date and Time  with Strftime()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">     As of now we have learned, how to use datetime and date object in Python. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">     We will advance a step further and learn how to use a formatting function to format Time and Date.<br><\/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\ndef main():\n   \n      now= datetime.now() #get the current date and time\n        \n      print(now.strftime(\"%c\")) \n      print(now.strftime(\"%x\")) \n      print(now.strftime(\"%X\")) \n\n      #%I\/%H - 12\/24 Hour, %M - minute, %S - second, %p - local's AM\/PM\n      print(now.strftime(\"%I:%M:%S %p\")) # 12-Hour:Minute:Second:AM\n      print(now.strftime(\"%H:%M\")) # 24-Hour:Minute\n\nif __name__== \"__main__\":\n    main()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">%c &#8211; local date and time<br>\n%x-local&#8217;s date<br>\n%X- local&#8217;s time<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">OUTPUT:<\/h3>\n\n\n\n<ul class=\"wp-block-gallery columns-1 is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\"><li class=\"blocks-gallery-item\"><figure><img decoding=\"async\" width=\"526\" height=\"161\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/DATE1.png\" alt=\"\" data-id=\"4807\" data-link=\"https:\/\/pheonixsolutions.com\/blog\/?attachment_id=4807\" class=\"wp-image-4807\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/DATE1.png 526w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/05\/DATE1-300x92.png 300w\" sizes=\"(max-width: 526px) 100vw, 526px\" \/><\/figure><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for using&nbsp;<a href=\"https:\/\/pheonixsolutions.com\/\">pheonix solutions<\/a>.<br>You find this tutorial helpful? Share with your friends to keep it alive.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>HOW TO USE PYTHON : date time, Strftime Date : 30\/05\/2019 INTRODUCTION Python date time provides a number of function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"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_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","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":"","_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":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4804\/revisions"}],"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}]}}