{"id":71778,"date":"2026-02-04T11:40:56","date_gmt":"2026-02-04T03:40:56","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/71778.html"},"modified":"2026-02-04T11:40:56","modified_gmt":"2026-02-04T03:40:56","slug":"python%e7%bc%96%e7%a8%8b-%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%ef%bc%88%e6%97%b6%e9%97%b4-time%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/71778.html","title":{"rendered":"Python\u7f16\u7a0b\u2014\u2014\u57fa\u7840\u77e5\u8bc6\uff08\u65f6\u95f4 Time\uff09"},"content":{"rendered":"<p id=\"main-toc\">\u524d\u8a00<\/p>\n<p id=\"%E4%B8%80%E3%80%81%E6%97%B6%E9%97%B4%E6%88%B3%E4%B8%8E%E6%97%B6%E9%97%B4%E5%85%83%E7%BB%84-toc\" style=\"margin-left:40px\">\u4e00\u3001\u65f6\u95f4\u6233\u4e0e\u65f6\u95f4\u5143\u7ec4<\/p>\n<p id=\"1.%20%E6%97%B6%E9%97%B4%E6%88%B3%EF%BC%88Timestamp%EF%BC%89-toc\" style=\"margin-left:80px\">1. \u65f6\u95f4\u6233&#xff08;Timestamp&#xff09;<\/p>\n<p id=\"2.%20%E6%97%B6%E9%97%B4%E5%85%83%E7%BB%84%EF%BC%88struct_time%EF%BC%89-toc\" style=\"margin-left:80px\">2. \u65f6\u95f4\u5143\u7ec4&#xff08;struct_time&#xff09;<\/p>\n<p id=\"%E4%BA%8C%E3%80%81time%20%E6%A8%A1%E5%9D%97%E5%8F%8A%E6%93%8D%E4%BD%9C-toc\" style=\"margin-left:40px\">\u4e8c\u3001time \u6a21\u5757\u53ca\u64cd\u4f5c<\/p>\n<p id=\"1.%20%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%E9%80%9F%E8%A7%88-toc\" style=\"margin-left:80px\">1. \u5e38\u7528\u51fd\u6570\u901f\u89c8<\/p>\n<p id=\"2.%20%E6%A0%BC%E5%BC%8F%E5%8C%96%E4%B8%8E%E8%A7%A3%E6%9E%90%EF%BC%9Astrftime%C2%A0%E4%B8%8E%C2%A0strptime-toc\" style=\"margin-left:80px\">2. \u683c\u5f0f\u5316\u4e0e\u89e3\u6790&#xff1a;strftime\u00a0\u4e0e\u00a0strptime<\/p>\n<p id=\"%E4%B8%89%E3%80%81calendar%20%E6%A8%A1%E5%9D%97%EF%BC%9A%E6%97%A5%E5%8E%86%E4%B8%8E%E6%97%A5%E6%9C%9F%E8%AE%A1%E7%AE%97-toc\" style=\"margin-left:40px\">\u4e09\u3001calendar \u6a21\u5757&#xff1a;\u65e5\u5386\u4e0e\u65e5\u671f\u8ba1\u7b97<\/p>\n<p id=\"%E5%9B%9B%E3%80%81datetime%20%E6%A8%A1%E5%9D%97-toc\" style=\"margin-left:40px\">\u56db\u3001datetime \u6a21\u5757<\/p>\n<p id=\"%E4%BA%94%E3%80%81%E9%AB%98%E7%BA%A7%E5%B7%A5%E5%85%B7-toc\" style=\"margin-left:40px\">\u4e94\u3001\u9ad8\u7ea7\u5de5\u5177<\/p>\n<p id=\"1.%C2%A0pytz%EF%BC%9A%E5%BC%BA%E5%A4%A7%E7%9A%84%E6%97%B6%E5%8C%BA%E5%A4%84%E7%90%86-toc\" style=\"margin-left:80px\">1.\u00a0pytz&#xff1a;\u5f3a\u5927\u7684\u65f6\u533a\u5904\u7406<\/p>\n<p id=\"2.%C2%A0dateutil%EF%BC%9A%E6%99%BA%E8%83%BD%E8%A7%A3%E6%9E%90%E4%B8%8E%E9%87%8D%E5%A4%8D%E8%A7%84%E5%88%99-toc\" style=\"margin-left:80px\">2.\u00a0dateutil&#xff1a;\u667a\u80fd\u89e3\u6790\u4e0e\u91cd\u590d\u89c4\u5219<\/p>\n<p id=\"%E5%85%AD%E3%80%81%E5%BB%BA%E8%AE%AE-toc\" style=\"margin-left:40px\">\u516d\u3001\u5efa\u8bae<\/p>\n<p id=\"%E5%B0%8F%E7%BB%93-toc\" style=\"margin-left:40px\">\u5c0f\u7ed3<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 style=\"text-align:center\">\u524d\u8a00<\/h2>\n<p>\u5728 Python \u7f16\u7a0b\u4e2d&#xff0c;\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u662f\u5e38\u89c1\u4e14\u91cd\u8981\u7684\u4efb\u52a1\u2014\u2014\u65e0\u8bba\u662f\u8bb0\u5f55\u65e5\u5fd7\u3001\u8ba1\u7b97\u65f6\u95f4\u5dee\u3001\u683c\u5f0f\u5316\u8f93\u51fa&#xff0c;\u8fd8\u662f\u5904\u7406\u65f6\u533a\u95ee\u9898\u3002Python \u63d0\u4f9b\u4e86\u591a\u4e2a\u6a21\u5757\u6765\u5e94\u5bf9\u8fd9\u4e9b\u9700\u6c42&#xff0c;\u4e3b\u8981\u5305\u62ec\u00a0time\u3001calendar\u00a0\u548c\u66f4\u73b0\u4ee3\u7684\u00a0datetime\u00a0\u6a21\u5757\u3002\u672c\u6587\u5c06\u7cfb\u7edf\u6027\u5730\u4ecb\u7ecd\u8fd9\u4e9b\u5de5\u5177\u7684\u4f7f\u7528\u65b9\u6cd5\u3001\u6838\u5fc3\u6982\u5ff5\u53ca\u6700\u4f73\u5b9e\u8df5&#xff0c;\u52a9\u4f60\u8f7b\u677e\u9a7e\u9a6d\u65f6\u95f4\u5904\u7406\u3002<\/p>\n<hr \/>\n<h3 id=\"%E4%B8%80%E3%80%81%E6%97%B6%E9%97%B4%E6%88%B3%E4%B8%8E%E6%97%B6%E9%97%B4%E5%85%83%E7%BB%84\">\u4e00\u3001\u65f6\u95f4\u6233\u4e0e\u65f6\u95f4\u5143\u7ec4<\/h3>\n<h4 id=\"1.%20%E6%97%B6%E9%97%B4%E6%88%B3%EF%BC%88Timestamp%EF%BC%89\">1. \u65f6\u95f4\u6233&#xff08;Timestamp&#xff09;<\/h4>\n<ul>\n<li>\u81ea\u00a01970\u5e741\u67081\u65e5 00:00:00 UTC&#xff08;Unix \u7eaa\u5143&#xff09;\u00a0\u8d77\u7ecf\u8fc7\u7684\u79d2\u6570\u3002<\/li>\n<li>\u7c7b\u578b\u4e3a\u6d6e\u70b9\u6570&#xff0c;\u9002\u5408\u8fdb\u884c\u65f6\u95f4\u8fd0\u7b97\u3002<\/li>\n<li>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233&#xff1a;<\/li>\n<\/ul>\n<p>import time<br \/>\nticks &#061; time.time()<br \/>\nprint(&#034;\u5f53\u524d\u65f6\u95f4\u6233:&#034;, ticks)  # \u5982&#xff1a;1738598245.123456 <\/p>\n<p>\u6ce8\u610f&#xff1a;\u4f20\u7edf Unix \u65f6\u95f4\u6233\u53d7\u9650\u4e8e\u00a02038 \u5e74\u95ee\u9898&#xff08;32 \u4f4d\u7cfb\u7edf&#xff09;&#xff0c;\u4f46 Python \u5728 64 \u4f4d\u7cfb\u7edf\u4e0a\u901a\u5e38\u65e0\u6b64\u9650\u5236&#xff0c;\u56e0\u6b64\u5927\u5bb6\u5f53\u505a\u4e86\u89e3\u5386\u53f2\u5c31\u884c\u4e86&#xff0c;\u4f46\u662f\u65f6\u95f4\u6233\u8fd8\u662f\u5f88\u6709\u7528\u7684\u3002<\/p>\n<hr \/>\n<h4 id=\"2.%20%E6%97%B6%E9%97%B4%E5%85%83%E7%BB%84%EF%BC%88struct_time%EF%BC%89\">2. \u65f6\u95f4\u5143\u7ec4&#xff08;struct_time&#xff09;<\/h4>\n<p>time\u00a0\u6a21\u5757\u4f7f\u7528\u4e00\u4e2a\u5305\u542b 9 \u4e2a\u5143\u7d20\u7684\u5143\u7ec4&#xff08;\u6216\u00a0struct_time\u00a0\u5bf9\u8c61&#xff09;\u8868\u793a\u672c\u5730\u6216 UTC \u65f6\u95f4&#xff1a;<\/p>\n<table>\n<tr>\u7d22\u5f15\u5c5e\u6027\u542b\u4e49\u53d6\u503c\u8303\u56f4<\/tr>\n<tbody>\n<tr>\n<td>0<\/td>\n<td>tm_year<\/td>\n<td>\u5e74<\/td>\n<td>\u5982 2026<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>tm_mon<\/td>\n<td>\u6708<\/td>\n<td>1\u201312<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>tm_mday<\/td>\n<td>\u65e5<\/td>\n<td>1\u201331<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>tm_hour<\/td>\n<td>\u5c0f\u65f6<\/td>\n<td>0\u201323<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>tm_min<\/td>\n<td>\u5206\u949f<\/td>\n<td>0\u201359<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>tm_sec<\/td>\n<td>\u79d2<\/td>\n<td>0\u201361&#xff08;\u542b\u95f0\u79d2&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>tm_wday<\/td>\n<td>\u661f\u671f\u51e0&#xff08;0&#061;\u5468\u4e00&#xff09;<\/td>\n<td>0\u20136<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>tm_yday<\/td>\n<td>\u4e00\u5e74\u4e2d\u7684\u7b2c\u51e0\u5929<\/td>\n<td>1\u2013366<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>tm_isdst<\/td>\n<td>\u662f\u5426\u590f\u4ee4\u65f6<\/td>\n<td>-1, 0, 1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5 id=\"%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E6%9C%AC%E5%9C%B0%E6%97%B6%E9%97%B4%E5%85%83%E7%BB%84%EF%BC%9A\">\u83b7\u53d6\u5f53\u524d\u672c\u5730\u65f6\u95f4\u5143\u7ec4&#xff1a;<\/h5>\n<p>local_time &#061; time.localtime()  # \u4f7f\u7528\u5f53\u524d\u65f6\u95f4\u6233<br \/>\nprint(local_time)<br \/>\n# time.struct_time(tm_year&#061;2026, tm_mon&#061;2, tm_mday&#061;3, &#8230;) <\/p>\n<hr \/>\n<h3 id=\"%E4%BA%8C%E3%80%81time%20%E6%A8%A1%E5%9D%97%E5%8F%8A%E6%93%8D%E4%BD%9C\">\u4e8c\u3001time \u6a21\u5757\u53ca\u64cd\u4f5c<\/h3>\n<h4 id=\"1.%20%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%E9%80%9F%E8%A7%88\">1. \u5e38\u7528\u51fd\u6570\u901f\u89c8<\/h4>\n<table>\n<tr>\u51fd\u6570\u529f\u80fd\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>time.time()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u6233<\/td>\n<\/tr>\n<tr>\n<td>time.localtime([secs])<\/td>\n<td>\u5c06\u65f6\u95f4\u6233\u8f6c\u4e3a\u672c\u5730\u65f6\u95f4\u5143\u7ec4<\/td>\n<\/tr>\n<tr>\n<td>time.gmtime([secs])<\/td>\n<td>\u5c06\u65f6\u95f4\u6233\u8f6c\u4e3a UTC \u65f6\u95f4\u5143\u7ec4<\/td>\n<\/tr>\n<tr>\n<td>time.mktime(t)<\/td>\n<td>\u5c06\u65f6\u95f4\u5143\u7ec4\u8f6c\u4e3a\u65f6\u95f4\u6233<\/td>\n<\/tr>\n<tr>\n<td>time.asctime([t])<\/td>\n<td>\u5c06\u65f6\u95f4\u5143\u7ec4\u8f6c\u4e3a\u53ef\u8bfb\u5b57\u7b26\u4e32&#xff08;\u5982\u00a0&#034;Tue Feb 3 21:30:00 2026&#034;&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>time.ctime([secs])<\/td>\n<td>\u7b49\u4ef7\u4e8e\u00a0asctime(localtime(secs))<\/td>\n<\/tr>\n<tr>\n<td>time.sleep(seconds)<\/td>\n<td>\u6682\u505c\u7a0b\u5e8f\u6267\u884c\u6307\u5b9a\u79d2\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"2.%20%E6%A0%BC%E5%BC%8F%E5%8C%96%E4%B8%8E%E8%A7%A3%E6%9E%90%EF%BC%9Astrftime%C2%A0%E4%B8%8E%C2%A0strptime\">2. \u683c\u5f0f\u5316\u4e0e\u89e3\u6790&#xff1a;strftime\u00a0\u4e0e\u00a0strptime<\/h4>\n<ul>\n<li>strftime(format, t)&#xff1a;\u5c06\u65f6\u95f4\u5143\u7ec4\u683c\u5f0f\u5316\u4e3a\u5b57\u7b26\u4e32\u3002<\/li>\n<li>strptime(string, format)&#xff1a;\u5c06\u5b57\u7b26\u4e32\u89e3\u6790\u4e3a\u65f6\u95f4\u5143\u7ec4\u3002<\/li>\n<\/ul>\n<h5 id=\"%E5%B8%B8%E7%94%A8%E6%A0%BC%E5%BC%8F%E7%AC%A6%EF%BC%9A\">\u5e38\u7528\u683c\u5f0f\u7b26&#xff1a;<\/h5>\n<table>\n<tr>\u7b26\u53f7\u542b\u4e49\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>%Y<\/td>\n<td>\u56db\u4f4d\u5e74\u4efd<\/td>\n<td>2026<\/td>\n<\/tr>\n<tr>\n<td>%m<\/td>\n<td>\u6708\u4efd&#xff08;01\u201312&#xff09;<\/td>\n<td>02<\/td>\n<\/tr>\n<tr>\n<td>%d<\/td>\n<td>\u65e5\u671f&#xff08;01\u201331&#xff09;<\/td>\n<td>03<\/td>\n<\/tr>\n<tr>\n<td>%H<\/td>\n<td>24\u5c0f\u65f6\u5236\u5c0f\u65f6&#xff08;00\u201323&#xff09;<\/td>\n<td>21<\/td>\n<\/tr>\n<tr>\n<td>%M<\/td>\n<td>\u5206\u949f&#xff08;00\u201359&#xff09;<\/td>\n<td>30<\/td>\n<\/tr>\n<tr>\n<td>%S<\/td>\n<td>\u79d2&#xff08;00\u201359&#xff09;<\/td>\n<td>45<\/td>\n<\/tr>\n<tr>\n<td>%a<\/td>\n<td>\u7b80\u5199\u661f\u671f\u540d<\/td>\n<td>Tue<\/td>\n<\/tr>\n<tr>\n<td>%A<\/td>\n<td>\u5168\u5199\u661f\u671f\u540d<\/td>\n<td>Tuesday<\/td>\n<\/tr>\n<tr>\n<td>%b<\/td>\n<td>\u7b80\u5199\u6708\u4efd\u540d<\/td>\n<td>Feb<\/td>\n<\/tr>\n<tr>\n<td>%B<\/td>\n<td>\u5168\u5199\u6708\u4efd\u540d<\/td>\n<td>February<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>import time<\/p>\n<p># \u683c\u5f0f\u5316\u5f53\u524d\u65f6\u95f4<br \/>\nnow &#061; time.strftime(&#034;%Y-%m-%d %H:%M:%S&#034;, time.localtime())<br \/>\nprint(now)  # 2026-02-03 21:30:45<\/p>\n<p># \u89e3\u6790\u5b57\u7b26\u4e32\u4e3a\u65f6\u95f4\u5143\u7ec4<br \/>\ndate_str &#061; &#034;2026-02-03 21:30:45&#034;<br \/>\nt &#061; time.strptime(date_str, &#034;%Y-%m-%d %H:%M:%S&#034;)<br \/>\ntimestamp &#061; time.mktime(t)<br \/>\nprint(timestamp)  # \u8f6c\u56de\u65f6\u95f4\u6233 <\/p>\n<hr \/>\n<h3 id=\"%E4%B8%89%E3%80%81calendar%20%E6%A8%A1%E5%9D%97%EF%BC%9A%E6%97%A5%E5%8E%86%E4%B8%8E%E6%97%A5%E6%9C%9F%E8%AE%A1%E7%AE%97\">\u4e09\u3001calendar \u6a21\u5757&#xff1a;\u65e5\u5386\u4e0e\u65e5\u671f\u8ba1\u7b97<\/h3>\n<p>calendar\u00a0\u6a21\u5757\u7528\u4e8e\u751f\u6210\u65e5\u5386\u3001\u5224\u65ad\u95f0\u5e74\u3001\u8ba1\u7b97\u661f\u671f\u7b49\u3002<\/p>\n<h4 id=\"%E5%B8%B8%E7%94%A8%E5%8A%9F%E8%83%BD%EF%BC%9A\">\u5e38\u7528\u529f\u80fd&#xff1a;<\/h4>\n<p>import calendar<\/p>\n<p># \u6253\u5370\u67d0\u6708\u65e5\u5386<br \/>\nprint(calendar.month(2026, 2))<\/p>\n<p># \u5224\u65ad\u662f\u5426\u4e3a\u95f0\u5e74<br \/>\nprint(calendar.isleap(2024))  # True<\/p>\n<p># \u8ba1\u7b97\u4e24\u4e2a\u5e74\u4efd\u95f4\u7684\u95f0\u5e74\u6570\u91cf<br \/>\nprint(calendar.leapdays(2020, 2030))  # 3&#xff08;2020, 2024, 2028&#xff09;<\/p>\n<p># \u83b7\u53d6\u67d0\u6708\u7b2c\u4e00\u5929\u662f\u661f\u671f\u51e0&#xff08;0&#061;\u5468\u4e00&#xff09;\u53ca\u5929\u6570<br \/>\nfirst_weekday, days_in_month &#061; calendar.monthrange(2026, 2)<br \/>\nprint(first_weekday, days_in_month)  # \u5982&#xff1a;6 28&#xff08;2026\u5e742\u67081\u65e5\u662f\u5468\u65e5&#xff0c;\u517128\u5929&#xff09;<\/p>\n<p># \u83b7\u53d6\u67d0\u5929\u662f\u661f\u671f\u51e0<br \/>\nprint(calendar.weekday(2026, 2, 3))  # 1&#xff08;\u5468\u4e8c&#xff09; <\/p>\n<p>\u6ce8\u610f&#xff1a;\u9ed8\u8ba4\u6bcf\u5468\u4ece\u661f\u671f\u4e00\u5f00\u59cb&#xff0c;\u53ef\u901a\u8fc7\u00a0calendar.setfirstweekday(calendar.SUNDAY)\u00a0\u8fd9\u4e2a\u51fd\u6570\u5b9e\u73b0\u4e2a\u4eba\u6240\u9700\u4fee\u6539\u3002<\/p>\n<hr \/>\n<h3 id=\"%E5%9B%9B%E3%80%81datetime%20%E6%A8%A1%E5%9D%97\">\u56db\u3001datetime \u6a21\u5757<\/h3>\n<p>\u867d\u7136\u00a0time\u00a0\u6a21\u5757\u529f\u80fd\u5f3a\u5927&#xff0c;\u4f46\u00a0datetime\u00a0\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u9762\u5411\u5bf9\u8c61\u3001\u66f4\u76f4\u89c2\u7684 API&#xff0c;\u662f\u5f53\u524d Python \u65e5\u671f\u65f6\u95f4\u5904\u7406\u7684\u9996\u9009\u3002<\/p>\n<h4 id=\"%E6%A0%B8%E5%BF%83%E7%B1%BB%EF%BC%9A\">\u6838\u5fc3\u7c7b&#xff1a;<\/h4>\n<ul>\n<li>datetime.date&#xff1a;\u4ec5\u65e5\u671f&#xff08;\u5e74\u3001\u6708\u3001\u65e5&#xff09;<\/li>\n<li>datetime.time&#xff1a;\u4ec5\u65f6\u95f4&#xff08;\u65f6\u3001\u5206\u3001\u79d2\u3001\u5fae\u79d2&#xff09;<\/li>\n<li>datetime.datetime&#xff1a;\u65e5\u671f &#043; \u65f6\u95f4<\/li>\n<li>datetime.timedelta&#xff1a;\u65f6\u95f4\u95f4\u9694&#xff08;\u7528\u4e8e\u52a0\u51cf&#xff09;<\/li>\n<li>datetime.timezone&#xff1a;\u65f6\u533a\u652f\u6301&#xff08;Python 3.2&#043;&#xff09;<\/li>\n<\/ul>\n<p>from datetime import datetime, timedelta<\/p>\n<p># \u83b7\u53d6\u5f53\u524d\u65e5\u671f\u65f6\u95f4<br \/>\nnow &#061; datetime.now()<br \/>\nprint(now)  # 2026-02-03 21:30:45.123456<\/p>\n<p># \u521b\u5efa\u6307\u5b9a\u65e5\u671f<br \/>\ndt &#061; datetime(2026, 2, 3, 21, 30, 45)<\/p>\n<p># \u683c\u5f0f\u5316\u8f93\u51fa<br \/>\nformatted &#061; dt.strftime(&#034;%Y-%m-%d %H:%M:%S&#034;)<br \/>\nprint(formatted)  # 2026-02-03 21:30:45<\/p>\n<p># \u89e3\u6790\u5b57\u7b26\u4e32<br \/>\nparsed &#061; datetime.strptime(&#034;2026-02-03 21:30:45&#034;, &#034;%Y-%m-%d %H:%M:%S&#034;)<\/p>\n<p># \u65f6\u95f4\u8fd0\u7b97<br \/>\nfuture &#061; now &#043; timedelta(days&#061;7, hours&#061;3)<br \/>\nprint(future)<\/p>\n<p># \u65e5\u671f\u5dee<br \/>\ndiff &#061; future &#8211; now<br \/>\nprint(diff.days)  # 7 <\/p>\n<h4 id=\"%E4%BC%98%E5%8A%BF%EF%BC%9A\">\u4f18\u52bf&#xff1a;<\/h4>\n<ul>\n<li>\u652f\u6301\u81ea\u7136\u7684\u65f6\u95f4\u8fd0\u7b97&#xff08;\u5982\u00a0&#043; timedelta&#xff09;<\/li>\n<li>\u66f4\u6e05\u6670\u7684\u5bf9\u8c61\u63a5\u53e3<\/li>\n<li>\u5185\u7f6e\u65f6\u533a\u652f\u6301&#xff08;\u914d\u5408\u00a0zoneinfo\u00a0\u6216\u00a0pytz&#xff09;<\/li>\n<\/ul>\n<hr \/>\n<h3 id=\"%E4%BA%94%E3%80%81%E9%AB%98%E7%BA%A7%E5%B7%A5%E5%85%B7\">\u4e94\u3001\u9ad8\u7ea7\u5de5\u5177<\/h3>\n<h4 id=\"1.%C2%A0pytz%EF%BC%9A%E5%BC%BA%E5%A4%A7%E7%9A%84%E6%97%B6%E5%8C%BA%E5%A4%84%E7%90%86\">1.\u00a0pytz&#xff1a;\u5f3a\u5927\u7684\u65f6\u533a\u5904\u7406<\/h4>\n<p>from datetime import datetime<br \/>\nimport pytz<\/p>\n<p>utc &#061; pytz.UTC<br \/>\neastern &#061; pytz.timezone(&#039;US\/Eastern&#039;)<\/p>\n<p>dt &#061; datetime(2026, 2, 3, 21, 30, tzinfo&#061;utc)<br \/>\neastern_time &#061; dt.astimezone(eastern) <\/p>\n<p>\u6ce8\u610f&#xff1a;Python 3.9&#043; \u4ee5\u53ca\u4e4b\u540e\u7684\u90fd\u63a8\u8350\u4f7f\u7528\u6807\u51c6\u5e93\u00a0zoneinfo\u00a0\u66ff\u4ee3\u00a0pytz\u3002<\/p>\n<h4 id=\"2.%C2%A0dateutil%EF%BC%9A%E6%99%BA%E8%83%BD%E8%A7%A3%E6%9E%90%E4%B8%8E%E9%87%8D%E5%A4%8D%E8%A7%84%E5%88%99\">2.\u00a0dateutil&#xff1a;\u667a\u80fd\u89e3\u6790\u4e0e\u91cd\u590d\u89c4\u5219<\/h4>\n<p>from dateutil.parser import parse<br \/>\nfrom dateutil.relativedelta import relativedelta<\/p>\n<p># \u667a\u80fd\u89e3\u6790\u5404\u79cd\u65e5\u671f\u5b57\u7b26\u4e32<br \/>\ndt &#061; parse(&#034;Feb 3, 2026 at 9:30 PM&#034;)<\/p>\n<p># \u76f8\u5bf9\u65f6\u95f4&#xff08;\u5982\u201c\u4e0b\u4e2a\u6708\u201d\u3001\u201c3\u5e74\u540e\u201d&#xff09;<br \/>\nnext_month &#061; dt &#043; relativedelta(months&#061;1) <\/p>\n<hr \/>\n<h3 id=\"%E5%85%AD%E3%80%81%E5%BB%BA%E8%AE%AE\">\u516d\u3001\u5efa\u8bae<\/h3>\n<li>\u4f18\u5148\u4f7f\u7528\u00a0datetime\u00a0\u800c\u975e\u00a0time&#xff1a;\u4ee3\u7801\u66f4\u6e05\u6670\u3001\u529f\u80fd\u66f4\u5f3a\u5927\u3002<\/li>\n<li>\u5b58\u50a8\u65f6\u95f4\u7528 UTC&#xff1a;\u907f\u514d\u65f6\u533a\u6df7\u4e71&#xff0c;\u663e\u793a\u65f6\u518d\u8f6c\u6362\u4e3a\u672c\u5730\u65f6\u95f4\u3002<\/li>\n<li>\u907f\u514d\u786c\u7f16\u7801\u65f6\u95f4\u683c\u5f0f&#xff1a;\u4f7f\u7528\u5e38\u91cf\u6216\u914d\u7f6e\u7ba1\u7406\u683c\u5f0f\u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\u7528\u00a0dateutil.parser&#xff1a;\u517c\u5bb9\u591a\u79cd\u683c\u5f0f\u3002<\/li>\n<li>\u614e\u7528\u00a0time.sleep()\u00a0\u8fdb\u884c\u5b9a\u65f6\u4efb\u52a1&#xff1a;\u8003\u8651\u4f7f\u7528\u00a0sched\u00a0\u6216\u5916\u90e8\u8c03\u5ea6\u5668\u3002<\/li>\n<hr \/>\n<h3 id=\"%E5%B0%8F%E7%BB%93\">\u5c0f\u7ed3<\/h3>\n<p>Python \u63d0\u4f9b\u4e86\u4ece\u5e95\u5c42\u00a0time\u00a0\u6a21\u5757\u5230\u9ad8\u5c42\u00a0datetime\u00a0\u7684\u5b8c\u6574\u65f6\u95f4\u5904\u7406\u4f53\u7cfb\u3002\u5bf9\u4e8e\u65b0\u9879\u76ee&#xff0c;\u5f3a\u70c8\u63a8\u8350\u4f7f\u7528\u00a0datetime\u00a0\u6a21\u5757&#xff0c;\u5b83\u4e0d\u4ec5\u8bed\u6cd5\u7b80\u6d01&#xff0c;\u8fd8\u652f\u6301\u73b0\u4ee3\u5f00\u53d1\u6240\u9700\u7684\u65f6\u533a\u3001\u8fd0\u7b97\u548c\u683c\u5f0f\u5316\u529f\u80fd\u3002\u800c\u00a0calendar\u00a0\u6a21\u5757\u5219\u5728\u9700\u8981\u65e5\u5386\u903b\u8f91\u65f6\u5927\u663e\u8eab\u624b\u3002<\/p>\n<p>\u201c\u65f6\u95f4\u662f\u7a0b\u5e8f\u4e2d\u6700\u5bb9\u6613\u51fa\u9519\u7684\u90e8\u5206\u4e4b\u4e00 \u2014\u2014 \u7528\u5bf9\u5de5\u5177&#xff0c;\u4e8b\u534a\u529f\u500d\u3002\u201d<\/p>\n<p>\u638c\u63e1\u8fd9\u4e9b\u6a21\u5757&#xff0c;\u4f60\u5c06\u80fd\u4ece\u5bb9\u5e94\u5bf9\u4efb\u4f55\u4e0e\u65e5\u671f\u65f6\u95f4\u76f8\u5173\u7684\u7f16\u7a0b\u6311\u6218&#xff01;<\/p>\n<p>\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u591f\u6709\u6240\u6536\u83b7&#xff0c;\u5927\u5bb6\u7684\u70b9\u8d5e\u548c\u5173\u6ce8\u662f\u5bf9\u6211\u6700\u5927\u7684\u652f\u6301\u548c\u5e2e\u52a9&#xff01;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00<br \/>\n\u4e00\u3001\u65f6\u95f4\u6233\u4e0e\u65f6\u95f4\u5143\u7ec4<br \/>\n1. \u65f6\u95f4\u6233&#xff08;Timestamp&#xff09;<br \/>\n2. \u65f6\u95f4\u5143\u7ec4&#xff08;struct_time&#xff09;<br \/>\n\u4e8c\u3001time \u6a21\u5757\u53ca\u64cd\u4f5c<br \/>\n1. \u5e38\u7528\u51fd\u6570\u901f\u89c8<br \/>\n2. \u683c\u5f0f\u5316\u4e0e\u89e3\u6790&#xff1a;strftime\u00a0\u4e0e\u00a0strptime<br \/>\n\u4e09\u3001calendar \u6a21\u5757&#xff1a;\u65e5\u5386\u4e0e\u65e5\u671f\u8ba1\u7b97<br \/>\n\u56db\u3001datetime \u6a21\u5757<br \/>\n\u4e94\u3001\u9ad8\u7ea7\u5de5\u5177<br \/>\n1.\u00a0pytz&#xff1a;\u5f3a\u5927\u7684\u65f6\u533a\u5904\u7406<br \/>\n2.\u00a0dateutil&amp;#xf<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,190],"topic":[],"class_list":["post-71778","post","type-post","status-publish","format-standard","hentry","category-server","tag-python","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python\u7f16\u7a0b\u2014\u2014\u57fa\u7840\u77e5\u8bc6\uff08\u65f6\u95f4 Time\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/71778.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python\u7f16\u7a0b\u2014\u2014\u57fa\u7840\u77e5\u8bc6\uff08\u65f6\u95f4 Time\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u524d\u8a00 \u4e00\u3001\u65f6\u95f4\u6233\u4e0e\u65f6\u95f4\u5143\u7ec4 1. \u65f6\u95f4\u6233&#xff08;Timestamp&#xff09; 2. \u65f6\u95f4\u5143\u7ec4&#xff08;struct_time&#xff09; \u4e8c\u3001time \u6a21\u5757\u53ca\u64cd\u4f5c 1. \u5e38\u7528\u51fd\u6570\u901f\u89c8 2. \u683c\u5f0f\u5316\u4e0e\u89e3\u6790&#xff1a;strftime\u00a0\u4e0e\u00a0strptime \u4e09\u3001calendar \u6a21\u5757&#xff1a;\u65e5\u5386\u4e0e\u65e5\u671f\u8ba1\u7b97 \u56db\u3001datetime \u6a21\u5757 \u4e94\u3001\u9ad8\u7ea7\u5de5\u5177 1.\u00a0pytz&#xff1a;\u5f3a\u5927\u7684\u65f6\u533a\u5904\u7406 2.\u00a0dateutil&amp;#xf\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/71778.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-04T03:40:56+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/71778.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/71778.html\",\"name\":\"Python\u7f16\u7a0b\u2014\u2014\u57fa\u7840\u77e5\u8bc6\uff08\u65f6\u95f4 Time\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-04T03:40:56+00:00\",\"dateModified\":\"2026-02-04T03:40:56+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/71778.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/71778.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/71778.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\u7f16\u7a0b\u2014\u2014\u57fa\u7840\u77e5\u8bc6\uff08\u65f6\u95f4 Time\uff09\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python\u7f16\u7a0b\u2014\u2014\u57fa\u7840\u77e5\u8bc6\uff08\u65f6\u95f4 Time\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/71778.html","og_locale":"zh_CN","og_type":"article","og_title":"Python\u7f16\u7a0b\u2014\u2014\u57fa\u7840\u77e5\u8bc6\uff08\u65f6\u95f4 Time\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u524d\u8a00 \u4e00\u3001\u65f6\u95f4\u6233\u4e0e\u65f6\u95f4\u5143\u7ec4 1. \u65f6\u95f4\u6233&#xff08;Timestamp&#xff09; 2. \u65f6\u95f4\u5143\u7ec4&#xff08;struct_time&#xff09; \u4e8c\u3001time \u6a21\u5757\u53ca\u64cd\u4f5c 1. \u5e38\u7528\u51fd\u6570\u901f\u89c8 2. \u683c\u5f0f\u5316\u4e0e\u89e3\u6790&#xff1a;strftime\u00a0\u4e0e\u00a0strptime \u4e09\u3001calendar \u6a21\u5757&#xff1a;\u65e5\u5386\u4e0e\u65e5\u671f\u8ba1\u7b97 \u56db\u3001datetime \u6a21\u5757 \u4e94\u3001\u9ad8\u7ea7\u5de5\u5177 1.\u00a0pytz&#xff1a;\u5f3a\u5927\u7684\u65f6\u533a\u5904\u7406 2.\u00a0dateutil&amp;#xf","og_url":"https:\/\/www.wsisp.com\/helps\/71778.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-04T03:40:56+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/71778.html","url":"https:\/\/www.wsisp.com\/helps\/71778.html","name":"Python\u7f16\u7a0b\u2014\u2014\u57fa\u7840\u77e5\u8bc6\uff08\u65f6\u95f4 Time\uff09 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-04T03:40:56+00:00","dateModified":"2026-02-04T03:40:56+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/71778.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/71778.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/71778.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Python\u7f16\u7a0b\u2014\u2014\u57fa\u7840\u77e5\u8bc6\uff08\u65f6\u95f4 Time\uff09"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/71778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=71778"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/71778\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=71778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=71778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=71778"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=71778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}