{"id":979391,"date":"2024-12-27T06:46:03","date_gmt":"2024-12-26T22:46:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/979391.html"},"modified":"2024-12-27T06:46:06","modified_gmt":"2024-12-26T22:46:06","slug":"python%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e6%95%b4%e5%b0%8f%e6%97%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/979391.html","title":{"rendered":"python\u5982\u4f55\u83b7\u53d6\u6574\u5c0f\u65f6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205238\/431dedae-e275-44a9-84b6-f0a1155f5806.webp\" alt=\"python\u5982\u4f55\u83b7\u53d6\u6574\u5c0f\u65f6\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u83b7\u53d6\u6574\u5c0f\u65f6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528datetime\u6a21\u5757\u3001\u5c06\u5206\u949f\u548c\u79d2\u6570\u8bbe\u7f6e\u4e3a\u96f6\u3001\u4f7f\u7528strftime\u683c\u5f0f\u5316\u65f6\u95f4\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528datetime\u6a21\u5757\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u65f6\u95f4\u64cd\u4f5c\u529f\u80fd\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528datetime\u6a21\u5757\u6765\u83b7\u53d6\u6574\u5c0f\u65f6\u3002<\/p>\n<\/p>\n<p><p>\u8981\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u6574\u5c0f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684datetime\u6a21\u5757\u3002\u9996\u5148\uff0c\u5bfc\u5165datetime\u6a21\u5757\uff0c\u7136\u540e\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u5c06\u5206\u949f\u548c\u79d2\u6570\u8bbe\u7f6e\u4e3a\u96f6\uff0c\u6765\u83b7\u53d6\u6574\u5c0f\u65f6\u3002\u6700\u540e\uff0c\u901a\u8fc7strftime\u65b9\u6cd5\u5c06\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u8f93\u51fa\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u6b65\u9aa4\u548c\u4ee3\u7801\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>\u83b7\u53d6\u6574\u5c0f\u65f6<\/strong><\/h2>\n<p>whole_hour = now.replace(minute=0, second=0, microsecond=0)<\/p>\n<h2><strong>\u8f93\u51fa\u6574\u5c0f\u65f6<\/strong><\/h2>\n<p>print(&quot;\u6574\u5c0f\u65f6\u4e3a:&quot;, whole_hour.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86datetime\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>datetime.now()<\/code>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>replace()<\/code>\u65b9\u6cd5\u5c06\u5206\u949f\u3001\u79d2\u548c\u5fae\u79d2\u8bbe\u7f6e\u4e3a\u96f6\uff0c\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u6574\u5c0f\u65f6\u90e8\u5206\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>strftime()<\/code>\u683c\u5f0f\u5316\u65f6\u95f4\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u6253\u5370\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528DATETIME\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u4f7f\u7528datetime\u6a21\u5757\u662f\u83b7\u53d6\u6574\u5c0f\u65f6\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002datetime\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u65f6\u95f4\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u83b7\u53d6\u548c\u683c\u5f0f\u5316\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528datetime\u6a21\u5757\u83b7\u53d6\u6574\u5c0f\u65f6\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong>\uff1a\u4f7f\u7528<code>datetime.now()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u8fd9\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u65e5\u671f\u548c\u65f6\u95f4\u7684datetime\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u6574\u65f6\u95f4\u5230\u6574\u5c0f\u65f6<\/strong>\uff1a\u4f7f\u7528<code>replace()<\/code>\u65b9\u6cd5\uff0c\u5c06\u5206\u949f\u3001\u79d2\u548c\u5fae\u79d2\u8bbe\u7f6e\u4e3a\u96f6\u3002\u8fd9\u53ef\u4ee5\u786e\u4fdd\u8fd4\u56de\u7684\u65f6\u95f4\u662f\u6574\u5c0f\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u683c\u5f0f\u5316\u8f93\u51fa<\/strong>\uff1a\u4f7f\u7528<code>strftime()<\/code>\u65b9\u6cd5\u5c06\u65f6\u95f4\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\uff0c\u4fbf\u4e8e\u8f93\u51fa\u548c\u663e\u793a\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\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>\u83b7\u53d6\u6574\u5c0f\u65f6<\/strong><\/h2>\n<p>whole_hour = now.replace(minute=0, second=0, microsecond=0)<\/p>\n<h2><strong>\u8f93\u51fa\u6574\u5c0f\u65f6<\/strong><\/h2>\n<p>print(&quot;\u6574\u5c0f\u65f6\u4e3a:&quot;, whole_hour.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528<code>replace()<\/code>\u65b9\u6cd5\u5c06\u5206\u949f\u3001\u79d2\u548c\u5fae\u79d2\u8bbe\u7f6e\u4e3a\u96f6\uff0c\u4ee5\u83b7\u53d6\u6574\u5c0f\u65f6\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>strftime()<\/code>\u683c\u5f0f\u5316\u65f6\u95f4\u4e3a\u5b57\u7b26\u4e32\uff0c\u4fbf\u4e8e\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u901a\u8fc7TIMESTAMP\u83b7\u53d6\u6574\u5c0f\u65f6<\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u83b7\u53d6\u6574\u5c0f\u65f6\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u65f6\u95f4\u6233\uff08timestamp\uff09\u3002\u65f6\u95f4\u6233\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\uff0c\u8868\u793a\u4ece1970\u5e741\u67081\u65e500:00:00 UTC\u8d77\u81f3\u4eca\u7684\u79d2\u6570\u3002\u901a\u8fc7\u65f6\u95f4\u6233\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u65f6\u95f4\u8ba1\u7b97\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233<\/strong>\uff1a\u4f7f\u7528<code>time.time()<\/code>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8ba1\u7b97\u6574\u5c0f\u65f6\u7684\u65f6\u95f4\u6233<\/strong>\uff1a\u901a\u8fc7\u6574\u9664\u8fd0\u7b97\u7b26\uff08<code>\/\/<\/code>\uff09\u548c\u4e58\u6cd5\uff0c\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u6574\u5c0f\u65f6\u7684\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61<\/strong>\uff1a\u4f7f\u7528<code>datetime.fromtimestamp()<\/code>\u65b9\u6cd5\u5c06\u6574\u5c0f\u65f6\u7684\u65f6\u95f4\u6233\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = time.time()<\/p>\n<h2><strong>\u8ba1\u7b97\u6574\u5c0f\u65f6\u7684\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>whole_hour_timestamp = (timestamp \/\/ 3600) * 3600<\/p>\n<h2><strong>\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61<\/strong><\/h2>\n<p>whole_hour = datetime.fromtimestamp(whole_hour_timestamp)<\/p>\n<h2><strong>\u8f93\u51fa\u6574\u5c0f\u65f6<\/strong><\/h2>\n<p>print(&quot;\u6574\u5c0f\u65f6\u4e3a:&quot;, whole_hour.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>time.time()<\/code>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\u3002\u63a5\u7740\uff0c\u901a\u8fc7\u6574\u9664\u8fd0\u7b97\u7b26\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u6574\u5c0f\u65f6\u7684\u65f6\u95f4\u6233\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>datetime.fromtimestamp()<\/code>\u5c06\u6574\u5c0f\u65f6\u7684\u65f6\u95f4\u6233\u8f6c\u6362\u4e3adatetime\u5bf9\u8c61\uff0c\u5e76\u683c\u5f0f\u5316\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528PANDAS\u5e93\u83b7\u53d6\u6574\u5c0f\u65f6<\/p>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u65f6\u95f4\u5e8f\u5217\u64cd\u4f5c\u7684\u4fbf\u6377\u529f\u80fd\u3002\u901a\u8fc7Pandas\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u53d6\u548c\u5904\u7406\u65f6\u95f4\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Pandas\u83b7\u53d6\u6574\u5c0f\u65f6\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165Pandas\u5e93<\/strong>\uff1a\u786e\u4fdd\u5df2\u5b89\u88c5Pandas\u5e93\uff0c\u5e76\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u65f6\u95f4\u5e8f\u5217<\/strong>\uff1a\u4f7f\u7528<code>pd.to_datetime()<\/code>\u65b9\u6cd5\u5c06\u5f53\u524d\u65f6\u95f4\u8f6c\u6362\u4e3a\u65f6\u95f4\u5e8f\u5217\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5411\u4e0b\u820d\u5165\u5230\u6574\u5c0f\u65f6<\/strong>\uff1a\u4f7f\u7528<code>dt.floor()<\/code>\u65b9\u6cd5\u5411\u4e0b\u820d\u5165\u5230\u6574\u5c0f\u65f6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = pd.to_datetime(&#39;now&#39;)<\/p>\n<h2><strong>\u5411\u4e0b\u820d\u5165\u5230\u6574\u5c0f\u65f6<\/strong><\/h2>\n<p>whole_hour = now.floor(&#39;H&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u6574\u5c0f\u65f6<\/strong><\/h2>\n<p>print(&quot;\u6574\u5c0f\u65f6\u4e3a:&quot;, whole_hour)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u5e93\u4e2d\u7684<code>pd.to_datetime()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>dt.floor(&#39;H&#39;)<\/code>\u65b9\u6cd5\u5411\u4e0b\u820d\u5165\u5230\u6574\u5c0f\u65f6\uff0c\u5e76\u6253\u5370\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528NUMPY\u5e93\u83b7\u53d6\u6574\u5c0f\u65f6<\/p>\n<\/p>\n<p><p>Numpy\u662f\u4e00\u4e2a\u5e38\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u529f\u80fd\u3002\u901a\u8fc7Numpy\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u65f6\u95f4\u64cd\u4f5c\u548c\u683c\u5f0f\u5316\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Numpy\u83b7\u53d6\u6574\u5c0f\u65f6\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165Numpy\u5e93<\/strong>\uff1a\u786e\u4fdd\u5df2\u5b89\u88c5Numpy\u5e93\uff0c\u5e76\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233<\/strong>\uff1a\u4f7f\u7528<code>numpy.datetime64()<\/code>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8f6c\u6362\u4e3a\u6574\u5c0f\u65f6\u7684\u65f6\u95f4\u6233<\/strong>\uff1a\u901a\u8fc7<code>astype()<\/code>\u65b9\u6cd5\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u6574\u5c0f\u65f6\u7684\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>now = np.datetime64(&#39;now&#39;)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u6574\u5c0f\u65f6\u7684\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>whole_hour = now.astype(&#39;datetime64[h]&#39;)<\/p>\n<h2><strong>\u8f93\u51fa\u6574\u5c0f\u65f6<\/strong><\/h2>\n<p>print(&quot;\u6574\u5c0f\u65f6\u4e3a:&quot;, whole_hour)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Numpy\u5e93\u4e2d\u7684<code>np.datetime64(&#39;now&#39;)<\/code>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7684\u65f6\u95f4\u6233\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>astype(&#39;datetime64[h]&#39;)<\/code>\u65b9\u6cd5\u5c06\u65f6\u95f4\u6233\u8f6c\u6362\u4e3a\u6574\u5c0f\u65f6\u7684\u65f6\u95f4\u6233\uff0c\u5e76\u6253\u5370\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5e94\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u83b7\u53d6\u6574\u5c0f\u65f6\u7684\u64cd\u4f5c\u53ef\u80fd\u7528\u4e8e\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u805a\u5408\u548c\u5206\u6790<\/strong>\uff1a\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u6309\u5c0f\u65f6\u805a\u5408\u6570\u636e\uff0c\u4ee5\u4fbf\u4e8e\u89c2\u5bdf\u548c\u5206\u6790\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65f6\u95f4\u6233\u683c\u5f0f\u5316<\/strong>\uff1a\u5728\u65e5\u5fd7\u548c\u62a5\u544a\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u5c06\u65f6\u95f4\u6233\u683c\u5f0f\u5316\u4e3a\u6574\u5c0f\u65f6\uff0c\u4ee5\u4fbf\u4e8e\u9605\u8bfb\u548c\u7406\u89e3\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u5ea6\u548c\u8ba1\u5212<\/strong>\uff1a\u5728\u8c03\u5ea6\u548c\u8ba1\u5212\u7cfb\u7edf\u4e2d\uff0c\u901a\u5e38\u9700\u8981\u6839\u636e\u6574\u5c0f\u65f6\u8fdb\u884c\u4efb\u52a1\u5b89\u6392\u548c\u6267\u884c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u83b7\u53d6\u6574\u5c0f\u65f6\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u65f6\u533a\u95ee\u9898<\/strong>\uff1a\u5728\u5904\u7406\u65f6\u95f4\u6570\u636e\u65f6\uff0c\u9700\u8981\u8003\u8651\u65f6\u533a\u5bf9\u65f6\u95f4\u7684\u5f71\u54cd\u3002\u53ef\u4ee5\u4f7f\u7528<code>pytz<\/code>\u5e93\u6765\u5904\u7406\u65f6\u533a\u8f6c\u6362\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7cbe\u5ea6\u95ee\u9898<\/strong>\uff1a\u5728\u65f6\u95f4\u8ba1\u7b97\u4e2d\uff0c\u9700\u8981\u6ce8\u610f\u65f6\u95f4\u6233\u7684\u7cbe\u5ea6\u95ee\u9898\uff0c\u786e\u4fdd\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u95ee\u9898<\/strong>\uff1a\u5728\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u65f6\uff0c\u9700\u8981\u8003\u8651\u6027\u80fd\u95ee\u9898\uff0c\u9009\u62e9\u9ad8\u6548\u7684\u7b97\u6cd5\u548c\u5e93\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u63cf\u8ff0\u4e86\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u6574\u5c0f\u65f6\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001\u901a\u8fc7\u65f6\u95f4\u6233\u3001\u4f7f\u7528Pandas\u5e93\u548cNumpy\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u5b9e\u73b0\u9ad8\u6548\u548c\u51c6\u786e\u7684\u65f6\u95f4\u64cd\u4f5c\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u60a8\u5728Python\u7f16\u7a0b\u4e2d\u5904\u7406\u65f6\u95f4\u6570\u636e\u63d0\u4f9b\u5e2e\u52a9\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\u6574\u5c0f\u65f6\u7684\u65f6\u95f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u8f7b\u677e\u83b7\u53d6\u5f53\u524d\u7684\u6574\u5c0f\u65f6\u3002\u901a\u8fc7\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u5c06\u5206\u949f\u548c\u79d2\u8bbe\u7f6e\u4e3a0\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">from datetime import datetime, timedelta\n\ncurrent_time = datetime.now()\nrounded_hour = current_time.replace(minute=0, second=0, microsecond=0)\nprint(rounded_hour)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa\u5f53\u524d\u6574\u5c0f\u65f6\u7684\u65f6\u95f4\uff0c\u4f8b\u5982\u201c2023-10-10 14:00:00\u201d\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u65f6\u95f4\u56db\u820d\u4e94\u5165\u5230\u6700\u8fd1\u7684\u6574\u5c0f\u65f6\uff1f<\/strong><br \/>\u5982\u679c\u60f3\u8981\u5c06\u5f53\u524d\u65f6\u95f4\u56db\u820d\u4e94\u5165\u5230\u6700\u8fd1\u7684\u6574\u5c0f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>timedelta<\/code>\u6765\u8c03\u6574\u65f6\u95f4\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from datetime import datetime, timedelta\n\ncurrent_time = datetime.now()\nif current_time.minute &gt;= 30:\n    rounded_hour = current_time + timedelta(hours=1)\nrounded_hour = rounded_hour.replace(minute=0, second=0, microsecond=0)\nprint(rounded_hour)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u6839\u636e\u5f53\u524d\u5206\u949f\u6570\u5224\u65ad\u662f\u5426\u9700\u8981\u589e\u52a0\u4e00\u4e2a\u5c0f\u65f6\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u65f6\u95f4\u83b7\u53d6\u6574\u5c0f\u65f6\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u4e00\u4e2a\u65f6\u95f4\u5bf9\u8c61\u6765\u83b7\u53d6\u8be5\u65f6\u95f4\u7684\u6574\u5c0f\u65f6\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">from datetime import datetime\n\nspecific_time = datetime(2023, 10, 10, 14, 45)  # \u6307\u5b9a\u65f6\u95f4\nrounded_hour = specific_time.replace(minute=0, second=0, microsecond=0)\nif specific_time.minute &gt;= 30:\n    rounded_hour += timedelta(hours=1)\nprint(rounded_hour)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u80fd\u6839\u636e\u4efb\u610f\u6307\u5b9a\u7684\u65f6\u95f4\u8ba1\u7b97\u51fa\u6574\u5c0f\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u83b7\u53d6\u6574\u5c0f\u65f6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528datetime\u6a21\u5757\u3001\u5c06\u5206\u949f\u548c\u79d2\u6570\u8bbe\u7f6e\u4e3a\u96f6\u3001\u4f7f\u7528strftime\u683c\u5f0f\u5316\u65f6 [&hellip;]","protected":false},"author":3,"featured_media":979399,"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\/979391"}],"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=979391"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/979391\/revisions"}],"predecessor-version":[{"id":979401,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/979391\/revisions\/979401"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/979399"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=979391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=979391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=979391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}