{"id":197,"date":"2023-02-22T22:16:22","date_gmt":"2023-02-22T14:16:22","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=197"},"modified":"2023-04-30T15:31:11","modified_gmt":"2023-04-30T07:31:11","slug":"python-format-date-time","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/22\/python-format-date-time\/","title":{"rendered":"Python\u683c\u5f0f\u5316\u65e5\u671f\u65f6\u95f4"},"content":{"rendered":"<p>Python\u4e2d\u65e5\u671f\u65f6\u95f4\u683c\u5f0f\u5316\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0cPython \u4e2d\u80fd\u7528\u5f88\u591a\u65b9\u5f0f\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u8f6c\u6362\u65e5\u671f\u65f6\u95f4\u683c\u5f0f\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u529f\u80fd\u3002Python \u63d0\u4f9b\u4e86\u4e00\u4e2a time \u548c calendar \u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u3002\u65f6\u95f4\u95f4\u9694\u662f\u4ee5\u79d2\u4e3a\u5355\u4f4d\u7684\u6d6e\u70b9\u5c0f\u6570\u3002\u6bcf\u4e2a\u65f6\u95f4\u6233\u90fd\u4ee5\u81ea\u4ece\u683c\u6797\u5a01\u6cbb\u65f6\u95f41970\u5e7401\u670801\u65e500\u65f600\u520600\u79d2\u8d77\u7ecf\u8fc7\u7684\u65f6\u95f4\u6765\u8868\u793a\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>\u57fa\u672c\u65b9\u6cd5<\/h2>\n<p>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\uff1atime.time()<\/p>\n<p>\u83b7\u53d6\u5143\u7ec4\u5f62\u5f0f\u7684\u65f6\u95f4\u6233\uff1atime.local(time.time())<\/p>\n<p>\u683c\u5f0f\u5316\u65e5\u671f\u7684\u51fd\u6570(\u57fa\u4e8e\u5143\u7ec4\u7684\u5f62\u5f0f\u8fdb\u884c\u683c\u5f0f\u5316)\uff1a<\/p>\n<p>\uff081\uff09time.asctime(time.local(time.time()))<br \/>\n\uff082\uff09time.strftime(format[,t])<\/p>\n<p>\u5c06\u683c\u5f0f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\uff1atime.strptime(str, fmt=&#8217;%a %b %d %H:%M:%S %Y&#8217;)<\/p>\n<p>\u5ef6\u8fdf\u6267\u884c\uff1atime.sleep([secs])\uff0c\u5355\u4f4d\u4e3a\u79d2<\/p>\n<h2>\u5f0f\u5316\u7b26\u53f7<\/h2>\n<p>Python\u4e2d\u65f6\u95f4\u65e5\u671f\u683c\u5f0f\u5316\u7b26\u53f7\uff1a<\/p>\n<p>%y &#8211; \u4e24\u4f4d\u6570\u7684\u5e74\u4efd\u8868\u793a\uff0800-99\uff09<br \/>\n%Y &#8211; \u56db\u4f4d\u6570\u7684\u5e74\u4efd\u8868\u793a\uff08000-9999\uff09<br \/>\n%m &#8211; \u6708\u4efd\uff0801-12\uff09<br \/>\n%d &#8211; \u6708\u5185\u4e2d\u7684\u4e00\u5929\uff080-31\uff09<br \/>\n%H &#8211; 24\u5c0f\u65f6\u5236\u5c0f\u65f6\u6570\uff080-23\uff09<br \/>\n%I &#8211; 12\u5c0f\u65f6\u5236\u5c0f\u65f6\u6570\uff0801-12\uff09<br \/>\n%M &#8211; \u5206\u949f\u6570\uff0800=59\uff09<br \/>\n%S &#8211; \u79d2\uff0800-59\uff09<br \/>\n%a &#8211; \u672c\u5730\u7b80\u5316\u661f\u671f\u540d\u79f0<br \/>\n%A &#8211; \u672c\u5730\u5b8c\u6574\u661f\u671f\u540d\u79f0<br \/>\n%b &#8211; \u672c\u5730\u7b80\u5316\u7684\u6708\u4efd\u540d\u79f0<br \/>\n%B &#8211; \u672c\u5730\u5b8c\u6574\u7684\u6708\u4efd\u540d\u79f0<br \/>\n%c &#8211; \u672c\u5730\u76f8\u5e94\u7684\u65e5\u671f\u8868\u793a\u548c\u65f6\u95f4\u8868\u793a<br \/>\n%j &#8211; \u5e74\u5185\u7684\u4e00\u5929\uff08001-366\uff09<br \/>\n%p &#8211; \u672c\u5730A.M.\u6216P.M.\u7684\u7b49\u4ef7\u7b26<br \/>\n%U &#8211; \u4e00\u5e74\u4e2d\u7684\u661f\u671f\u6570\uff0800-53\uff09\u661f\u671f\u5929\u4e3a\u661f\u671f\u7684\u5f00\u59cb<br \/>\n%w &#8211; \u661f\u671f\uff080-6\uff09\uff0c\u661f\u671f\u5929\u4e3a\u661f\u671f\u7684\u5f00\u59cb<br \/>\n%W &#8211; \u4e00\u5e74\u4e2d\u7684\u661f\u671f\u6570\uff0800-53\uff09\u661f\u671f\u4e00\u4e3a\u661f\u671f\u7684\u5f00\u59cb<br \/>\n%x &#8211; \u672c\u5730\u76f8\u5e94\u7684\u65e5\u671f\u8868\u793a<br \/>\n%X &#8211; \u672c\u5730\u76f8\u5e94\u7684\u65f6\u95f4\u8868\u793a<br \/>\n%Z &#8211; \u5f53\u524d\u65f6\u533a\u7684\u540d\u79f0<br \/>\n%% &#8211; %\u53f7\u672c\u8eab<\/p>\n<h2>\u6848\u4f8b\u6d4b\u8bd5<\/h2>\n<pre><code class=\"language-python\"># -*- coding:utf-8 -*-\nimport time\nimport datetime\n\n#\u5f53\u524d\u65f6\u95f4\nprint time.time()\n#\u65f6\u95f4\u6233\u5f62\u5f0f\nprint time.localtime(time.time())\n#\u7b80\u5355\u53ef\u8bfb\u5f62\u5f0f\nprint time.asctime(time.localtime(time.time()))\n# \u683c\u5f0f\u5316\u62102016-03-20 11:55:30\u5f62\u5f0f\nprint time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, time.localtime())\n# \u683c\u5f0f\u5316\u6210Sat Mar 28 22:22:22 2016\u5f62\u5f0f\nprint time.strftime(&quot;%a %b %d %H:%M:%S %Y&quot;, time.localtime())\n\n# \u5c06\u683c\u5f0f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\nt = &quot;Sat Mar 28 22:22:22 2016&quot;\nprint int(time.mktime(time.strptime(t, &quot;%a %b %d %H:%M:%S %Y&quot;)))\nt = &quot;2016-03-28 22:22:22&quot;\nprint int(time.mktime(time.strptime(t, &quot;%Y-%m-%d %H:%M:%S&quot;)))\n\n# \u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u683c\u5f0f\u5b57\u7b26\u4e32\ntimestamp = 1459174942\nprint time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, time.localtime(timestamp))\n\n# \u683c\u5f0f\u5207\u6362\nt = &quot;2016-03-28 22:22:22&quot;\nstrptime = time.strptime(t, &quot;%Y-%m-%d %H:%M:%S&quot;)\nprint time.strftime(&quot;%Y\/%m\/%d %H:%M:%S&quot;, strptime)\n\n# \u5c06\u5f53\u524d\u65f6\u95f4\u8f6c\u6362\u4e3a\u683c\u5f0f\u5b57\u7b26\u4e32\nprint time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, time.localtime(time.time()))\n\n# \u83b7\u5f973\u5929\u524d\u7684\u65f6\u95f4\nthree_days_ago = (datetime.datetime.now() - datetime.timedelta(days=3))\nprint int(time.mktime(three_days_ago.timetuple()))  # \u65f6\u95f4\u6233\nprint three_days_ago.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)  # \u65f6\u95f4\u5b57\u7b26\u4e32\n\n# \u4f7f\u7528datetime\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\ni = datetime.datetime.now()\nprint (&quot;\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4: %s&quot; % i)\nprint (&quot;ISO\u683c\u5f0f\u7684\u65e5\u671f\u548c\u65f6\u95f4: %s&quot; % i.isoformat() )\nprint (&quot;\u5f53\u524d\u7684\u5e74\u4efd: %s&quot; % i.year)\nprint (&quot;\u5f53\u524d\u7684\u6708\u4efd: %s&quot; % i.month)\nprint (&quot;\u5f53\u524d\u7684\u65e5\u671f: %s&quot; % i.day)\nprint (&quot;dd\/mm\/yyyy \u683c\u5f0f: %s\/%s\/%s&quot; % (i.day, i.month, i.year) )\nprint (&quot;\u5f53\u524d\u5c0f\u65f6: %s&quot; % i.hour)\nprint (&quot;\u5f53\u524d\u5206\u949f: %s&quot; % i.minute)\nprint (&quot;\u5f53\u524d\u79d2: %s&quot; % i.second)<\/code><\/pre>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre><code>1524831532.01\ntime.struct_time(tm_year=2018, tm_mon=4, tm_mday=27, tm_hour=20, tm_min=18, tm_sec=52, tm_wday=4, tm_yday=117, tm_isdst=0)\nFri Apr 27 20:18:52 2018\n2018-04-27 20:18:52\nFri Apr 27 20:18:52 2018\n1459174942\n1459174942\n2016-03-28 22:22:22\n2016\/03\/28 22:22:22\n2018-04-27 20:18:52\n1524572332\n2018-04-24 20:18:52\n\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4: 2018-04-27 20:18:52.017000\nISO\u683c\u5f0f\u7684\u65e5\u671f\u548c\u65f6\u95f4: 2018-04-27T20:18:52.017000\n\u5f53\u524d\u7684\u5e74\u4efd: 2018\n\u5f53\u524d\u7684\u6708\u4efd: 4\n\u5f53\u524d\u7684\u65e5\u671f: 27\ndd\/mm\/yyyy \u683c\u5f0f: 27\/4\/2018\n\u5f53\u524d\u5c0f\u65f6: 20\n\u5f53\u524d\u5206\u949f: 18\n\u5f53\u524d\u79d2: 52<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u65e5\u671f\u65f6\u95f4\u683c\u5f0f\u5316\u662f\u975e\u5e38\u5e38\u89c1\u7684\u64cd\u4f5c\uff0cPython \u4e2d\u80fd\u7528\u5f88\u591a\u65b9\u5f0f\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u8f6c\u6362\u65e5\u671f\u65f6\u95f4\u683c\u5f0f\u662f\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-197","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=197"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/197\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}