{"id":174360,"date":"2024-05-08T18:30:00","date_gmt":"2024-05-08T10:30:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/174360.html"},"modified":"2024-05-08T18:30:05","modified_gmt":"2024-05-08T10:30:05","slug":"python%e5%a6%82%e4%bd%95%e4%bf%ae%e6%94%b9-datetime-now%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/174360.html","title":{"rendered":"python\u5982\u4f55\u4fee\u6539 datetime.now\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27051929\/dfaa39d3-8c8c-45e0-bc15-b81598e9ad47.webp\" alt=\"python\u5982\u4f55\u4fee\u6539 datetime.now\u7684\u503c\" \/><\/p>\n<p><p><strong>\u5728Python\u4e2d\u4fee\u6539<code>datetime.now()<\/code>\u7684\u503c\u901a\u5e38\u5305\u62ec\u4e24\u4e2a\u6b65\u9aa4\uff1a\u521b\u5efa\u4e00\u4e2a<code>datetime<\/code>\u5bf9\u8c61\u5e76\u901a\u8fc7\u4f7f\u7528<code>replace()<\/code>\u65b9\u6cd5\u66f4\u6539\u5176\u5c5e\u6027\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u6216\u6a21\u5757\u6765\u6a21\u62df\u65f6\u95f4\u3002<\/strong> \u5bf9\u4e8e\u5fc5\u987b\u5728\u6d4b\u8bd5\u6216\u5176\u4ed6\u7279\u6b8a\u60c5\u51b5\u4e0b\u6539\u53d8\u5f53\u524d\u65f6\u95f4\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a<code>datetime<\/code>\u5bf9\u8c61\u5e76\u66f4\u6539\u5176\u5e74\u4efd\u3001\u6708\u4efd\u3001\u65e5\u671f\u3001\u5c0f\u65f6\u3001\u5206\u949f\u6216\u79d2\u6765\u8fdb\u884c\u8c03\u6574\u3002\u8fd9\u662f\u901a\u8fc7<code>replace()<\/code>\u65b9\u6cd5\u5b9e\u73b0\u7684\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u73b0\u5728\u7684\u65f6\u95f4\u6539\u4e3a2022\u5e741\u67081\u65e5\uff0c\u4f60\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a<code>datetime<\/code>\u5bf9\u8c61\u5e76\u4f7f\u7528<code>replace()<\/code>\u65b9\u6cd5\u66f4\u6362\u5e74\u4efd\u548c\u6708\u4efd\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u521b\u5efa DATETIME \u5bf9\u8c61<\/p>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u64cd\u4f5c\u65f6\u95f4\uff0c\u4f60\u9700\u8981\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u4f60\u5e94\u8be5\u5bfc\u5165\u8fd9\u4e2a\u6a21\u5757\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u5f53\u524d\u65f6\u95f4\u7684<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>now = datetime.now()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>now<\/code>\u53d8\u91cf\u5c31\u662f\u4e00\u4e2a\u5305\u542b\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\u7684<code>datetime<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528 REPLACE() \u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f60\u53ef\u4ee5\u8c03\u7528<code>replace()<\/code>\u65b9\u6cd5\u6765\u4fee\u6539<code>now<\/code>\u5bf9\u8c61\u4e2d\u4efb\u4f55\u4f60\u60f3\u8981\u6539\u53d8\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fee\u6539now\u5bf9\u8c61\u7684\u5e74\u4efd\u3001\u6708\u4efd\u548c\u65e5\u671f<\/p>\n<p>now = now.replace(year=2022, month=1, day=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93 FREEZE TIME<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u573a\u666f\uff0c\u6bd4\u5982\u5728\u81ea\u52a8\u5316\u6d4b\u8bd5\u4e2d\u9700\u8981\u56fa\u5b9a\u5f53\u524d\u65f6\u95f4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>freezegun<\/code>\u5e93\u6765&quot;\u51bb\u7ed3&quot;\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from freezegun import freeze_time<\/p>\n<h2><strong>\u5047\u8bbe\u5f53\u524d\u65f6\u95f4\u662f2022\u5e741\u67081\u65e5<\/strong><\/h2>\n<p>@freeze_time(&quot;2022-01-01&quot;)<\/p>\n<p>def test():<\/p>\n<p>    # \u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u5f53\u524d\u65f6\u95f4\u4f1a\u662f2022\u5e741\u67081\u65e5<\/p>\n<p>    now = datetime.now()<\/p>\n<p>    print(now)<\/p>\n<p>test()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f60\u4e0d\u80fd\u771f\u6b63\u5730\u4fee\u6539<code>datetime.now()<\/code>\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u603b\u662f\u8fd4\u56de\u8c03\u7528\u5b83\u90a3\u4e00\u523b\u7684\u65f6\u95f4\u3002\u4f46\u4f60\u53ef\u4ee5\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u6765<strong>\u521b\u5efa\u65b0\u7684\u65f6\u95f4\u5bf9\u8c61\u3001\u66ff\u6362\u65f6\u95f4\u5c5e\u6027\uff0c\u6216\u8005\u5728\u6d4b\u8bd5\u4e2d\u51bb\u7ed3\u65f6\u95f4<\/strong>\u3002\u8fd9\u4e9b\u6280\u5de7\u5728\u7f16\u5199\u4f9d\u8d56\u4e8e\u65f6\u95f4\u7684\u7a0b\u5e8f\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5728\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u6216\u6a21\u62df\u7279\u5b9a\u65f6\u95f4\u573a\u666f\u65f6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: \u5982\u4f55\u5728Python\u4e2d\u4fee\u6539<code>datetime.now()<\/code>\u7684\u503c?<\/strong><\/p>\n<p>A: \u4fee\u6539<code>datetime.now()<\/code>\u7684\u503c\u662f\u4e0d\u53ef\u80fd\u7684\uff0c\u56e0\u4e3a<code>datetime.now()<\/code>\u8fd4\u56de\u7684\u662f\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4\u3002\u4f46\u662f\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684<code>datetime<\/code>\u5bf9\u8c61\u6765\u4ee3\u66ff<code>datetime.now()<\/code>\u3002<\/p>\n<p><strong>Q: \u5982\u4f55\u4f7f\u7528Python\u4fee\u6539\u65e5\u671f\u548c\u65f6\u95f4?<\/strong><\/p>\n<p>A: \u4f60\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u4e2d\u7684\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u4fee\u6539\u65e5\u671f\u548c\u65f6\u95f4\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>datetime.now()<\/code>\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u7136\u540e\u4f7f\u7528<code>datetime.timedelta()<\/code>\u51fd\u6570\u6765\u6dfb\u52a0\u6216\u51cf\u53bb\u4e00\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u3002<\/p>\n<p><strong>Q: \u5982\u4f55\u901a\u8fc7\u4fee\u6539\u65f6\u533a\u6765\u6539\u53d8<code>datetime.now()<\/code>\u8fd4\u56de\u7684\u503c?<\/strong><\/p>\n<p>A: \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>datetime.now()<\/code>\u8fd4\u56de\u7684\u662f\u7cfb\u7edf\u672c\u5730\u65f6\u95f4\u3002\u5982\u679c\u4f60\u60f3\u6539\u53d8\u5b83\u8fd4\u56de\u7684\u65f6\u95f4\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pytz<\/code>\u6a21\u5757\u6765\u8bbe\u7f6e\u4e00\u4e2a\u4e0d\u540c\u7684\u65f6\u533a\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5<code>pytz<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>datetime.now()<\/code>\u548c<code>pytz.timezone()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u9700\u8981\u7684\u65f6\u533a\u7684\u5f53\u524d\u65f6\u95f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4fee\u6539datetime.now()\u7684\u503c\u901a\u5e38\u5305\u62ec\u4e24\u4e2a\u6b65\u9aa4\uff1a\u521b\u5efa\u4e00\u4e2adatetime\u5bf9\u8c61\u5e76\u901a\u8fc7\u4f7f\u7528r [&hellip;]","protected":false},"author":3,"featured_media":174368,"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\/174360"}],"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=174360"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/174360\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/174368"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=174360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=174360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=174360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}