2 lines
1.5 KiB
JavaScript
2 lines
1.5 KiB
JavaScript
"use strict";function t(t){return String(t).padStart(2,"0")}function e(t=new Date){if(t instanceof Date)return new Date(t.getTime());if("number"==typeof t)return new Date(t);if("string"==typeof t){if(/^\d{4}-\d{2}-\d{2}$/.test(t)){const[e,n,r]=t.split("-").map(Number);return new Date(e,n-1,r)}if(/^\d{4}-\d{2}$/.test(t)){const[e,n]=t.split("-").map(Number);return new Date(e,n-1,1)}return new Date(t.replace(/-/g,"/"))}return new Date}function n(n=new Date){const r=e(n);return`${r.getFullYear()}-${t(r.getMonth()+1)}-${t(r.getDate())}`}function r(n=new Date){const r=e(n);return`${r.getFullYear()}-${t(r.getMonth()+1)}`}function o(t){const[e,n]=t.split("-").map(Number);return new Date(e,n,0).getDate()}exports.formatDateLabel=function(t){if(!t)return"";const n=e(t);return`${n.getMonth()+1}月${n.getDate()}日 ${["周日","周一","周二","周三","周四","周五","周六"][n.getDay()]}`},exports.formatMonthLabel=function(t){if(!t)return"";const[e,n]=t.split("-");return`${e}年${Number(n)}月`},exports.getDaysLeftInMonth=function(t){const e=new Date;return r(e)!==t?o(t):o(t)-e.getDate()+1},exports.getMonthSeries=function(t,e=r()){const[n,o]=e.split("-").map(Number),a=new Date(n,o-1,1),s=[];for(let u=t-1;u>=0;u-=1){const t=new Date(a);t.setMonth(a.getMonth()-u),s.push(r(t))}return s},exports.getRecentDateKeys=function(t,r=new Date){const o=[],a=e(r);for(let e=t-1;e>=0;e-=1){const t=new Date(a);t.setDate(a.getDate()-e),o.push(n(t))}return o},exports.isSameMonth=function(t,e){return r(t)===e},exports.parseDate=e,exports.toDateKey=n,exports.toMonthKey=r;
|