{"id":1162685,"date":"2025-01-15T14:46:25","date_gmt":"2025-01-15T06:46:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1162685.html"},"modified":"2025-01-15T14:46:27","modified_gmt":"2025-01-15T06:46:27","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e6%98%be%e7%a4%ba%e6%97%b6%e9%97%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1162685.html","title":{"rendered":"\u5982\u4f55\u5728python\u4e2d\u663e\u793a\u65f6\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25203626\/caff0343-2143-4509-9e59-158318599f25.webp\" alt=\"\u5982\u4f55\u5728python\u4e2d\u663e\u793a\u65f6\u95f4\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u663e\u793a\u65f6\u95f4\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4e3b\u8981\u4f9d\u8d56\u4e8ePython\u5185\u7f6e\u7684<code>datetime<\/code>\u6a21\u5757\u548c<code>time<\/code>\u6a21\u5757\u3002<strong>\u901a\u8fc7\u5bfc\u5165<code>datetime<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u3001\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4<\/strong>\u7b49\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u65f6\u95f4\u7684\u663e\u793a\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u5728Python\u4e2d\u663e\u793a\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165<code>datetime<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1. <code>datetime<\/code>\u6a21\u5757\u7684\u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\u3002\u901a\u8fc7\u8fd9\u4e2a\u6a21\u5757\uff0c\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u5e76\u5c06\u5176\u683c\u5f0f\u5316\u4e3a\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<p>print(&quot;\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff1a&quot;, now)<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f<\/strong><\/h2>\n<p>today = datetime.today()<\/p>\n<p>print(&quot;\u5f53\u524d\u65e5\u671f\uff1a&quot;, today)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>datetime<\/code>\u7c7b\uff0c\u7136\u540e\u4f7f\u7528<code>datetime.now()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002<code>datetime.today()<\/code>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><h4>2. \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u5177\u4f53\u90e8\u5206<\/h4>\n<\/p>\n<p><p><code>datetime<\/code>\u5bf9\u8c61\u5305\u542b\u4e86\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5404\u4e2a\u90e8\u5206\uff0c\u4f8b\u5982\u5e74\u3001\u6708\u3001\u65e5\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u7b49\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u83b7\u53d6\u8fd9\u4e9b\u5177\u4f53\u90e8\u5206\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>now = datetime.now()<\/p>\n<p>print(&quot;\u5e74\uff1a&quot;, now.year)<\/p>\n<p>print(&quot;\u6708\uff1a&quot;, now.month)<\/p>\n<p>print(&quot;\u65e5\uff1a&quot;, now.day)<\/p>\n<p>print(&quot;\u5c0f\u65f6\uff1a&quot;, now.hour)<\/p>\n<p>print(&quot;\u5206\u949f\uff1a&quot;, now.minute)<\/p>\n<p>print(&quot;\u79d2\uff1a&quot;, now.second)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>now.year<\/code>\u3001<code>now.month<\/code>\u7b49\u5c5e\u6027\u53ef\u4ee5\u5206\u522b\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u5e74\u3001\u6708\u3001\u65e5\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002<\/p>\n<\/p>\n<p><h4>3. \u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4<\/h4>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>strftime<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a\u4e0d\u540c\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u683c\u5f0f\u5316\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>now = datetime.now()<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h2>\n<p>formatted_time = now.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u540e\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff1a&quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>strftime<\/code>\u65b9\u6cd5\u4f7f\u7528\u683c\u5f0f\u5316\u4ee3\u7801\u5c06<code>datetime<\/code>\u5bf9\u8c61\u8f6c\u6362\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c<code>%Y<\/code>\u8868\u793a\u56db\u4f4d\u6570\u7684\u5e74\u4efd\uff0c<code>%m<\/code>\u8868\u793a\u4e24\u4f4d\u6570\u7684\u6708\u4efd\uff0c<code>%d<\/code>\u8868\u793a\u4e24\u4f4d\u6570\u7684\u65e5\u671f\uff0c<code>%H<\/code>\u8868\u793a\u4e24\u4f4d\u6570\u7684\u5c0f\u65f6\uff0c<code>%M<\/code>\u8868\u793a\u4e24\u4f4d\u6570\u7684\u5206\u949f\uff0c<code>%S<\/code>\u8868\u793a\u4e24\u4f4d\u6570\u7684\u79d2\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>time<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1. <code>time<\/code>\u6a21\u5757\u7684\u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p><code>time<\/code>\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u65f6\u95f4\u76f8\u5173\u64cd\u4f5c\u7684\u53e6\u4e00\u4e2a\u6807\u51c6\u5e93\u3002\u8fd9\u4e2a\u6a21\u5757\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u65f6\u95f4\u6233\u3001\u65f6\u95f4\u5ef6\u8fdf\u7b49\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = time.time()<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4\u6233\uff1a&quot;, timestamp)<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>current_time = time.localtime()<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4\uff1a&quot;, current_time)<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>formatted_time = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, current_time)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u540e\u7684\u5f53\u524d\u65f6\u95f4\uff1a&quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>time<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>time.time()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u7684\u65f6\u95f4\u6233\u3002<code>time.localtime()<\/code>\u65b9\u6cd5\u5c06\u5f53\u524d\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u672c\u5730\u65f6\u95f4\u7ed3\u6784\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>time.strftime<\/code>\u65b9\u6cd5\u5c06\u65f6\u95f4\u7ed3\u6784\u683c\u5f0f\u5316\u4e3a\u6307\u5b9a\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>2. \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u5177\u4f53\u90e8\u5206<\/h4>\n<\/p>\n<p><p>\u7c7b\u4f3c\u4e8e<code>datetime<\/code>\u6a21\u5757\uff0c<code>time<\/code>\u6a21\u5757\u4e5f\u63d0\u4f9b\u4e86\u83b7\u53d6\u65f6\u95f4\u5177\u4f53\u90e8\u5206\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>current_time = time.localtime()<\/p>\n<p>print(&quot;\u5e74\uff1a&quot;, current_time.tm_year)<\/p>\n<p>print(&quot;\u6708\uff1a&quot;, current_time.tm_mon)<\/p>\n<p>print(&quot;\u65e5\uff1a&quot;, current_time.tm_mday)<\/p>\n<p>print(&quot;\u5c0f\u65f6\uff1a&quot;, current_time.tm_hour)<\/p>\n<p>print(&quot;\u5206\u949f\uff1a&quot;, current_time.tm_min)<\/p>\n<p>print(&quot;\u79d2\uff1a&quot;, current_time.tm_sec)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>current_time.tm_year<\/code>\u3001<code>current_time.tm_mon<\/code>\u7b49\u5c5e\u6027\u53ef\u4ee5\u5206\u522b\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u5e74\u3001\u6708\u3001\u65e5\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>strftime<\/code>\u65b9\u6cd5\u8fdb\u884c\u683c\u5f0f\u5316<\/h4>\n<\/p>\n<p><p>\u65e0\u8bba\u662f<code>datetime<\/code>\u6a21\u5757\u8fd8\u662f<code>time<\/code>\u6a21\u5757\uff0c\u90fd\u63d0\u4f9b\u4e86<code>strftime<\/code>\u65b9\u6cd5\u7528\u4e8e\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u683c\u5f0f\u5316\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>now = datetime.now()<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h2>\n<p>formatted_time = now.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u540e\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff1a&quot;, formatted_time)<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u4e3a\u4e0d\u540c\u7684\u5f62\u5f0f<\/strong><\/h2>\n<p>formatted_time_2 = now.strftime(&quot;%A, %B %d, %Y %I:%M:%S %p&quot;)<\/p>\n<p>print(&quot;\u53e6\u4e00\u79cd\u683c\u5f0f\u5316\u540e\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff1a&quot;, formatted_time_2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>%A<\/code>\u8868\u793a\u661f\u671f\u51e0\u7684\u5168\u79f0\uff0c<code>%B<\/code>\u8868\u793a\u6708\u4efd\u7684\u5168\u79f0\uff0c<code>%d<\/code>\u8868\u793a\u4e24\u4f4d\u6570\u7684\u65e5\u671f\uff0c<code>%Y<\/code>\u8868\u793a\u56db\u4f4d\u6570\u7684\u5e74\u4efd\uff0c<code>%I<\/code>\u8868\u793a12\u5c0f\u65f6\u5236\u7684\u5c0f\u65f6\uff0c<code>%M<\/code>\u8868\u793a\u4e24\u4f4d\u6570\u7684\u5206\u949f\uff0c<code>%S<\/code>\u8868\u793a\u4e24\u4f4d\u6570\u7684\u79d2\uff0c<code>%p<\/code>\u8868\u793aAM\u6216PM\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528<code>strptime<\/code>\u65b9\u6cd5\u89e3\u6790\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p><code>strptime<\/code>\u65b9\u6cd5\u7528\u4e8e\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a<code>datetime<\/code>\u5bf9\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>date_string = &quot;2023-10-25 14:30:00&quot;<\/p>\n<p>date_object = datetime.strptime(date_string, &quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;\u89e3\u6790\u540e\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\uff1a&quot;, date_object)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c<code>strptime<\/code>\u65b9\u6cd5\u6839\u636e\u6307\u5b9a\u7684\u683c\u5f0f\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a<code>datetime<\/code>\u5bf9\u8c61\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u4ece\u5916\u90e8\u6765\u6e90\u83b7\u53d6\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5b57\u7b26\u4e32\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u65f6\u95f4\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1. \u8ba1\u7b97\u65f6\u95f4\u5dee<\/h4>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u8ba1\u7b97\u65f6\u95f4\u5dee\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime, timedelta<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<h2><strong>\u8ba1\u7b97\u672a\u67657\u5929\u7684\u65f6\u95f4<\/strong><\/h2>\n<p>future_time = now + timedelta(days=7)<\/p>\n<p>print(&quot;\u672a\u67657\u5929\u7684\u65f6\u95f4\uff1a&quot;, future_time)<\/p>\n<h2><strong>\u8ba1\u7b97\u8fc7\u53bb7\u5929\u7684\u65f6\u95f4<\/strong><\/h2>\n<p>past_time = now - timedelta(days=7)<\/p>\n<p>print(&quot;\u8fc7\u53bb7\u5929\u7684\u65f6\u95f4\uff1a&quot;, past_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>timedelta<\/code>\u5bf9\u8c61\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u65f6\u95f4\u7684\u52a0\u51cf\u64cd\u4f5c\u3002<code>timedelta<\/code>\u5bf9\u8c61\u53ef\u4ee5\u8868\u793a\u65f6\u95f4\u95f4\u9694\uff0c\u4f8b\u5982\u5929\u3001\u5c0f\u65f6\u3001\u5206\u949f\u548c\u79d2\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2. \u6bd4\u8f83\u65e5\u671f\u548c\u65f6\u95f4<\/h4>\n<\/p>\n<p><p><code>datetime<\/code>\u5bf9\u8c61\u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u6bd4\u8f83\u64cd\u4f5c\uff0c\u4f8b\u5982\u5224\u65ad\u4e24\u4e2a\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5148\u540e\u987a\u5e8f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u7684\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h2>\n<p>specified_time = datetime(2023, 10, 25, 14, 30, 0)<\/p>\n<h2><strong>\u6bd4\u8f83\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h2>\n<p>if now &gt; specified_time:<\/p>\n<p>    print(&quot;\u5f53\u524d\u65f6\u95f4\u5728\u6307\u5b9a\u65f6\u95f4\u4e4b\u540e&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u5f53\u524d\u65f6\u95f4\u5728\u6307\u5b9a\u65f6\u95f4\u4e4b\u524d\u6216\u76f8\u540c&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u76f4\u63a5\u6bd4\u8f83\u4e24\u4e2a<code>datetime<\/code>\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5224\u65ad\u5b83\u4eec\u7684\u5148\u540e\u987a\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6269\u5c55\u4f7f\u7528<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528<code>pytz<\/code>\u6a21\u5757\u5904\u7406\u65f6\u533a<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e0d\u540c\u65f6\u533a\u7684\u65f6\u95f4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pytz<\/code>\u6a21\u5757\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now(pytz.utc)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u6307\u5b9a\u65f6\u533a\u7684\u65f6\u95f4<\/strong><\/h2>\n<p>eastern = pytz.timezone(&#39;US\/Eastern&#39;)<\/p>\n<p>eastern_time = now.astimezone(eastern)<\/p>\n<p>print(&quot;\u4e1c\u90e8\u65f6\u533a\u7684\u65f6\u95f4\uff1a&quot;, eastern_time)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u5176\u4ed6\u65f6\u533a\u7684\u65f6\u95f4<\/strong><\/h2>\n<p>pacific = pytz.timezone(&#39;US\/Pacific&#39;)<\/p>\n<p>pacific_time = now.astimezone(pacific)<\/p>\n<p>print(&quot;\u592a\u5e73\u6d0b\u65f6\u533a\u7684\u65f6\u95f4\uff1a&quot;, pacific_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u83b7\u53d6\u4e86UTC\u65f6\u95f4\uff0c\u7136\u540e\u4f7f\u7528<code>astimezone<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u6307\u5b9a\u65f6\u533a\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528<code>arrow<\/code>\u6a21\u5757\u8fdb\u884c\u9ad8\u7ea7\u65f6\u95f4\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p><code>arrow<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u66f4\u5f3a\u5927\u548c\u6613\u7528\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import arrow<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = arrow.now()<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4\uff1a&quot;, now)<\/p>\n<h2><strong>\u83b7\u53d6UTC\u65f6\u95f4<\/strong><\/h2>\n<p>utc = arrow.utcnow()<\/p>\n<p>print(&quot;UTC\u65f6\u95f4\uff1a&quot;, utc)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u6307\u5b9a\u65f6\u533a\u7684\u65f6\u95f4<\/strong><\/h2>\n<p>eastern_time = now.to(&#39;US\/Eastern&#39;)<\/p>\n<p>print(&quot;\u4e1c\u90e8\u65f6\u533a\u7684\u65f6\u95f4\uff1a&quot;, eastern_time)<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/h2>\n<p>formatted_time = now.format(&#39;YYYY-MM-DD HH:mm:ss&#39;)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u540e\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff1a&quot;, formatted_time)<\/p>\n<h2><strong>\u89e3\u6790\u5b57\u7b26\u4e32\u4e3a\u65f6\u95f4\u5bf9\u8c61<\/strong><\/h2>\n<p>date_string = &quot;2023-10-25 14:30:00&quot;<\/p>\n<p>date_object = arrow.get(date_string, &#39;YYYY-MM-DD HH:mm:ss&#39;)<\/p>\n<p>print(&quot;\u89e3\u6790\u540e\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5bf9\u8c61\uff1a&quot;, date_object)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>arrow<\/code>\u6a21\u5757\u7684<code>now<\/code>\u3001<code>utcnow<\/code>\u3001<code>to<\/code>\u3001<code>format<\/code>\u548c<code>get<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u65e5\u671f\u548c\u65f6\u95f4\u7684\u83b7\u53d6\u3001\u8f6c\u6362\u3001\u683c\u5f0f\u5316\u548c\u89e3\u6790\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u663e\u793a\u65f6\u95f4\u7684\u591a\u79cd\u65b9\u6cd5\u548c\u6280\u5de7\u3002\u5305\u62ec\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3001\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u3001\u8ba1\u7b97\u65f6\u95f4\u5dee\u3001\u6bd4\u8f83\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u4ee5\u53ca\u4f7f\u7528<code>pytz<\/code>\u548c<code>arrow<\/code>\u6a21\u5757\u8fdb\u884c\u9ad8\u7ea7\u65f6\u95f4\u64cd\u4f5c\u3002\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u5904\u7406\u5404\u79cd\u65e5\u671f\u548c\u65f6\u95f4\u76f8\u5173\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p><strong>\u5bfc\u5165<code>datetime<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u3001\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u3001\u8ba1\u7b97\u65f6\u95f4\u5dee\u3001\u6bd4\u8f83\u65e5\u671f\u548c\u65f6\u95f4\u3001\u5904\u7406\u65f6\u533a\u548c\u9ad8\u7ea7\u65f6\u95f4\u64cd\u4f5c<\/strong>\u7b49\u90fd\u662f\u5728Python\u4e2d\u663e\u793a\u65f6\u95f4\u7684\u5173\u952e\u8981\u70b9\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u5185\u5bb9\uff0c\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u638c\u63e1Python\u4e2d\u7684\u65f6\u95f4\u5904\u7406\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u6a21\u5757\u6765\u663e\u793a\u5f53\u524d\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u6a21\u5757\u662f<code>datetime<\/code>\u548c<code>time<\/code>\u3002<code>datetime<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u800c<code>time<\/code>\u6a21\u5757\u5219\u4e3b\u8981\u7528\u4e8e\u83b7\u53d6\u65f6\u95f4\u6233\u548c\u63a7\u5236\u7a0b\u5e8f\u7684\u6267\u884c\u65f6\u95f4\u3002\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>datetime.datetime.now()<\/code>\u6765\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u6216\u8005\u4f7f\u7528<code>datetime.datetime.today()<\/code>\u6765\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u683c\u5f0f\u5316\u65f6\u95f4\u8f93\u51fa\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u663e\u793a\uff1f<\/strong><br \/>Python\u7684<code>datetime<\/code>\u6a21\u5757\u5141\u8bb8\u4f60\u81ea\u5b9a\u4e49\u65f6\u95f4\u7684\u8f93\u51fa\u683c\u5f0f\u3002\u4f7f\u7528<code>strftime()<\/code>\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u683c\u5f0f\u5316\u65f6\u95f4\u3002\u4f8b\u5982\uff0c<code>now.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/code>\u5c06\u8f93\u51fa\u5f53\u524d\u65f6\u95f4\u4e3a\u201c\u5e74-\u6708-\u65e5 \u65f6:\u5206:\u79d2\u201d\u7684\u683c\u5f0f\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u683c\u5f0f\u7b26\u53f7\uff08\u5982<code>%Y<\/code>\u8868\u793a\u5e74\u4efd\uff0c<code>%m<\/code>\u8868\u793a\u6708\u4efd\u7b49\uff09\u6765\u521b\u5efa\u4f60\u60f3\u8981\u7684\u65f6\u95f4\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u663e\u793a\u65f6\u533a\u76f8\u5173\u7684\u65f6\u95f4\uff1f<\/strong><br \/>\u8981\u5904\u7406\u65f6\u533a\u76f8\u5173\u7684\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pytz<\/code>\u5e93\u4e0e<code>datetime<\/code>\u6a21\u5757\u7ed3\u5408\u3002<code>pytz<\/code>\u5e93\u63d0\u4f9b\u4e86\u5bf9\u4e16\u754c\u5404\u5730\u65f6\u533a\u7684\u652f\u6301\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>pytz.timezone(&#39;\u65f6\u533a\u540d&#39;)<\/code>\u83b7\u53d6\u5bf9\u5e94\u7684\u65f6\u533a\u5bf9\u8c61\uff0c\u7136\u540e\u7ed3\u5408<code>datetime<\/code>\u6a21\u5757\u6765\u663e\u793a\u7279\u5b9a\u65f6\u533a\u7684\u65f6\u95f4\u3002\u4f8b\u5982\uff0c\u83b7\u53d6\u7ebd\u7ea6\u65f6\u533a\u7684\u5f53\u524d\u65f6\u95f4\u53ef\u4ee5\u8fd9\u6837\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">import datetime\nimport pytz\n\nny_tz = pytz.timezone(&#39;America\/New_York&#39;)\nny_time = datetime.datetime.now(ny_tz)\nprint(ny_time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;))\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u663e\u793a\u65f6\u95f4\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u4e3b\u8981\u4f9d\u8d56\u4e8ePython\u5185\u7f6e\u7684datetime\u6a21\u5757\u548ctime\u6a21\u5757\u3002\u901a\u8fc7\u5bfc\u5165d [&hellip;]","protected":false},"author":3,"featured_media":1162690,"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\/1162685"}],"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=1162685"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1162685\/revisions"}],"predecessor-version":[{"id":1162691,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1162685\/revisions\/1162691"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1162690"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1162685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1162685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1162685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}