{"id":1001827,"date":"2024-12-27T10:03:38","date_gmt":"2024-12-27T02:03:38","guid":{"rendered":""},"modified":"2024-12-27T10:03:41","modified_gmt":"2024-12-27T02:03:41","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e6%97%b6%e9%97%b4%e6%88%b3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1001827.html","title":{"rendered":"python\u5982\u4f55\u8f93\u51fa\u65f6\u95f4\u6233"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075831\/8ddf2c7d-061b-4cd8-afa9-9d964fea9617.webp\" alt=\"python\u5982\u4f55\u8f93\u51fa\u65f6\u95f4\u6233\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u8f93\u51fa\u65f6\u95f4\u6233\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u3001<code>datetime<\/code>\u6a21\u5757\u3001\u4ee5\u53ca<code>calendar<\/code>\u6a21\u5757\u7b49\u65b9\u6cd5\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>time.time()<\/code>\u3001<code>datetime.datetime.timestamp()<\/code>\u3001\u4ee5\u53ca<code>calendar.timegm()<\/code>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f<\/strong>\u3002\u4f8b\u5982\uff0c<code>time.time()<\/code>\u53ef\u4ee5\u5feb\u901f\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\uff0c\u800c<code>datetime<\/code>\u6a21\u5757\u5219\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u7684\u65f6\u95f4\u65e5\u671f\u5904\u7406\u529f\u80fd\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528TIME\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684<code>time<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u4e0e\u65f6\u95f4\u76f8\u5173\u7684\u529f\u80fd\u3002\u5176\u4e2d\uff0c<code>time.time()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\u3002\u65f6\u95f4\u6233\u901a\u5e38\u662f\u4ece1970\u5e741\u67081\u65e5\uff08UTC\uff09\u5f00\u59cb\u8ba1\u7b97\u7684\u79d2\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li><code>time.time()<\/code>\u51fd\u6570<\/li>\n<\/ol>\n<p><p><code>time.time()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u8868\u793a\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\u3002\u8fd9\u4e2a\u65f6\u95f4\u6233\u662f\u4ece1970\u5e741\u67081\u65e5UTC\u7684\u79d2\u6570\u3002\u7531\u4e8e\u5b83\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u6240\u4ee5\u5b83\u4e5f\u5305\u542b\u4e86\u5fae\u79d2\u7ea7\u7684\u7cbe\u5ea6\u3002<\/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>current_timestamp = time.time()<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4\u6233\u4e3a\uff1a&quot;, current_timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u7cbe\u786e\u5230\u79d2\u7684\u65f6\u95f4\u6233<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u53ea\u9700\u8981\u7cbe\u786e\u5230\u79d2\u7684\u65f6\u95f4\u6233\uff0c\u53ef\u4ee5\u4f7f\u7528<code>int()<\/code>\u51fd\u6570\u5bf9\u8fd4\u56de\u503c\u8fdb\u884c\u53d6\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\u5e76\u7cbe\u786e\u5230\u79d2<\/strong><\/h2>\n<p>current_timestamp = int(time.time())<\/p>\n<p>print(&quot;\u7cbe\u786e\u5230\u79d2\u7684\u65f6\u95f4\u6233\u4e3a\uff1a&quot;, current_timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528DATETIME\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u6bd4<code>time<\/code>\u6a21\u5757\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u3002\u901a\u8fc7\u8fd9\u4e2a\u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u65e5\u671f\u548c\u65f6\u95f4\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<ol>\n<li><code>datetime.datetime.timestamp()<\/code>\u65b9\u6cd5<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a<code>datetime<\/code>\u5bf9\u8c61\uff0c\u4f7f\u7528<code>timestamp()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u8be5\u5bf9\u8c61\u7684\u65f6\u95f4\u6233\u3002<\/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>current_time = datetime.now()<\/p>\n<h2><strong>\u5c06\u5f53\u524d\u65f6\u95f4\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>current_timestamp = current_time.timestamp()<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4\u6233\u4e3a\uff1a&quot;, current_timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5c06\u7279\u5b9a\u65f6\u95f4\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u7279\u5b9a\u65f6\u95f4\u7684<code>datetime<\/code>\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>timestamp()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7279\u5b9a\u65f6\u95f4<\/strong><\/h2>\n<p>specific_time = datetime(2023, 10, 1, 12, 0, 0)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>specific_timestamp = specific_time.timestamp()<\/p>\n<p>print(&quot;\u7279\u5b9a\u65f6\u95f4\u7684\u65f6\u95f4\u6233\u4e3a\uff1a&quot;, specific_timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528CALENDAR\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>calendar<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u51fd\u6570\u6765\u5904\u7406\u65e5\u671f\u76f8\u5173\u7684\u4efb\u52a1\uff0c\u5176\u4e2d<code>calendar.timegm()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u65f6\u95f4\u5143\u7ec4\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>calendar.timegm()<\/code><\/li>\n<\/ol>\n<p><p><code>timegm()<\/code>\u51fd\u6570\u5c06UTC\u65f6\u95f4\u5143\u7ec4\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\uff0c\u8fd9\u5728\u5904\u7406UTC\u65f6\u95f4\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<p>import time<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524dUTC\u65f6\u95f4\u5143\u7ec4<\/strong><\/h2>\n<p>utc_time_tuple = time.gmtime()<\/p>\n<h2><strong>\u5c06UTC\u65f6\u95f4\u5143\u7ec4\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>utc_timestamp = calendar.timegm(utc_time_tuple)<\/p>\n<p>print(&quot;UTC\u65f6\u95f4\u6233\u4e3a\uff1a&quot;, utc_timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5c06\u672c\u5730\u65f6\u95f4\u8f6c\u6362\u4e3aUTC\u65f6\u95f4\u6233<\/li>\n<\/ol>\n<p><p>\u5982\u679c\u9700\u8981\u5c06\u672c\u5730\u65f6\u95f4\u8f6c\u6362\u4e3aUTC\u65f6\u95f4\u6233\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528<code>time.localtime()<\/code>\u83b7\u5f97\u672c\u5730\u65f6\u95f4\u5143\u7ec4\uff0c\u7136\u540e\u4f7f\u7528<code>calendar.timegm()<\/code>\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import calendar<\/p>\n<p>import time<\/p>\n<h2><strong>\u83b7\u53d6\u672c\u5730\u65f6\u95f4\u5143\u7ec4<\/strong><\/h2>\n<p>local_time_tuple = time.localtime()<\/p>\n<h2><strong>\u5c06\u672c\u5730\u65f6\u95f4\u5143\u7ec4\u8f6c\u6362\u4e3aUTC\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>utc_timestamp = calendar.timegm(local_time_tuple)<\/p>\n<p>print(&quot;\u672c\u5730\u65f6\u95f4\u8f6c\u6362\u4e3aUTC\u65f6\u95f4\u6233\u4e3a\uff1a&quot;, utc_timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u65f6\u95f4\u6233\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u65f6\u95f4\u6233\u5728\u7f16\u7a0b\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u8bb0\u5f55\u548c\u6bd4\u8f83\u65f6\u95f4\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u65f6\u95f4\u6233\u5e38\u7528\u4e8e\u8bb0\u5f55\u65e5\u5fd7\u7684\u751f\u6210\u65f6\u95f4\u3002\u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u548c\u5206\u6790\u7cfb\u7edf\u884c\u4e3a\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6570\u636e\u6392\u5e8f<\/strong><\/li>\n<\/ol>\n<p><p>\u65f6\u95f4\u6233\u53ef\u4ee5\u7528\u6765\u5bf9\u65f6\u95f4\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u786e\u4fdd\u6570\u636e\u6309\u7167\u65f6\u95f4\u987a\u5e8f\u6392\u5217\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8ba1\u7b97\u65f6\u95f4\u5dee<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u6bd4\u8f83\u4e24\u4e2a\u65f6\u95f4\u6233\uff0c\u53ef\u4ee5\u8f7b\u677e\u8ba1\u7b97\u51fa\u4e24\u8005\u4e4b\u95f4\u7684\u65f6\u95f4\u5dee\u3002\u8fd9\u5728\u9700\u8981\u8ddf\u8e2a\u4e8b\u4ef6\u6301\u7eed\u65f6\u95f4\u7684\u5e94\u7528\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u65f6\u95f4\u6233\u7684\u683c\u5f0f\u5316\u8f93\u51fa<\/p>\n<\/p>\n<p><p>\u867d\u7136\u65f6\u95f4\u6233\u672c\u8eab\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u4f46\u5728\u8bb8\u591a\u573a\u5408\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u65e5\u671f\u548c\u65f6\u95f4\u683c\u5f0f\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>time.strftime()<\/code><\/li>\n<\/ol>\n<p><p><code>time.strftime()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u65f6\u95f4\u5143\u7ec4\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u3002\u7ed3\u5408<code>time.localtime()<\/code>\u6216<code>time.gmtime()<\/code>\uff0c\u53ef\u4ee5\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u53ef\u8bfb\u683c\u5f0f\u3002<\/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>current_timestamp = time.time()<\/p>\n<h2><strong>\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u672c\u5730\u65f6\u95f4\u5143\u7ec4<\/strong><\/h2>\n<p>local_time_tuple = time.localtime(current_timestamp)<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong><\/h2>\n<p>formatted_time = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, local_time_tuple)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65f6\u95f4\u4e3a\uff1a&quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528<code>datetime.strftime()<\/code><\/li>\n<\/ol>\n<p><p><code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>strftime()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f5c\u7528\u4e8e<code>datetime<\/code>\u5bf9\u8c61\u3002<\/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>current_time = datetime.now()<\/p>\n<h2><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong><\/h2>\n<p>formatted_time = current_time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65f6\u95f4\u4e3a\uff1a&quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u65f6\u533a\u95ee\u9898<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u65f6\u95f4\u6233\u65f6\uff0c\u65f6\u533a\u95ee\u9898\u5e38\u5e38\u662f\u4e00\u4e2a\u6311\u6218\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5de5\u5177\u6765\u5e2e\u52a9\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>pytz<\/code>\u5e93<\/li>\n<\/ol>\n<p><p><code>pytz<\/code>\u5e93\u63d0\u4f9b\u4e86\u5bf9\u65f6\u533a\u7684\u5168\u9762\u652f\u6301\uff0c\u5141\u8bb8\u60a8\u8f7b\u677e\u5730\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u4e0d\u540c\u7684\u65f6\u533a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pytz<\/p>\n<h2><strong>\u83b7\u53d6UTC\u65f6\u95f4<\/strong><\/h2>\n<p>utc_time = datetime.now(pytz.utc)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u7279\u5b9a\u65f6\u533a\u65f6\u95f4<\/strong><\/h2>\n<p>eastern_time = utc_time.astimezone(pytz.timezone(&#39;US\/Eastern&#39;))<\/p>\n<p>print(&quot;\u7f8e\u56fd\u4e1c\u90e8\u65f6\u95f4\u4e3a\uff1a&quot;, eastern_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5c06\u672c\u5730\u65f6\u95f4\u8f6c\u6362\u4e3aUTC<\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u4e2d\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5c06\u672c\u5730\u65f6\u95f4\u8f6c\u6362\u4e3aUTC\uff0c\u4ee5\u4fbf\u8fdb\u884c\u7edf\u4e00\u7684\u65f6\u95f4\u6bd4\u8f83\u548c\u5b58\u50a8\u3002<\/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\u672c\u5730\u65f6\u95f4<\/strong><\/h2>\n<p>local_time = datetime.now()<\/p>\n<h2><strong>\u5c06\u672c\u5730\u65f6\u95f4\u8f6c\u6362\u4e3aUTC<\/strong><\/h2>\n<p>utc_time = local_time.astimezone(pytz.utc)<\/p>\n<p>print(&quot;\u672c\u5730\u65f6\u95f4\u8f6c\u6362\u4e3aUTC\u4e3a\uff1a&quot;, utc_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u548c\u8f93\u51fa\u65f6\u95f4\u6233\uff0c\u4ece\u57fa\u672c\u7684<code>time<\/code>\u6a21\u5757\u5230\u529f\u80fd\u66f4\u4e30\u5bcc\u7684<code>datetime<\/code>\u6a21\u5757\u548c<code>pytz<\/code>\u5e93\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u65f6\u95f4\u8bb0\u5f55\uff0c\u8fd8\u662f\u590d\u6742\u7684\u65f6\u533a\u8f6c\u6362\uff0cPython\u90fd\u80fd\u63d0\u4f9b\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u7684<code>time()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5f53\u524d\u7684\u65f6\u95f4\u6233\u3002\u65f6\u95f4\u6233\u8868\u793a\u4ece1970\u5e741\u67081\u65e500:00:00 UTC\u5230\u73b0\u5728\u7684\u79d2\u6570\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import time\ntimestamp = time.time()\nprint(timestamp)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u5f53\u524d\u7684\u65f6\u95f4\u6233\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u65e5\u671f\u683c\u5f0f\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u53ef\u8bfb\u7684\u65e5\u671f\u683c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import datetime\ntimestamp = time.time()\ndate_time = datetime.datetime.fromtimestamp(timestamp)\nprint(date_time.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;))\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u4f1a\u8f93\u51fa\u5f53\u524d\u65f6\u95f4\u7684\u53ef\u8bfb\u683c\u5f0f\uff0c\u4f8b\u5982\u201c2023-10-01 12:45:30\u201d\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u7279\u5b9a\u65e5\u671f\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u9700\u8981\u5c06\u4e00\u4e2a\u7279\u5b9a\u7684\u65e5\u671f\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684<code>strptime()<\/code>\u65b9\u6cd5\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import datetime\ndate_str = &#39;2023-10-01 12:00:00&#39;\ndt = datetime.datetime.strptime(date_str, &#39;%Y-%m-%d %H:%M:%S&#39;)\ntimestamp = dt.timestamp()\nprint(timestamp)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\u5bf9\u5e94\u65e5\u671f\u7684\u65f6\u95f4\u6233\uff0c\u4fbf\u4e8e\u540e\u7eed\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8f93\u51fa\u65f6\u95f4\u6233\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528time\u6a21\u5757\u3001datetime\u6a21\u5757\u3001\u4ee5\u53cacalendar\u6a21\u5757\u7b49\u65b9\u6cd5\u3002\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1001836,"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\/1001827"}],"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=1001827"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001827\/revisions"}],"predecessor-version":[{"id":1001840,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001827\/revisions\/1001840"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1001836"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1001827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1001827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1001827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}