﻿{"id":656,"date":"2011-08-23T17:12:07","date_gmt":"2011-08-23T09:12:07","guid":{"rendered":"http:\/\/nick.workao.org\/?p=656"},"modified":"2021-11-17T11:33:53","modified_gmt":"2021-11-17T03:33:53","slug":"python-datetime-%e6%93%8d%e4%bd%9c%e7%9b%b8%e5%85%b3","status":"publish","type":"post","link":"http:\/\/nick.txtcc.com\/index.php\/python\/656","title":{"rendered":"python datetime \u64cd\u4f5c\u76f8\u5173"},"content":{"rendered":"<p>\u7f51\u4e0aCOPY\u7684\u3002\u91cc\u9762\u6709\u95ee\u9898\u8bf7\u81ea\u4fbf\u3002<\/p>\n<p>\u6211\u53ea\u662f\u7528\u4e86\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u6765\u53d6\u6628\u5929\u7684\u65e5\u671f\uff0c\u5e76\u4e14\u683c\u5f0f\u4e3ayyyy-mm-dd\nyesterday = (datetime.date.today() + datetime.timedelta(days=-1)).isoformat()<\/p>\n<p>import time\nimport datetime<\/p>\n<h1>2007-11-25 15:36:35<\/h1>\n<p>#\u4f7f\u7528datetime\u6a21\u5757\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u4e3e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p>d1 = datetime.datetime(2005, 2, 16)\nd2 = datetime.datetime(2004, 12, 31)<\/p>\n<h1>\u7ed3\u679c:47<\/h1>\n<p>print (d1 &#8211; d2).days<\/p>\n<p>#\u4e0a\u4f8b\u6f14\u793a\u4e86\u8ba1\u7b97\u4e24\u4e2a\u65e5\u671f\u76f8\u5dee\u5929\u6570\u7684\u8ba1\u7b97\u3002<\/p>\n<p>starttime = datetime.datetime.now()<\/p>\n<p>endtime = datetime.datetime.now()\nprint (endtime &#8211; starttime).seconds<\/p>\n<p>#\u4e0a\u4f8b\u6f14\u793a\u4e86\u8ba1\u7b97\u8fd0\u884c\u65f6\u95f4\u7684\u4f8b\u5b50\uff0c\u4ee5\u79d2\u8fdb\u884c\u663e\u793a\u3002<\/p>\n<p>d1 = datetime.datetime.now()\nd3 = d1 + datetime.timedelta(days =10)<\/p>\n<p>print str(d3)\nprint d3.ctime()<\/p>\n<h1>\u4e0a\u4f8b\u6f14\u793a\u4e86\u8ba1\u7b97\u5f53\u524d\u65f6\u95f4\u5411\u540e10\u5929\u7684\u65f6\u95f4\u3002<\/h1>\n<h1>\u5982\u679c\u662f\u5c0f\u65f6 days \u6362\u6210 hours<\/h1>\n<h1>\u5176\u672c\u4e0a\u5e38\u7528\u7684\u7c7b\u6709\uff1adatetime\u548ctimedelta\u4e24\u4e2a\u3002\u5b83\u4eec\u4e4b\u95f4\u53ef\u4ee5\u76f8\u4e92\u52a0\u51cf\u3002<\/h1>\n<h1>\u6bcf\u4e2a\u7c7b\u90fd\u6709\u4e00\u4e9b\u65b9\u6cd5\u548c\u5c5e\u6027\u53ef\u4ee5\u67e5\u770b\u5177\u4f53\u7684\u503c\uff0c\u5982datetime\u53ef\u4ee5\u67e5\u770b\uff1a\u5929\u6570(day)\uff0c\u5c0f\u65f6\u6570(hour)\uff0c\u661f\u671f\u51e0(weekday())\u7b49;<\/h1>\n<h1>timedelta\u53ef\u4ee5\u67e5\u770b\uff1a\u5929\u6570(days)\uff0c\u79d2\u6570(seconds) \u7b49\u3002<\/h1>\n<p>#<\/p>\n<h1>time , datetime , string \u7c7b\u578b\u4e92\u76f8\u8f6c\u6362<\/h1>\n<p>#<\/p>\n<h1>string -> time<\/h1>\n<h1>time.strptime(publishDate,&#8221;%Y-%m-%d %H:%M:%S&#8221;)<\/h1>\n<p>#<\/p>\n<h1>time -> string<\/h1>\n<h1>time.strftime(&#8220;%y-%m-%d&#8221;,t)<\/h1>\n<p>date = &#8216;2007-01-01&#8217;<\/p>\n<p>print type(date)<\/p>\n<p>date = time.strptime(date,&#8221;%Y-%m-%d&#8221;)<\/p>\n<p>print type(date)<\/p>\n<p>print date[0]<\/p>\n<p>d4 = datetime.datetime(date[0], date[1],date[2])<\/p>\n<p>print d4\nprint type(d4)<\/p>\n<p>#\u5c06\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u8f6c\u6210\u5b57\u7b26\u4e32\u5219\u8981\u7528<\/p>\n<p>date = time.strftime(&#8220;%y-%m-%d&#8221;,date)\nprint type(date)<\/p>\n<p>#\u5176\u4e2dd\u4e3a\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61<\/p>\n<p>\u5f00\u53d1\u9700\u8981\u3000\u641c\u5230\u4e86\u8fd9\u4e2a\u7a0b\u5e8f\u3000\u56e0\u8981\u6c42\u524d\u4e00\u5929\u7684\u65e5\u671f\u3000\u5c31\u8be5\u4e86\u4e0b<\/p>\n<p>\u6839\u636e\u4e0a\u9762\u7684\u539f\u7406\u3000\u5f88\u5bb9\u6613\u5c31\u53ef\u4ee5\u5199\u51fa\u53d6\u5f97\u524d\u4e00\u5929\u65e5\u671f\u7684\u7a0b\u5e8f<\/p>\n<p>d1 = datetime.datetime.now()\nd3 = d1 + datetime.timedelta(days = -1)<\/p>\n<p>print datetime.date.today() + datetime.timedelta(days=-1)\n\u6216\nprint datetime.date.today() &#8211; datetime.timedelta(days=1)<\/p>","protected":false},"excerpt":{"rendered":"<p>\u7f51\u4e0aCOPY\u7684\u3002\u91cc\u9762\u6709\u95ee\u9898\u8bf7\u81ea\u4fbf\u3002 \u6211\u53ea\u662f\u7528\u4e86\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7528\u6765\u53d6\u6628\u5929\u7684\u65e5\u671f\uff0c\u5e76\u4e14\u683c\u5f0f\u4e3ayyyy-mm-dd yesterday = (datetime.date.today() + datetime.t&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[274],"tags":[316],"class_list":["post-656","post","type-post","status-publish","format-standard","hentry","category-python","tag-datetime"],"_links":{"self":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/656","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=656"}],"version-history":[{"count":2,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/656\/revisions"}],"predecessor-version":[{"id":2226,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/656\/revisions\/2226"}],"wp:attachment":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=656"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}