{"id":208,"date":"2023-02-22T22:24:53","date_gmt":"2023-02-22T14:24:53","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=208"},"modified":"2023-04-30T15:28:43","modified_gmt":"2023-04-30T07:28:43","slug":"python-time-addition-and-subtraction","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/22\/python-time-addition-and-subtraction\/","title":{"rendered":"Python\u65f6\u95f4\u52a0\u51cf"},"content":{"rendered":"<h2>datetime\u4f7f\u7528<\/h2>\n<pre><code class=\"language-python\">import datetime\n\n>&gt;&gt; print datetime.datetime.now()\n2017-02-15 15:01:24.619000<\/code><\/pre>\n<p><!-- more --><\/p>\n<h2>\u65f6\u95f4\u683c\u5f0f\u5316<\/h2>\n<pre><code class=\"language-python\">>&gt;&gt; print datetime.datetime.now().strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)\n2017-02-15 15:01:35\n\n>&gt;&gt; print datetime.datetime.now().strftime(&quot;%Y-%m-%d %H:%M&quot;)\n2017-02-15 15:01\n\n>&gt;&gt; print datetime.datetime.now().strftime(&quot;%Y%m%d&quot;)\n20170215<\/code><\/pre>\n<h2>\u65f6\u95f4\u52a0\u51cf<\/h2>\n<p>\u591a\u52a0\u4e00\u5929<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; print (datetime.datetime.now()+datetime.timedelta(days=1)).strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)\n2017-02-16 15:12:22<\/code><\/pre>\n<p>\u51cf\u4e00\u5929<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; print (datetime.datetime.now()+datetime.timedelta(days=-1)).strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)\n2017-02-15 15:12:25<\/code><\/pre>\n<p>\u591a\u52a0\u4e00\u5c0f\u65f6<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; print (datetime.datetime.now()+datetime.timedelta(hours=1)).strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)\n2017-02-15 16:10:28<\/code><\/pre>\n<p>\u591a\u52a0\u4e00\u5206\u949f<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; print (datetime.datetime.now()+datetime.timedelta(minutes=1)).strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)\n2017-02-15 15:12:16<\/code><\/pre>\n<p>\u51cf\u53bb\u4e00\u5e74<\/p>\n<pre><code class=\"language-python\">import datetime\nfrom dateutil.relativedelta import relativedelta\nd = datetime.datetime.strptime(&#039;20170131&#039;, &#039;%Y%m%d&#039;)\nprint(d)  \/\/ 2017-01-31 00:00:00\nprint((d - relativedelta(years=1)).strftime(&#039;%Y%m%d&#039;))  \/\/ 20160131<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>datetime\u4f7f\u7528 import datetime >&gt;&gt; print datetime.dat [&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-208","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/208","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=208"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/208\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}