{"id":1101375,"date":"2025-01-08T15:49:13","date_gmt":"2025-01-08T07:49:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101375.html"},"modified":"2025-01-08T15:49:16","modified_gmt":"2025-01-08T07:49:16","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e6%97%a5%e6%9c%9f%e8%bd%ac%e6%88%90%e6%95%b0%e5%80%bc-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101375.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u65e5\u671f\u8f6c\u6210\u6570\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064206\/d579142c-5037-4321-ac21-a06da8ed3b28.webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u65e5\u671f\u8f6c\u6210\u6570\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u6570\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001pandas\u5e93\u7b49\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u3001\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u5929\u6570\u3001\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u6574\u6570\u8868\u793a\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u81ea1970\u5e741\u67081\u65e5\u4ee5\u6765\u7684\u79d2\u6570\uff0c\u65b9\u4fbf\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u548c\u6bd4\u8f83\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528datetime\u6a21\u5757<\/p>\n<\/p>\n<p><p>datetime\u6a21\u5757\u662fPython\u4e2d\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u64cd\u4f5c\u65e5\u671f\u548c\u65f6\u95f4\u3002\u53ef\u4ee5\u901a\u8fc7datetime\u6a21\u5757\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u3001\u5929\u6570\u7b49\u6570\u503c\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/h3>\n<\/p>\n<p><p>\u65f6\u95f4\u6233\u662f\u6307\u81ea1970\u5e741\u67081\u65e5\u4ee5\u6765\u7684\u79d2\u6570\u3002\u5728datetime\u6a21\u5757\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>timestamp()<\/code>\u65b9\u6cd5\u5c06\u65e5\u671f\u5bf9\u8c61\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\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>date = datetime(2023, 10, 15)<\/p>\n<h2><strong>\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = date.timestamp()<\/p>\n<p>print(&quot;\u65f6\u95f4\u6233\uff1a&quot;, timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u5929\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>toordinal()<\/code>\u65b9\u6cd5\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u81ea\u516c\u51431\u5e741\u67081\u65e5\u4ee5\u6765\u7684\u5929\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>date = datetime(2023, 10, 15)<\/p>\n<h2><strong>\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u5929\u6570<\/strong><\/h2>\n<p>days = date.toordinal()<\/p>\n<p>print(&quot;\u5929\u6570\uff1a&quot;, days)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u6574\u6570\u8868\u793a<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5c06\u65e5\u671f\u7684\u5e74\u3001\u6708\u3001\u65e5\u5206\u522b\u8f6c\u6362\u4e3a\u6574\u6570\u5e76\u8fdb\u884c\u7ec4\u5408\uff0c\u5f97\u5230\u4e00\u4e2a\u6574\u6570\u8868\u793a\u7684\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>date = datetime(2023, 10, 15)<\/p>\n<h2><strong>\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u6574\u6570\u8868\u793a<\/strong><\/h2>\n<p>date_int = date.year * 10000 + date.month * 100 + date.day<\/p>\n<p>print(&quot;\u6574\u6570\u8868\u793a\u7684\u65e5\u671f\uff1a&quot;, date_int)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528time\u6a21\u5757<\/p>\n<\/p>\n<p><p>time\u6a21\u5757\u4e5f\u662fPython\u4e2d\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u7684\u6807\u51c6\u5e93\uff0c\u63d0\u4f9b\u4e86\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>mktime()<\/code>\u65b9\u6cd5\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>from datetime import datetime<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u671f\u5bf9\u8c61<\/strong><\/h2>\n<p>date = datetime(2023, 10, 15)<\/p>\n<h2><strong>\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = time.mktime(date.timetuple())<\/p>\n<p>print(&quot;\u65f6\u95f4\u6233\uff1a&quot;, timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528pandas\u5e93<\/p>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u529f\u80fd\u3002\u53ef\u4ee5\u901a\u8fc7pandas\u5e93\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u3001\u5929\u6570\u7b49\u6570\u503c\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>pd.to_datetime()<\/code>\u65b9\u6cd5\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = &#39;2023-10-15&#39;<\/p>\n<h2><strong>\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = pd.to_datetime(date_str).timestamp()<\/p>\n<p>print(&quot;\u65f6\u95f4\u6233\uff1a&quot;, timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u5929\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>pd.to_datetime()<\/code>\u65b9\u6cd5\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5929\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = &#39;2023-10-15&#39;<\/p>\n<h2><strong>\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5929\u6570<\/strong><\/h2>\n<p>days = pd.to_datetime(date_str).toordinal()<\/p>\n<p>print(&quot;\u5929\u6570\uff1a&quot;, days)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u5e38\u7528\u7684\u65b9\u6cd5\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u4e00\u4e9b\u5e93\u6216\u65b9\u6cd5\u6765\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u6570\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u4e2d\u7684<code>datetime64<\/code>\u7c7b\u578b\u8fdb\u884c\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528numpy\u5e93<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7<code>numpy.datetime64<\/code>\u7c7b\u578b\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = &#39;2023-10-15&#39;<\/p>\n<h2><strong>\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3adatetime64\u7c7b\u578b<\/strong><\/h2>\n<p>date_np = np.datetime64(date_str)<\/p>\n<h2><strong>\u5c06datetime64\u7c7b\u578b\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = date_np.astype(&#39;datetime64[s]&#39;).astype(&#39;int&#39;)<\/p>\n<p>print(&quot;\u65f6\u95f4\u6233\uff1a&quot;, timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u6765\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u6570\u503c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>arrow<\/code>\u5e93\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import arrow<\/p>\n<h2><strong>\u521b\u5efa\u65e5\u671f\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>date_str = &#39;2023-10-15&#39;<\/p>\n<h2><strong>\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233<\/strong><\/h2>\n<p>timestamp = arrow.get(date_str).timestamp()<\/p>\n<p>print(&quot;\u65f6\u95f4\u6233\uff1a&quot;, timestamp)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u6570\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001pandas\u5e93\u7b49\u3002\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u6765\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u65f6\u95f4\u6233\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u548c\u6bd4\u8f83\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u6570\u503c\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u6570\u503c\u7c7b\u578b\u901a\u5e38\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u3002\u53ef\u4ee5\u901a\u8fc7\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\uff0c\u5f97\u5230\u4e00\u4e2a\u6d6e\u70b9\u6570\u8868\u793a\u81ea1970\u5e741\u67081\u65e5\u4ee5\u6765\u7684\u79d2\u6570\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>datetime.timestamp()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u5f97\u65e5\u671f\u7684\u6570\u503c\u8868\u793a\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u65e5\u671f\u548c\u65f6\u95f4\u7684\u5904\u7406\uff1f<\/strong><br \/>\u9664\u4e86\u5185\u7f6e\u7684<code>datetime<\/code>\u6a21\u5757\uff0c\u8fd8\u6709\u8bb8\u591a\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u5982<code>pandas<\/code>\u3001<code>numpy<\/code>\u548c<code>dateutil<\/code>\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u80fd\u591f\u7b80\u5316\u65e5\u671f\u683c\u5f0f\u8f6c\u6362\u3001\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u7b49\u4efb\u52a1\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u683c\u5f0f\u7684\u65e5\u671f\u8f6c\u6362\u4e3a\u6570\u503c\uff1f<\/strong><br \/>\u5982\u679c\u65e5\u671f\u4ee5\u5b57\u7b26\u4e32\u683c\u5f0f\u5b58\u5728\uff0c\u53ef\u4ee5\u4f7f\u7528<code>datetime.strptime()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\uff0c\u7136\u540e\u518d\u4f7f\u7528<code>timestamp()<\/code>\u65b9\u6cd5\u83b7\u53d6\u6570\u503c\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32&#39;2023-10-01&#39;\u8f6c\u6362\u4e3a\u65e5\u671f\u5bf9\u8c61\u540e\uff0c\u53ef\u4ee5\u83b7\u53d6\u5bf9\u5e94\u7684\u65f6\u95f4\u6233\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u6216\u6587\u4ef6\u8bfb\u53d6\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5c06\u65e5\u671f\u8f6c\u6362\u4e3a\u6570\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u4f7f\u7528datetime\u6a21\u5757\u3001time\u6a21\u5757\u3001pandas\u5e93\u7b49\u3002\u5e38\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1101383,"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\/1101375"}],"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=1101375"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101375\/revisions"}],"predecessor-version":[{"id":1101386,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101375\/revisions\/1101386"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1101383"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1101375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1101375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1101375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}