{"id":157614,"date":"2020-09-08T23:30:44","date_gmt":"2020-09-08T15:30:44","guid":{"rendered":"http:\/\/4563.org\/?p=157614"},"modified":"2020-09-21T21:26:36","modified_gmt":"2020-09-21T13:26:36","slug":"%e2%8f%b0-moment-js-%e5%ae%a3%e5%b8%83%e5%81%9c%e6%ad%a2%e5%bc%80%e5%8f%91%ef%bc%8c%e7%8e%b0%e5%9c%a8%e8%af%a5%e7%94%a8%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=157614","title":{"rendered":"\u23f0 Moment.js \u5ba3\u5e03\u505c\u6b62\u5f00\u53d1\uff0c\u73b0\u5728\u8be5\u7528\u4ec0\u4e48\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u23f0 Moment.js \u5ba3\u5e03\u505c\u6b62\u5f00\u53d1\uff0c\u73b0\u5728\u8be5\u7528\u4ec0\u4e48\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : iamkun <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<blockquote>\n<p>\u672c\u6587\u6574\u7406\u81ea Monent.js \u5b98\u65b9\u82f1\u6587\u516c\u544a https:\/\/momentjs.com\/docs\/#\/-project-status\/<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/09\/4a015d1681e742a6a8045231dcdab196tplv-k3u1fbpfcp-zoom-1.image_.png\" alt=\"\u23f0 Moment.js \u5ba3\u5e03\u505c\u6b62\u5f00\u53d1\uff0c\u73b0\u5728\u8be5\u7528\u4ec0\u4e48\uff1f\" \/><\/p>\n<p>Moment.js \u5ba3\u5e03\u505c\u6b62\u5f00\u53d1\uff0c\u8fdb\u5165\u7ef4\u62a4\u72b6\u6001\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5927\u800c\u5168\u7684\u65f6\u95f4\u65e5\u671f\u5e93\uff0c\u6781\u5927\u65b9\u4fbf\u4e86\u6211\u4eec\u5728 JavaScript \u4e2d\u8ba1\u7b97\u65f6\u95f4\u548c\u65e5\u671f\uff0c\u6bcf\u5468\u4e0b\u8f7d\u91cf\u8d85\u8fc7 1200 \u4e07\uff0c\u5df2\u6210\u529f\u7528\u4e8e\u6570\u767e\u4e07\u4e2a\u9879\u76ee\u4e2d\u3002<\/p>\n<p>\u4f46\u662f\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u8bde\u751f\u4e8e 2011 \u5e74\u7684\u5143\u8001\u7ea7\u660e\u661f\u9879\u76ee\uff0c\u4ee5\u73b0\u5728\u7684\u773c\u5149\u6765\u770b Moment.js \u5e76\u975e\u5b8c\u7f8e\u65e0\u7f3a\uff0c\u5b98\u65b9\u603b\u7ed3\u4e86\u4e24\u5927\u95ee\u9898\uff1a<\/p>\n<h4>1. \u53ef\u53d8\u5bf9\u8c61<\/h4>\n<p>Moment \u5bf9\u8c61\u662f\u53ef\u53d8\u5bf9\u8c61\uff08 mutable \uff09\uff0c\u7b80\u5355\u70b9\u8bf4\uff0c\u4efb\u4f55\u65f6\u95f4\u4e0a\u7684\u52a0\u51cf\u7b49\u8ba1\u7b97\u90fd\u6539\u53d8\u4e86\u5176\u672c\u8eab\u3002\u8fd9\u79cd\u8bbe\u8ba1\u8ba9\u4ee3\u7801\u53d8\u7684\u5341\u5206\u4e0d\u53ef\u63a7\uff0c\u800c\u4e14\u5f88\u5bb9\u6613\u5e26\u6765\u5404\u79cd\u9690\u853d\u4e14\u96be\u4ee5\u8c03\u8bd5\u7684 bug \u3002\u4ee5\u81f3\u4e8e\u6211\u4eec\u5728\u6bcf\u6b65\u4fee\u6539\u4e4b\u524d\uff0c\u90fd\u8981\u5148\u8c03\u7528 <code>.clone<\/code> \u514b\u9686\u4e00\u6b21\u624d\u80fd\u653e\u5fc3\u64cd\u4f5c\u3002<\/p>\n<h4>2. \u5305\u4f53\u79ef\u8fc7\u5927<\/h4>\n<p>\u56e0\u4e3a Momnet.js \u5c06\u5168\u90e8\u7684\u529f\u80fd\u548c\u6240\u6709\u652f\u6301\u7684\u8bed\u8a00\u90fd\u6253\u5230\u4e00\u4e2a\u5305\u91cc\uff0c\u5305\u7684\u5927\u5c0f\u4e5f\u662f\u5230\u4e86 <strong>280.9 kB<\/strong> \u8fd9\u6837\u4e00\u4e2a\u5938\u5f20\u7684\u6570\u5b57\uff0c\u800c\u4e14\u5bf9\u4e8e Tree shaking \u65e0\u6548\u3002\u5982\u679c\u8981\u4f7f\u7528\u65f6\u533a\u76f8\u5173\u7684\u529f\u80fd\uff0c\u5305\u4f53\u79ef\u66f4\u662f\u6709 <strong>467.6 kB<\/strong> \u7684\u5927\u5c0f\u3002\u7b80\u5355\u70b9\u8bf4\uff0c\u6211\u4eec\u53ef\u80fd\u53ea\u9700\u8981\u4e00\u4e2a <code>.format<\/code> \u683c\u5f0f\u5316\u65f6\u95f4\u7684\u65b9\u6cd5\uff0c\u7528\u6237\u5c31\u9700\u8981\u52a0\u8f7d\u6570\u767e kB \u7684\u5e93\uff0c\u8fd9\u662f\u5341\u5206\u4e0d\u5212\u7b97\u7684\u3002<\/p>\n<p>\u6700\u65b0\u7248\u672c\u7684 Chrome \u5f00\u53d1\u8005\u5de5\u5177\u4e5f\u5f00\u59cb\u5efa\u8bae\u7528\u6237\u66f4\u6362 Moment.js \u4e3a\u540c\u7c7b\u66f4\u5c0f\u7684\u5e93\u3002 <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/09\/c13e2aa3b7ad4004b1af4db25f9c5bd2tplv-k3u1fbpfcp-zoom-1.image_.png\" alt=\"\u23f0 Moment.js \u5ba3\u5e03\u505c\u6b62\u5f00\u53d1\uff0c\u73b0\u5728\u8be5\u7528\u4ec0\u4e48\uff1f\" \/><\/p>\n<p>\u73b0\u5728\uff0cMoment.js \u505c\u6b62\u5f00\u53d1\u4e86\uff0c\u90a3\u6211\u4eec\u63a5\u4e0b\u6765\u8be5\u7528\u4ec0\u4e48\u5462\uff1f<\/p>\n<p>\u5b98\u65b9\u7ed9\u4e86 3 \u79cd\u66ff\u4ee3\u65b9\u6848\uff1a<\/p>\n<h3>1. \u4e0d\u4f7f\u7528\u5e93<\/h3>\n<p>\u5bf9\u4e8e\u4e00\u4e9b\u7b80\u5355\u7684\u65f6\u95f4\u5904\u7406\u9700\u6c42\uff0c\u5176\u5b9e JavaScript \u81ea\u5e26\u7684 <code>Date<\/code> \u548c <code>Intl<\/code> \u5bf9\u8c61\u5b8c\u5168\u53ef\u4ee5\u6ee1\u8db3\u3002\u5f3a\u5927\u7684 <code>Intl<\/code> \u5bf9\u8c61\u53ef\u4ee5\u5c55\u793a\u4e0d\u540c\u65f6\u533a\u4e0d\u540c\u8bed\u8a00\u7684\u65f6\u95f4\u65e5\u671f\u683c\u5f0f\uff0c\u5728\u591a\u6570\u73b0\u4ee3\u6d4f\u89c8\u5668\u4e0a\u5df2\u7ecf\u6709\u5f88\u597d\u7684\u652f\u6301\u3002<\/p>\n<h3>2. Temporal<\/h3>\n<p>\u4e5f\u8bb8\u4eca\u540e\u7684\u67d0\u4e00\u5929\uff0c\u6211\u4eec\u518d\u4e5f\u4e0d\u9700\u8981\u4f7f\u7528\u4efb\u4f55\u5e93\uff0c\u88ab\u770b\u4f5c\u662f\u672a\u6765\u7684\u5168\u65b0\u5185\u7f6e\u7684\u65f6\u95f4\u65e5\u671f\u65b9\u6848 Temporal \u5f88\u503c\u5f97\u671f\u5f85\u3002\u8fd9\u662f\u4e00\u4e2a JS \u8bed\u8a00\u5185\u7f6e\u7684\u91cd\u65b0\u8bbe\u8ba1\u7684\u65f6\u95f4\u548c\u65e5\u671f API\uff0c\u73b0\u5728\u53ef\u4ee5\u901a\u8fc7\u5b9e\u9a8c\u6027\u7684 polyfill \u6765\u5c1d\u8bd5 Temporal\uff0c\u4f46\u79bb\u751f\u4ea7\u4e0a\u5927\u89c4\u6a21\u53ef\u7528\u8fd8\u6709\u5f88\u957f\u7684\u8def\u8981\u8d70\u3002<\/p>\n<h3>3. \u5176\u4ed6\u66ff\u4ee3\u5e93<\/h3>\n<p>\u5b98\u65b9\u63a8\u8350\u4e86 Luxon\uff0cDay.js \uff0cdate-fns \u7b49\u66f4\u5148\u8fdb\u8bbe\u8ba1\u66f4\u4f18\u79c0\u7684\u65f6\u95f4\u65e5\u671f\u5e93\u3002\u76f8\u6bd4\u4e4b\u4e0b\u53ef\u80fd Day.js \u662f\u6700\u503c\u5f97\u5c1d\u8bd5\u7684\u4e00\u4e2a\u3002<\/p>\n<h2>Day.js<\/h2>\n<blockquote>\n<p>\u5b98\u65b9\u63a8\u8350\u8bed\uff1aDay.js \u88ab\u8bbe\u8ba1\u4e3a Moment.js \u7684\u6781\u7b80\u66ff\u4ee3\u54c1\uff0c\u62e5\u6709\u51e0\u4e4e\u4e00\u6837\u7684 API \u3002\u5982\u679c\u4f60\u4e60\u60ef\u4f7f\u7528 Moment \u7684 API \u5e76\u5e0c\u671b\u5feb\u901f\u5165\u95e8\uff0c\u8bf7\u8003\u8651\u4f7f\u7528 Day.js \u3002<\/p>\n<\/blockquote>\n<p>\u4e0a\u9762\u8bf4\u5230\u4e86\u5b98\u65b9\u603b\u7ed3\u7684 Moment.js \u7684\u4e24\u70b9\u8bbe\u8ba1\u7f3a\u9677\uff0c\u53ef\u53d8\u5bf9\u8c61\u964d\u4f4e\u4e86\u4ee3\u7801\u7684\u7ef4\u62a4\u6027\uff0c\u8fc7\u5927\u7684\u5305\u4f53\u79ef\u5f71\u54cd\u4e86\u5f71\u54cd\u4e86\u6574\u4e2a\u9879\u76ee\u7684\u52a0\u8f7d\u901f\u5ea6\u3002<\/p>\n<p>\u800c\u5c06\u8fd1 <strong>3 \u4e07 Github Star<\/strong> \u7684 Day.js \u7684\u8bbe\u8ba1\u7406\u5ff5\u4e0e\u8fd9\u4efd\u603b\u7ed3\u4e0d\u8c0b\u800c\u5408\u3002\u5728\u4fdd\u6301\u4e86\u4f18\u79c0\u7684 API \u8bbe\u8ba1\u4e0d\u53d8\u7684\u540c\u65f6\uff0c\u5f15\u5165\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff08 immutable \uff09\u51cf\u5c11\u4e86\u5f00\u53d1\u65f6\u6240\u9700\u7684\u5fc3\u667a\u6210\u672c\uff0c\u540c\u65f6\u7b80\u5316\u903b\u8f91\u4f7f\u6574\u4e2a\u5305\u4f53\u79ef\u4ec5\u6709 <strong>2 kB<\/strong> \u5927\u5c0f\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/09\/0c1f158ef4144e31ad9a3fa920eccb2etplv-k3u1fbpfcp-zoom-1.image_.png\" alt=\"\u23f0 Moment.js \u5ba3\u5e03\u505c\u6b62\u5f00\u53d1\uff0c\u73b0\u5728\u8be5\u7528\u4ec0\u4e48\uff1f\" \/> https:\/\/github.com\/iamkun\/dayjs<\/p>\n<p>Day.js \u662f\u4e00\u4e2a\u8f7b\u91cf\u7684 JavaScript \u65f6\u95f4\u65e5\u671f\u5904\u7406\u5e93\uff0c\u548c Moment.js \u7684 API \u8bbe\u8ba1\u4fdd\u6301\u5b8c\u5168\u4e00\u6837. \u5982\u679c\u4f60\u66fe\u7ecf\u7528\u8fc7 Moment.js, \u90a3\u4e48\u4f60\u5df2\u7ecf\u77e5\u9053\u5982\u4f55\u4f7f\u7528 Day.js<\/p>\n<p>Day.js \u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff0c\u76f8\u540c\u7684 API\uff0c\u76f8\u540c\u7684\u94fe\u5f0f\u64cd\u4f5c\uff0c\u719f\u6089\u7684\u5473\u9053\u3002<\/p>\n<pre><code>dayjs()     .startOf('month')     .add(1, 'day')     .set('year', 2018)     .format('YYYY-MM-DD HH:mm:ss'); <\/code><\/pre>\n<ul>\n<li>  <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3357434\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : liushuangbill <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u559c\u6b22 date-fns                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3357436\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : liyang5945 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             dayjs                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3357439\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zazzaz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bf4\u7684\u5f88\u4e2d\u80af\uff0c\u7b80\u5355\u65e5\u671f\u5904\u7406\u7528 Date \u5c31\u8db3\u591f\u4e86\uff0c \u641e\u4e0d\u5b9a\u7684\u6211\u9009 Day.js                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3357442\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : justin2018 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             dayjs<br \/>luxon<br \/>day-fns                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3357445\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : revalue <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u66f4\u597d\u5947 dayjs \u662f\u600e\u4e48\u505a\u5230\u66ff\u4ee3 momentjs \u7684\u529f\u80fd\u7684\uff0c\u5c31\u662f\u8ddf\u8fdb\u4e86\u4e00\u4e0b\u5305\u7ba1\u7406\u673a\u5236\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3357452\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : AlynxZhou <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u81ea\u5df1\u7684\u9879\u76ee\u76f4\u63a5\u91cd\u65b0\u8bbe\u8ba1\u4e86\u4e00\u4e0b\u9700\u6c42\uff0c\u7528 Date \u548c Intl.DateTimeFormat \u641e\u5b9a\u4e86\uff0c\u65e9\u5c31\u60f3\u628a\u8fd9\u4e2a\u5927\u5bb6\u4f19\u53bb\u6389\u4e86\uff0c\u53ea\u662f\u6ca1\u4e0b\u5b9a\u51b3\u5fc3\u780d\u9700\u6c42\u2026\u2026                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u23f0 Moment.js \u5ba3\u5e03\u505c\u6b62\u5f00&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/157614"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=157614"}],"version-history":[{"count":1,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/157614\/revisions"}],"predecessor-version":[{"id":157618,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/157614\/revisions\/157618"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=157614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=157614"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=157614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}