{"id":1019422,"date":"2024-12-27T12:59:33","date_gmt":"2024-12-27T04:59:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1019422.html"},"modified":"2024-12-27T13:00:01","modified_gmt":"2024-12-27T05:00:01","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e6%af%ab%e7%a7%92","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1019422.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u663e\u793a\u6beb\u79d2"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25161925\/9954d0a5-c899-42be-8629-01d5e607b710.webp\" alt=\"Python\u4e2d\u5982\u4f55\u663e\u793a\u6beb\u79d2\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u663e\u793a\u6beb\u79d2\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001strftime\u51fd\u6570\u3001\u4ee5\u53caformatted string<\/strong>\u7b49\u591a\u79cd\u65b9\u6cd5\u3002<strong>datetime\u6a21\u5757<\/strong>\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u6570\u636e\uff0c<strong>time\u6a21\u5757<\/strong>\u53ef\u4ee5\u83b7\u5f97\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\uff0c<strong>strftime\u51fd\u6570<\/strong>\u53ef\u4ee5\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u800c<strong>formatted string<\/strong>\u8ba9\u683c\u5f0f\u5316\u8f93\u51fa\u53d8\u5f97\u66f4\u52a0\u7b80\u4fbf\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<strong>\u4f7f\u7528datetime\u6a21\u5757\u7684strftime\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f<\/strong>\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7075\u6d3b\u7684\u683c\u5f0f\u5316\u63a5\u53e3\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a\u5305\u542b\u6beb\u79d2\u7684\u5b57\u7b26\u4e32\u5f62\u5f0f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 DATETIME \u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>datetime<\/code>\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6807\u51c6\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u4e2a\u7c7b\u548c\u65b9\u6cd5\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/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;\u5f53\u524d\u65f6\u95f4\uff1a&quot;, now)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u8fd4\u56de\u5f53\u524d\u7684\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u5305\u62ec\u6beb\u79d2\u3002\u4f8b\u5982\uff1a<code>2023-10-08 15:45:30.123456<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2. \u683c\u5f0f\u5316\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u5c06\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a\u66f4\u5177\u53ef\u8bfb\u6027\u7684\u5f62\u5f0f\uff0c\u5305\u62ec\u6beb\u79d2\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strftime<\/code>\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_time = now.strftime(&quot;%Y-%m-%d %H:%M:%S.%f&quot;)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65f6\u95f4\uff1a&quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728<code>strftime<\/code>\u4e2d\uff0c<code>%f<\/code>\u7528\u4e8e\u8868\u793a\u5fae\u79d2\uff0c\u4e3a\u4e86\u663e\u793a\u6beb\u79d2\uff0c\u6211\u4eec\u53ef\u4ee5\u622a\u53d6\u524d\u4e09\u4f4d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">formatted_time_millis = now.strftime(&quot;%Y-%m-%d %H:%M:%S.%f&quot;)[:-3]<\/p>\n<p>print(&quot;\u5305\u542b\u6beb\u79d2\u7684\u683c\u5f0f\u5316\u65f6\u95f4\uff1a&quot;, formatted_time_millis)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 TIME \u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>time<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7528\u4e8e\u5904\u7406\u65f6\u95f4\u6233\u7684\u51fd\u6570\u3002\u65f6\u95f4\u6233\u662f\u81ea1970\u5e741\u67081\u65e5\u4ee5\u6765\u7684\u79d2\u6570\u3002<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>time()<\/code>\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u7684\u65f6\u95f4\u6233\uff0c\u7136\u540e\u683c\u5f0f\u5316\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>current_time = time.time()<\/p>\n<p>print(&quot;\u5f53\u524d\u65f6\u95f4\u6233\uff1a&quot;, current_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u683c\u5f0f\u5316\u65f6\u95f4\u6233<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u5305\u542b\u6beb\u79d2\u7684\u65f6\u95f4\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">local_time = time.localtime(current_time)<\/p>\n<p>formatted_time = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, local_time)<\/p>\n<p>milliseconds = int((current_time - int(current_time)) * 1000)<\/p>\n<p>print(&quot;\u683c\u5f0f\u5316\u65f6\u95f4\uff1a{}.{:03d}&quot;.format(formatted_time, milliseconds))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u672c\u5730\u65f6\u95f4\uff0c\u7136\u540e\u4f7f\u7528<code>strftime<\/code>\u683c\u5f0f\u5316\u65f6\u95f4\uff0c\u6700\u540e\u624b\u52a8\u6dfb\u52a0\u6beb\u79d2\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 STRFTIME \u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>strftime<\/code>\u51fd\u6570\u662f\u683c\u5f0f\u5316\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u5141\u8bb8\u5c06\u65e5\u671f\u548c\u65f6\u95f4\u4ee5\u591a\u79cd\u683c\u5f0f\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528<code>strftime<\/code>\u65b9\u6cd5\uff0c\u6307\u5b9a\u683c\u5f0f\u6765\u8f93\u51fa\u65f6\u95f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>now = datetime.now()<\/p>\n<p>formatted_time = now.strftime(&quot;%Y-%m-%d %H:%M:%S.%f&quot;)[:-3]<\/p>\n<p>print(&quot;\u57fa\u672c\u683c\u5f0f\u5316\u65f6\u95f4\uff1a&quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u81ea\u5b9a\u4e49\u683c\u5f0f<\/h4>\n<\/p>\n<p><p><code>strftime<\/code>\u652f\u6301\u591a\u79cd\u683c\u5f0f\u5316\u9009\u9879\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">custom_format = now.strftime(&quot;Year: %Y, Month: %m, Day: %d, Time: %H:%M:%S.%f&quot;)[:-3]<\/p>\n<p>print(&quot;\u81ea\u5b9a\u4e49\u683c\u5f0f\u5316\u65f6\u95f4\uff1a&quot;, custom_format)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u5728\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4e2d\u81ea\u7531\u52a0\u5165\u6587\u5b57\u548c\u7b26\u53f7\uff0c\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u8f93\u51fa\u8981\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528 FORMATTED STRING<\/h3>\n<\/p>\n<p><p>\u5728Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c<code>f-string<\/code>\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u4fbf\u7684\u65b9\u6cd5\u6765\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>f-string<\/code>\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u7b26\u4e32\u4e2d\u5d4c\u5165\u8868\u8fbe\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>now = datetime.now()<\/p>\n<p>formatted_time = f&quot;{now:%Y-%m-%d %H:%M:%S.%f}&quot;[:-3]<\/p>\n<p>print(&quot;f-string\u683c\u5f0f\u5316\u65f6\u95f4\uff1a&quot;, formatted_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7075\u6d3b\u5e94\u7528<\/h4>\n<\/p>\n<p><p><code>f-string<\/code>\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u683c\u5f0f\u5316\u65f6\u95f4\uff0c\u8fd8\u53ef\u4ee5\u7075\u6d3b\u7ec4\u5408\u5176\u4ed6\u53d8\u91cf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = &quot;Alice&quot;<\/p>\n<p>formatted_string = f&quot;Hello {name}, the current time is {now:%Y-%m-%d %H:%M:%S.%f}&quot;[:-3]<\/p>\n<p>print(&quot;\u7075\u6d3b\u7684f-string\uff1a&quot;, formatted_string)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u8ba9\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u64cd\u4f5c\u53d8\u5f97\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u9605\u8bfb\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4e0d\u540c\u65b9\u6cd5\u7684\u6bd4\u8f83<\/h3>\n<\/p>\n<p><h4>1. \u7b80\u5355\u6027<\/h4>\n<\/p>\n<ul>\n<li><strong>datetime\u6a21\u5757<\/strong>\uff1a\u63d0\u4f9b\u4e86\u76f4\u63a5\u83b7\u53d6\u548c\u683c\u5f0f\u5316\u65f6\u95f4\u7684\u529f\u80fd\uff0c\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002<\/li>\n<li><strong>time\u6a21\u5757<\/strong>\uff1a\u9700\u8981\u624b\u52a8\u5904\u7406\u6beb\u79d2\uff0c\u7a0d\u5fae\u590d\u6742\u4e00\u4e9b\u3002<\/li>\n<li><strong>strftime\u51fd\u6570<\/strong>\uff1a\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u683c\u5f0f\u5316\u9009\u9879\uff0c\u975e\u5e38\u5f3a\u5927\u3002<\/li>\n<li><strong>formatted string<\/strong>\uff1a\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u8bfb\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u9002\u5408Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/li>\n<\/ul>\n<p><h4>2. \u7075\u6d3b\u6027<\/h4>\n<\/p>\n<ul>\n<li><strong>datetime\u6a21\u5757<\/strong>\u548c<strong>strftime\u51fd\u6570<\/strong>\uff1a\u63d0\u4f9b\u4e86\u9ad8\u5ea6\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u65f6\u95f4\u683c\u5f0f\u3002<\/li>\n<li><strong>time\u6a21\u5757<\/strong>\uff1a\u7075\u6d3b\u6027\u8f83\u4f4e\uff0c\u9700\u8981\u7ed3\u5408\u5176\u4ed6\u51fd\u6570\u4f7f\u7528\u3002<\/li>\n<li><strong>formatted string<\/strong>\uff1a\u7ed3\u5408\u4e86\u7075\u6d3b\u6027\u548c\u6613\u8bfb\u6027\uff0c\u662f\u73b0\u4ee3Python\u7f16\u7a0b\u7684\u63a8\u8350\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<p><h4>3. \u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u5728\u6027\u80fd\u65b9\u9762\uff0c<strong>datetime\u6a21\u5757<\/strong>\u548c<strong>time\u6a21\u5757<\/strong>\u7684\u57fa\u672c\u529f\u80fd\u5df2\u7ecf\u8db3\u591f\u9ad8\u6548\u3002\u4f7f\u7528<code>f-string<\/code>\u8fdb\u884c\u683c\u5f0f\u5316\u4e5f\u4e0d\u4f1a\u5bf9\u6027\u80fd\u4ea7\u751f\u660e\u663e\u5f71\u54cd\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\u6765\u8bf4\uff0c\u6027\u80fd\u5dee\u5f02\u53ef\u4ee5\u5ffd\u7565\u4e0d\u8ba1\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1. \u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p>\u5728\u65e5\u5fd7\u8bb0\u5f55\u4e2d\uff0c\u51c6\u786e\u7684\u65f6\u95f4\u6233\u975e\u5e38\u91cd\u8981\u3002\u5305\u542b\u6beb\u79d2\u7684\u65f6\u95f4\u683c\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u7cbe\u786e\u5b9a\u4f4d\u4e8b\u4ef6\u53d1\u751f\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>from datetime import datetime<\/p>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<p>def log_event(event):<\/p>\n<p>    now = datetime.now()<\/p>\n<p>    timestamp = now.strftime(&quot;%Y-%m-%d %H:%M:%S.%f&quot;)[:-3]<\/p>\n<p>    logging.info(f&quot;Event &#39;{event}&#39; occurred at {timestamp}&quot;)<\/p>\n<p>log_event(&quot;User login&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u65f6\u95f4\u6233\u5e38\u7528\u4e8e\u5206\u6790\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u6beb\u79d2\u7ea7\u7684\u65f6\u95f4\u6233\u53ef\u4ee5\u7528\u4e8e\u9ad8\u9891\u6570\u636e\u7684\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from datetime import datetime<\/p>\n<p>data = {<\/p>\n<p>    &#39;time&#39;: [datetime.now().strftime(&quot;%Y-%m-%d %H:%M:%S.%f&quot;)[:-3] for _ in range(5)],<\/p>\n<p>    &#39;value&#39;: [10, 20, 30, 40, 50]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5b9e\u65f6\u7cfb\u7edf<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u65f6\u7cfb\u7edf\u4e2d\uff0c\u4f8b\u5982\u7269\u8054\u7f51\u548c\u91d1\u878d\u7cfb\u7edf\uff0c\u65f6\u95f4\u6233\u7684\u7cbe\u786e\u6027\u76f4\u63a5\u5f71\u54cd\u7cfb\u7edf\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def monitor_system():<\/p>\n<p>    while True:<\/p>\n<p>        current_time = time.time()<\/p>\n<p>        formatted_time = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, time.localtime(current_time))<\/p>\n<p>        milliseconds = int((current_time - int(current_time)) * 1000)<\/p>\n<p>        print(f&quot;System check at {formatted_time}.{milliseconds:03d}&quot;)<\/p>\n<p>        time.sleep(1)<\/p>\n<p>monitor_system()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u663e\u793a\u6beb\u79d2\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\uff0c<strong>datetime\u6a21\u5757\u548cstrftime\u51fd\u6570<\/strong>\u8db3\u4ee5\u6ee1\u8db3\u9700\u6c42\u3002<strong>formatted string<\/strong>\u63d0\u4f9b\u4e86\u73b0\u4ee3\u5316\u7684\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u975e\u5e38\u9002\u5408Python 3.6\u53ca\u4ee5\u4e0a\u7248\u672c\u7684\u7a0b\u5e8f\u5f00\u53d1\u3002\u5728\u65e5\u5fd7\u8bb0\u5f55\u3001\u6570\u636e\u5206\u6790\u3001\u5b9e\u65f6\u7cfb\u7edf\u7b49\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u51c6\u786e\u7684\u65f6\u95f4\u6233\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u901a\u8fc7\u5408\u7406\u7684\u9009\u62e9\u548c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u4e2d\u65f6\u95f4\u6233\u7684\u7cbe\u786e\u6027\u548c\u53ef\u8bfb\u6027\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\u7684\u6beb\u79d2\u90e8\u5206\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u63d0\u53d6\u6beb\u79d2\u90e8\u5206\u3002\u9996\u5148\uff0c\u5bfc\u5165<code>datetime<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>datetime.now()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3002\u53ef\u4ee5\u901a\u8fc7<code>microsecond<\/code>\u5c5e\u6027\u83b7\u53d6\u5fae\u79d2\u503c\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6beb\u79d2\uff0c\u53ea\u9700\u5c06\u5176\u9664\u4ee51000\u5373\u53ef\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32\u4ee5\u663e\u793a\u6beb\u79d2\uff1f<\/strong><br \/>\u4e3a\u4e86\u5728\u65f6\u95f4\u5b57\u7b26\u4e32\u4e2d\u663e\u793a\u6beb\u79d2\uff0c\u53ef\u4ee5\u4f7f\u7528<code>strftime<\/code>\u65b9\u6cd5\u7ed3\u5408<code>microsecond<\/code>\u5c5e\u6027\u3002\u901a\u8fc7\u5c06\u5f53\u524d\u65f6\u95f4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u5728\u683c\u5f0f\u4e2d\u6dfb\u52a0<code>%f<\/code>\uff0c\u53ef\u4ee5\u83b7\u5f97\u5fae\u79d2\u8868\u793a\u3002\u5728\u8f93\u51fa\u65f6\uff0c\u5c06\u5176\u683c\u5f0f\u5316\u4e3a\u4e09\u4f4d\u6570\u5b57\uff0c\u8fbe\u5230\u663e\u793a\u6beb\u79d2\u7684\u6548\u679c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u7cbe\u786e\u6d4b\u91cf\u4ee3\u7801\u6267\u884c\u65f6\u95f4\u5230\u6beb\u79d2\uff1f<\/strong><br \/>\u4f7f\u7528<code>time<\/code>\u6a21\u5757\u4e2d\u7684<code>time()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7cbe\u786e\u6d4b\u91cf\u4ee3\u7801\u6267\u884c\u7684\u65f6\u95f4\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u6267\u884c\u524d\u540e\u8c03\u7528<code>time()<\/code>\uff0c\u53ef\u4ee5\u5f97\u5230\u4e24\u4e2a\u65f6\u95f4\u6233\uff0c\u7136\u540e\u76f8\u51cf\u5e76\u4e58\u4ee51000\uff0c\u4fbf\u53ef\u5f97\u5230\u6beb\u79d2\u7ea7\u7684\u65f6\u95f4\u5dee\u3002\u5bf9\u4e8e\u66f4\u9ad8\u7cbe\u5ea6\u7684\u6d4b\u91cf\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>time.perf_counter()<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u663e\u793a\u6beb\u79d2\uff0c\u53ef\u4ee5\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001strftime\u51fd\u6570\u3001\u4ee5\u53caformatt [&hellip;]","protected":false},"author":3,"featured_media":1019449,"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\/1019422"}],"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=1019422"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019422\/revisions"}],"predecessor-version":[{"id":1019451,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019422\/revisions\/1019451"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1019449"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1019422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1019422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1019422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}