{"id":942377,"date":"2024-12-26T22:01:50","date_gmt":"2024-12-26T14:01:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/942377.html"},"modified":"2024-12-26T22:01:53","modified_gmt":"2024-12-26T14:01:53","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/942377.html","title":{"rendered":"python\u5982\u4f55\u628a\u65f6\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080212\/1ae93eeb-1776-4ff0-96ec-b809cf286019.webp\" alt=\"python\u5982\u4f55\u628a\u65f6\u95f4\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u5904\u7406\u65f6\u95f4\u7684\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528time\u6a21\u5757\u3001datetime\u6a21\u5757\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93pytz\u548cdateutil\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3001\u683c\u5f0f\u5316\u65f6\u95f4\u3001\u5904\u7406\u65f6\u533a\u8f6c\u6362\u7b49\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>datetime\u6a21\u5757\u662f\u6700\u5e38\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7c7b\u548c\u65b9\u6cd5\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528TIME\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>time<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9\u65f6\u95f4\u6233\u7684\u8bbf\u95ee\u3002\u65f6\u95f4\u6233\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u8868\u793a\u4ece1970\u5e741\u67081\u65e500:00:00\uff08epoch\uff09\u5230\u73b0\u5728\u7684\u79d2\u6570\u3002<code>time<\/code>\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u5904\u7406Unix\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<ol>\n<li>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>current_timestamp = time.time()<\/p>\n<p>print(f&quot;Current timestamp: {current_timestamp}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u65f6\u95f4\u5143\u7ec4\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">time_tuple = time.localtime(current_timestamp)<\/p>\n<p>print(f&quot;Time tuple: {time_tuple}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u65f6\u95f4\u5143\u7ec4\u8f6c\u6362\u4e3a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">time_string = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, time_tuple)<\/p>\n<p>print(f&quot;Formatted time: {time_string}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>time\u6a21\u5757\u7684\u4f18\u70b9\u662f\u7b80\u5355\u76f4\u63a5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\u6216\u7b80\u5355\u65f6\u95f4\u64cd\u4f5c\u7684\u573a\u666f<\/strong>\u3002\u4e0d\u8fc7\uff0c<code>time<\/code>\u6a21\u5757\u5bf9\u65f6\u533a\u652f\u6301\u6709\u9650\uff0c\u65e0\u6cd5\u5904\u7406\u590d\u6742\u7684\u65e5\u671f\u548c\u65f6\u95f4\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528DATETIME\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u65e5\u671f\u65f6\u95f4\u64cd\u4f5c\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u8fd8\u652f\u6301\u65e5\u671f\u65f6\u95f4\u7684\u7b97\u672f\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<ol>\n<li>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>now = datetime.now()<\/p>\n<p>print(f&quot;Current date and time: {now}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u683c\u5f0f\u5316\u65e5\u671f\u65f6\u95f4\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">formatted_now = now.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(f&quot;Formatted date and time: {formatted_now}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u65e5\u671f\u65f6\u95f4\u89e3\u6790\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">date_string = &quot;2023-10-01 12:34:56&quot;<\/p>\n<p>parsed_date = datetime.strptime(date_string, &quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(f&quot;Parsed date and time: {parsed_date}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li>\u65e5\u671f\u65f6\u95f4\u7b97\u672f\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from datetime import timedelta<\/p>\n<p>one_week_later = now + timedelta(weeks=1)<\/p>\n<p>print(f&quot;One week later: {one_week_later}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>datetime\u6a21\u5757\u5f3a\u5927\u4e14\u7075\u6d3b\uff0c\u662f\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u9996\u9009\u5de5\u5177<\/strong>\u3002\u5b83\u652f\u6301\u4ece\u65e5\u671f\u65f6\u95f4\u7684\u57fa\u672c\u64cd\u4f5c\u5230\u590d\u6742\u7684\u65e5\u671f\u65f6\u95f4\u7b97\u672f\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93PYTZ<\/p>\n<\/p>\n<p><p><code>pytz<\/code>\u5e93\u63d0\u4f9b\u4e86\u5bf9\u65f6\u533a\u7684\u652f\u6301\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u540c\u65f6\u533a\u4e4b\u95f4\u8f6c\u6362\u65f6\u95f4\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5pytz\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install pytz<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u8f6c\u6362\u4e3a\u4e0d\u540c\u65f6\u533a\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<p>utc_now = datetime.now(pytz.utc)<\/p>\n<p>print(f&quot;UTC now: {utc_now}&quot;)<\/p>\n<p>eastern = pytz.timezone(&quot;US\/Eastern&quot;)<\/p>\n<p>eastern_now = utc_now.astimezone(eastern)<\/p>\n<p>print(f&quot;Eastern Time now: {eastern_now}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528pytz\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u65f6\u533a\u95ee\u9898\uff0c\u786e\u4fdd\u65e5\u671f\u65f6\u95f4\u5728\u4e0d\u540c\u65f6\u533a\u4e4b\u95f4\u7684\u8f6c\u6362\u51c6\u786e\u65e0\u8bef<\/strong>\u3002\u8fd9\u5bf9\u4e8e\u56fd\u9645\u5316\u5e94\u7528\u5c24\u5176\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93DATEUTIL<\/p>\n<\/p>\n<p><p><code>dateutil<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65e5\u671f\u89e3\u6790\u529f\u80fd\u548c\u65e5\u671f\u65f6\u95f4\u8ba1\u7b97\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5dateutil\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install python-dateutil<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u65e5\u671f\u89e3\u6790\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from dateutil import parser<\/p>\n<p>date_string = &quot;October 1, 2023 12:34:56&quot;<\/p>\n<p>parsed_date = parser.parse(date_string)<\/p>\n<p>print(f&quot;Parsed date and time: {parsed_date}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u65e5\u671f\u65f6\u95f4\u8ba1\u7b97\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from dateutil.relativedelta import relativedelta<\/p>\n<p>one_month_later = now + relativedelta(months=1)<\/p>\n<p>print(f&quot;One month later: {one_month_later}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>dateutil\u5e93\u63d0\u4f9b\u4e86\u6bd4datetime\u6a21\u5757\u66f4\u7075\u6d3b\u7684\u89e3\u6790\u548c\u8ba1\u7b97\u529f\u80fd\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u76f8\u5bf9\u65f6\u95f4\uff08\u5982\u201c\u4e0b\u4e2a\u6708\u201d\uff09\u65f6\u975e\u5e38\u6709\u7528<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5904\u7406\u65f6\u95f4\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u975e\u5e38\u91cd\u8981\u3002<code>time<\/code>\u6a21\u5757\u9002\u7528\u4e8e\u7b80\u5355\u7684\u65f6\u95f4\u6233\u64cd\u4f5c\uff0c<code>datetime<\/code>\u6a21\u5757\u662f\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u4e3b\u529b\u519b\uff0c\u800c<code>pytz<\/code>\u548c<code>dateutil<\/code>\u63d0\u4f9b\u4e86\u9ad8\u7ea7\u7684\u65f6\u533a\u548c\u65e5\u671f\u89e3\u6790\u652f\u6301\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6a21\u5757\u548c\u5e93\uff0c\u53ef\u4ee5\u8ba9\u65f6\u95f4\u5904\u7406\u66f4\u52a0\u9ad8\u6548\u548c\u51c6\u786e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32\u3002\u901a\u8fc7<code>strftime<\/code>\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5c06\u65f6\u95f4\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6240\u9700\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>datetime.now().strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/code>\u53ef\u4ee5\u83b7\u5f97\u5f53\u524d\u65f6\u95f4\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\uff0c\u683c\u5f0f\u4e3a\u201c\u5e74-\u6708-\u65e5 \u65f6:\u5206:\u79d2\u201d\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4fee\u6539\u683c\u5f0f\u5b57\u7b26\u4e32\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u8f93\u51fa\u9700\u6c42\u3002<\/p>\n<p><strong>Python\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u65f6\u95f4\u5904\u7406\u5e93\uff1f<\/strong><br \/>\u9664\u4e86\u5185\u7f6e\u7684<code>datetime<\/code>\u6a21\u5757\uff0cPython\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5e38\u7528\u7684\u65f6\u95f4\u5904\u7406\u5e93\uff0c\u6bd4\u5982<code>time<\/code>\u548c<code>pytz<\/code>\u3002<code>time<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u65f6\u95f4\u76f8\u5173\u529f\u80fd\uff0c\u4f8b\u5982\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\u6216\u6682\u505c\u7a0b\u5e8f\u6267\u884c\u3002\u800c<code>pytz<\/code>\u5e93\u5219\u7528\u4e8e\u5904\u7406\u65f6\u533a\u95ee\u9898\uff0c\u80fd\u591f\u65b9\u4fbf\u5730\u8f6c\u6362\u4e0d\u540c\u5730\u533a\u7684\u65f6\u95f4\u3002\u6839\u636e\u9879\u76ee\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u65f6\u95f4\u5904\u7406\u7684\u590d\u6742\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8ba1\u7b97\u65f6\u95f4\u5dee\uff1f<\/strong><br \/>\u8981\u8ba1\u7b97\u65f6\u95f4\u5dee\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>timedelta<\/code>\u5bf9\u8c61\u3002\u901a\u8fc7\u521b\u5efa\u4e24\u4e2a<code>datetime<\/code>\u5bf9\u8c61\u5e76\u76f8\u51cf\uff0c\u60a8\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a<code>timedelta<\/code>\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u8868\u793a\u4e24\u4e2a\u65f6\u95f4\u70b9\u4e4b\u95f4\u7684\u5dee\u5f02\u3002\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u5929\u6570\u3001\u79d2\u6570\u7b49\u4fe1\u606f\u3002\u4f8b\u5982\uff0c<code>delta = end_time - start_time<\/code>\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7<code>delta.days<\/code>\u548c<code>delta.seconds<\/code>\u6765\u83b7\u53d6\u8be6\u7ec6\u7684\u65f6\u95f4\u5dee\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5904\u7406\u65f6\u95f4\u7684\u65b9\u5f0f\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528time\u6a21\u5757\u3001datetime\u6a21\u5757\u3001\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93pytz\u548cd [&hellip;]","protected":false},"author":3,"featured_media":942383,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/942377"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=942377"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/942377\/revisions"}],"predecessor-version":[{"id":942384,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/942377\/revisions\/942384"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/942383"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=942377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=942377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=942377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}