{"id":180190,"date":"2024-05-08T20:30:10","date_gmt":"2024-05-08T12:30:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/180190.html"},"modified":"2024-05-08T20:30:14","modified_gmt":"2024-05-08T12:30:14","slug":"r%e8%af%ad%e8%a8%80%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e5%ad%97%e8%bd%ac%e5%8c%96%e4%b8%ba%e6%97%a5%e6%9c%9f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/180190.html","title":{"rendered":"R\u8bed\u8a00\u4e2d\u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u65e5\u671f"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27065315\/2d372c05-1986-4757-9070-f9a64703b2b2.webp\" alt=\"R\u8bed\u8a00\u4e2d\u5982\u4f55\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u65e5\u671f\" \/><\/p>\n<p><p>\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u65e5\u671f\u5728R\u8bed\u8a00\u4e2d\u662f\u4e00\u9879\u5e38\u89c1\u7684\u6570\u636e\u5904\u7406\u64cd\u4f5c\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u51fd\u6570\u5b9e\u73b0\uff0c\u4f8b\u5982<code>as.Date()<\/code>\u3001<code>lubridate<\/code>\u5305\u4e2d\u7684\u51fd\u6570\u3001<code>origin<\/code>\u53c2\u6570\u7684\u4f7f\u7528\u3002<strong>\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>as.Date()<\/code>\u51fd\u6570\u8f6c\u6362\u4ee5\u5929\u4e3a\u5355\u4f4d\u7684\u6570\u5b57\u3001\u5229\u7528<code>lubridate<\/code>\u5305\u4e2d\u7684<code>ymd()<\/code>\u7b49\u51fd\u6570\u5904\u7406\u683c\u5f0f\u5316\u7684\u65e5\u671f\u6570\u5b57\u3001\u901a\u8fc7\u6307\u5b9a\u8d77\u59cb\u65e5\u671f<code>origin<\/code>\u5c06\u4ee5\u5929\u6570\u8868\u793a\u7684\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u3001\u4f7f\u7528<code>POSIXct<\/code>\u548c<code>POSIXlt<\/code>\u683c\u5f0f\u8f6c\u6362\u4ee5\u79d2\u4e3a\u5355\u4f4d\u7684\u6570\u5b57\u4e3a\u65e5\u671f<\/strong>\u3002\u901a\u8fc7\u8fd9\u4e9b\u529f\u80fd\u5f3a\u5927\u7684\u51fd\u6570\uff0c\u7528\u6237\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u5404\u79cd\u5f62\u5f0f\u7684\u6570\u5b57\u6570\u636e\u8f6c\u6362\u4e3a\u6807\u51c6\u7684\u65e5\u671f\u683c\u5f0f\uff0c\u8fdb\u800c\u8fdb\u884c\u5404\u79cd\u65e5\u671f\u76f8\u5173\u7684\u8ba1\u7b97\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 AS.DATE \u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>as.Date()<\/code>\u662fR\u8bed\u8a00\u4e2d\u6700\u57fa\u672c\u7684\u65e5\u671f\u8f6c\u6362\u51fd\u6570\u4e4b\u4e00\uff0c\u5b83\u80fd\u591f\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a&quot;\u5e74-\u6708-\u65e5&quot;\u683c\u5f0f\u7684\u65e5\u671f\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u5c55\u5f00\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e2a\u8f6c\u6362\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5f53\u6570\u5b57\u8868\u793a\u7684\u662f\u8d77\u59cb\u65e5\u671f\uff08\u901a\u5e38\u662f1970-01-01\uff09\u540e\u7684\u5929\u6570\u65f6\uff0c<code>as.Date()<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002\u7528\u6237\u53ea\u9700\u8981\u63d0\u4f9b\u8fd9\u4e2a\u6570\u5b57\u548c\u8d77\u59cb\u65e5\uff08<code>origin<\/code>\u53c2\u6570\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\"># \u4ee51970-01-01\u4e3a\u8d77\u59cb\u65e5\u671f\uff0c\u8f6c\u6362\u5929\u6570\u4e3a\u65e5\u671f<\/p>\n<p>days_since_epoch &lt;- 10000<\/p>\n<p>date &lt;- as.Date(days_since_epoch, origin = &quot;1970-01-01&quot;)<\/p>\n<p>print(date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6570\u5b5710000\u88ab\u8f6c\u6362\u4e3a\u4ece1970-01-01\u7b97\u8d77\u540e\u768410000\u5929\u7684\u65e5\u671f\u3002<code>origin<\/code>\u662f\u4e00\u4e2a\u5173\u952e\u53c2\u6570\uff0c\u5b83\u5b9a\u4e49\u4e86\u6570\u5b57\u4e2d\u7684\u201c\u7b2c\u4e00\u5929\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 LUBRIDATE \u5305<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u5b57\u8f6c\u65e5\u671f\u60c5\u51b5\uff0c\u6bd4\u5982\u6570\u5b57\u662f\u6309\u7167\u7279\u5b9a\u7684\u683c\u5f0f\u6765\u8868\u5f81\u65e5\u671f\u7684\uff0c<code>lubridate<\/code>\u5305\u63d0\u4f9b\u4e86\u5404\u79cd\u529f\u80fd\u5f3a\u5927\u7684\u51fd\u6570\uff0c\u5982<code>ymd()<\/code>\u3001<code>mdy()<\/code>\u3001<code>dmy()<\/code>\u7b49\uff0c\u6765\u5904\u7406\u8fd9\u4e9b\u6570\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\">library(lubridate)<\/p>\n<h2><strong>\u5047\u8bbe\u6570\u5b57\u8868\u793aYYYYMMDD\u683c\u5f0f\u7684\u65e5\u671f<\/strong><\/h2>\n<p>numeric_date &lt;- 20230101<\/p>\n<p>formatted_date &lt;- ymd(numeric_date)<\/p>\n<p>print(formatted_date)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>ymd()<\/code>\u51fd\u6570\u5c06\u4e00\u4e2a\u8868\u793a\u4e3aYYYYMMDD\u683c\u5f0f\u7684\u6570\u5b57\u8f6c\u6362\u4e3a\u4e86\u6b63\u786e\u7684\u65e5\u671f\u683c\u5f0f\u3002<code>lubridate<\/code>\u4e2d\u76f8\u5e94\u7684\u51fd\u6570\u6709\u5f88\u5f3a\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u4ee5\u5904\u7406\u5404\u79cd\u4e0d\u540c\u7684\u6570\u5b57\u8868\u793a\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6307\u5b9a ORIGIN \u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6570\u5b57\u4ee3\u8868\u4ece\u4e00\u4e2a\u7279\u5b9a\u7684\u8d77\u59cb\u65e5\u5f00\u59cb\u8ba1\u7b97\u7684\u5929\u6570\uff0c\u4f8b\u5982\u4ece1900-01-01\u5f00\u59cb\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u5728<code>as.Date()<\/code>\u51fd\u6570\u4e2d\u901a\u8fc7<code>origin<\/code>\u53c2\u6570\u6307\u5b9a\u8fd9\u4e00\u8d77\u59cb\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\"># \u5047\u8bbe\u6570\u5b57\u4ece1900-01-01\u5f00\u59cb\u7b97\u8d77\u7684\u5929\u6570<\/p>\n<p>days_since_1900 &lt;- 45000<\/p>\n<p>date_from_1900 &lt;- as.Date(days_since_1900, origin = &quot;1900-01-01&quot;)<\/p>\n<p>print(date_from_1900)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>origin<\/code>\u53c2\u6570\u7684\u7075\u6d3b\u8fd0\u7528\uff0c\u4f7f\u5f97\u51e0\u4e4e\u53ef\u4ee5\u4ece\u4efb\u4f55\u57fa\u7ebf\u65e5\u671f\u5f00\u59cb\u5c06\u5929\u6570\u8f6c\u6362\u6210\u5b9e\u9645\u65e5\u671f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406 POSIXCT \u548c POSIXLT<\/h3>\n<\/p>\n<p><p>R\u8bed\u8a00\u8fd8\u652f\u6301UNIX\u65f6\u95f4\u6233\u7684\u8f6c\u6362\uff0c\u5373\u5c06\u4ee3\u8868\u81ea1970-01-01\u81f3\u4eca\u79d2\u6570\u7684\u6570\u5b57\u8f6c\u6362\u4e3a\u65e5\u671f\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528<code>as.POSIXct<\/code>\u548c<code>as.POSIXlt<\/code>\u51fd\u6570\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-R\"># \u5047\u5b9a\u4e00\u4e2aUNIX\u65f6\u95f4\u6233\uff08\u79d2\u4e3a\u5355\u4f4d\uff09<\/p>\n<p>timestamp &lt;- 1672531200  # \u4f8b\u59822023-01-01 00:00:00 UTC<\/p>\n<p>date_time &lt;- as.POSIXct(timestamp, origin = &quot;1970-01-01&quot;, tz = &quot;UTC&quot;)<\/p>\n<p>print(date_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8f6c\u6362\u4e86\u4e00\u4e2aUNIX\u65f6\u95f4\u6233\u4e3aPOSIXct\u7c7b\u578b\u7684\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u3002\u540c\u6837\u53ef\u4ee5\u7528<code>as.POSIXlt<\/code>\u8fdb\u884c\u7c7b\u4f3c\u7684\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u7684\u51e0\u79cd\u65b9\u5f0f\uff0c\u6570\u5b57\u53ef\u4ee5\u65b9\u4fbf\u5730\u8f6c\u6362\u4e3a\u65e5\u671f\uff0c\u5e2e\u52a9\u7528\u6237\u8fdb\u884c\u540e\u7eed\u7684\u6570\u636e\u5206\u6790\u5de5\u4f5c\u3002\u5728\u5b9e\u9645\u64cd\u4f5c\u4e2d\uff0c\u7528\u6237\u5e94\u6839\u636e\u5177\u4f53\u7684\u6570\u5b57\u683c\u5f0f\u548c\u9700\u8981\u5f97\u5230\u7684\u65e5\u671f\u7c7b\u578b\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u51fd\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728R\u8bed\u8a00\u4e2d\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u65e5\u671f\uff1f<\/strong><\/p>\n<ul>\n<li><strong>\u95ee\u9898\u89e3\u7b54\uff1a<\/strong> \u5728R\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>as.Date()<\/code>\u51fd\u6570\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u65e5\u671f\u3002\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n<pre><code># \u521b\u5efa\u4e00\u4e2a\u6570\u5b57\u5411\u91cf\nnums &lt;- c(20220101, 20220102, 20220103)\n\n# \u5c06\u6570\u5b57\u5411\u91cf\u8f6c\u5316\u4e3a\u65e5\u671f\ndates &lt;- as.Date(nums, format = &quot;%Y%m%d&quot;)\n\n# \u6253\u5370\u8f6c\u5316\u540e\u7684\u65e5\u671f\u5411\u91cf\nprint(dates)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6570\u5b57\u7684\u5411\u91cf<code>nums<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>as.Date()<\/code>\u51fd\u6570\u5c06\u5176\u4e2d\u7684\u6570\u5b57\u8f6c\u5316\u4e3a\u65e5\u671f\u3002\u5176\u4e2d\uff0c<code>format = &quot;%Y%m%d&quot;<\/code>\u7528\u4e8e\u6307\u5b9a\u6570\u5b57\u7684\u683c\u5f0f\uff0c\u5176\u4e2d<code>%Y<\/code>\u8868\u793a4\u4f4d\u5e74\u4efd\uff0c<code>%m<\/code>\u8868\u793a\u6708\u4efd\uff0c<code>%d<\/code>\u8868\u793a\u65e5\u671f\u3002\u6700\u540e\uff0c\u901a\u8fc7\u6253\u5370<code>dates<\/code>\u5411\u91cf\u53ef\u4ee5\u67e5\u770b\u8f6c\u5316\u540e\u7684\u65e5\u671f\u3002<\/p>\n<ul>\n<li>\n<p><strong>\u76f8\u5173\u63d0\u793a\uff1a<\/strong> \u82e5\u8981\u5c06\u65e5\u671f\u8f6c\u5316\u4e3a\u6570\u5b57\uff0c\u53ef\u4ee5\u4f7f\u7528<code>as.integer()<\/code>\u51fd\u6570\u3002\u540c\u6837\u7684\uff0c\u901a\u8fc7\u6307\u5b9a\u9002\u5f53\u7684\u683c\u5f0f\uff0c\u5c06\u65e5\u671f\u8f6c\u5316\u4e3a\u6570\u5b57\u5411\u91cf\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u989d\u5916\u4fe1\u606f\uff1a<\/strong> R\u8bed\u8a00\u4e2d\u8fd8\u6709\u5176\u4ed6\u7528\u4e8e\u65e5\u671f\u64cd\u4f5c\u7684\u51fd\u6570\uff0c\u5982<code>Sys.Date()<\/code>\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u65e5\u671f\uff0c<code>format()<\/code>\u7528\u4e8e\u683c\u5f0f\u5316\u65e5\u671f\u7684\u663e\u793a\u65b9\u5f0f\u7b49\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u51fd\u6570\u8fdb\u884c\u65e5\u671f\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u5c06\u6570\u5b57\u8f6c\u5316\u4e3a\u65e5\u671f\u5728R\u8bed\u8a00\u4e2d\u662f\u4e00\u9879\u5e38\u89c1\u7684\u6570\u636e\u5904\u7406\u64cd\u4f5c\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u51fd\u6570\u5b9e\u73b0\uff0c\u4f8b\u5982as.Date()\u3001lubri [&hellip;]","protected":false},"author":3,"featured_media":180194,"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\/180190"}],"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=180190"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/180190\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/180194"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=180190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=180190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=180190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}