@livelybone/date-generator
Version:
A module that generates calendar, which includes years, months, dates, hours, minutes, seconds
10 lines (8 loc) • 5.04 kB
JavaScript
/**
* Bundle of @livelybone/date-generator
* Generated: 2020-05-17
* Version: 4.2.1
* License: MIT
* Author: 2631541504@qq.com
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).dateDateGenerator={})}(this,function(e){"use strict";function t(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,n)}return r}function s(a){for(var e=1;e<arguments.length;e++){var o=null!=arguments[e]?arguments[e]:{};e%2?t(Object(o),!0).forEach(function(e){var t,r,n;t=a,n=o[r=e],r in t?Object.defineProperty(t,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[r]=n}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(o)):t(Object(o)).forEach(function(e){Object.defineProperty(a,e,Object.getOwnPropertyDescriptor(o,e))})}return a}function E(e,t,r){var n=2<arguments.length&&void 0!==r?r:"0";return void 0===n&&(n="0"),0<(e-=(t=t.toString()).length)?new Array(e+(/\./.test(t)?2:1)).join(n)+t:t}function x(e){return(e=+e)===Math.floor(e)&&0<=e}function S(e,t){return(e%t+t)%t}function c(e){return(e=+e)%4==0&&(e%100!=0||e%400==0)}function Y(e,t){return e=+e,2===(t=+t)?c(e)?29:28:Math.ceil(Math.abs(t-7.5))%2==1?31:30}function N(e){if(!e)return null;var t=/^(\d{4})-?(\d{1,2})?-?(\d{1,2})?$/;if(!t.test(e))return console.warn(new Error("Param date `".concat(e,"` is invalid. The right example: 2018[-02][-01]"))),null;var r=e.match(t),n={year:E(4,r[1]),month:E(2,S(+r[2]||1,12)||12)},a=Y(n.year,n.month);return n.date=E(2,S(+r[3]||1,a)||a),n}function B(e,t){var r="string"==typeof e?N(e):e;if(!r)return null;var n=+r.month+t,a=S(n,12),o=Math.floor(n/12);return 0===a&&--o,{year:E(4,+r.year+o),month:E(2,a||12)}}var r,n,o,a;function y(e,t){var r="string"==typeof e?N(e):e,n="string"==typeof t?N(t):t;if(!r||!n)return o.Equal;var a=r.year-n.year;return a?0<a?o.GreatThanYear:o.LessThanYear:(a=r.month-n.month)?0<a?o.GreatThanMonth:o.LessThanMonth:(a=r.date-n.date)?0<a?o.GreatThanDate:o.LessThanDate:o.Equal}function q(e,t){var r="string"==typeof e?N(e):e,n="string"==typeof t?N(t):t;if(!r||!n)return 0;var a=y(r,n);if(0===a)return 0;var o=0<a?n:r,i=0<a?r:n,h=0<a?1:-1,u=i.year-o.year;if(1<u)return(new Array(u).join(",").split("").reduce(function(e,t,r){return e+(c(+o.year+r+1)?366:365)},0)+q(s({},o,{month:12,date:31}),o)+q(i,s({},i,{month:1,date:1}))+1)*h;if(1==u)return(q(s({},o,{month:12,date:31}),o)+q(i,s({},i,{month:1,date:1}))+1)*h;var f=i.month-o.month;return 1<f?(new Array(f).join(",").split("").reduce(function(e,t,r){var n=+o.month+r+1;return e+Y(o.year,n)},0)+q(s({},o,{date:Y(o.year,o.month)}),o)+q(i,s({},i,{date:1}))+1)*h:1==f?(q(s({},o,{date:Y(o.year,o.month)}),o)+q(i,s({},i,{date:1}))+1)*h:(i.date-o.date)*h}(n=r=r||{})[n.Hour=23]="Hour",n[n.Minute=59]="Minute",n[n.Second=59]="Second",(a=o=o||{})[a.GreatThanYear=100]="GreatThanYear",a[a.GreatThanMonth=10]="GreatThanMonth",a[a.GreatThanDate=1]="GreatThanDate",a[a.Equal=0]="Equal",a[a.LessThanDate=-1]="LessThanDate",a[a.LessThanMonth=-10]="LessThanMonth",a[a.LessThanYear=-100]="LessThanYear",e.calcStepBetweenDates=q,e.compareDates=y,e.getDateByStep=function e(t,r){var n="string"==typeof t?N(t):t;if(!n)return null;var a=Y(n.year,n.month),o=+n.date+r;if(o<=0){var i=B(n,-1);return e(s({},i,{date:Y(i.year,i.month)}),o)}return a<o?e(s({},B(n,1),{date:1}),o-a):{year:E(4,n.year),month:E(2,n.month),date:E(2,o)}},e.gntCalendar=function(e,t){var r="string"==typeof e?N(e):e;if(!r)return null;var n=+r.year,a=+r.month;if(t=t||{},!x(n)||!x(a))throw new Error("Prop year and month must be a non-negative number");for(var o,i,h,u,f,s,c,y="string"==typeof t.min?N(t.min):t.min,l="string"==typeof t.max?N(t.max):t.max,m=t.firstDayOfWeek||0,d=B({year:n,month:a},-1),p=Y(d.year,d.month),v=Y(n,a),g=B({year:n,month:a},1),b=Math.ceil(31/7)+1,M=[],w=function(o,i,h){function e(e,t){if(!e)return!0;t=t||1;var r=+e.year,n=+e.month,a=+e.date;return 0<(o-r)*t||o===r&&0<(i-n)*t||o===r&&i===n&&0<=(h-a)*t}return e(y)&&e(l,-1)},T=1,O=1,j=((o=+(o=n),h=+(h=1),s=1==(i=+(i=a))||2===i?(u=Math.floor((o-1)/100),f=(o-1)%100,i+12):(u=Math.floor(o/100),f=o%100,i),S(f+Math.floor(f/4)+Math.floor(u/4)-2*u+Math.floor(26*(s+1)/10)+h-1,7))-m+7)%7,D={year:E(4,(c=new Date).getFullYear()),month:E(2,c.getMonth()+1),date:E(2,c.getDate())},P=0;P<b;P+=1){M[P]=[];for(var G=0;G<7;G+=1)if(0===P&&G<j){var L=p-j+1+G;M[P][G]={year:E(4,d.year),month:E(2,d.month),date:E(2,L),isInThisMonth:!1,canBeChose:w(+d.year,+d.month,L),isNow:!1},M[P][G].isNow=0===q(D,M[P][G])}else T<=v?(M[P][G]={year:E(4,n),month:E(2,a),date:E(2,T),isInThisMonth:!0,canBeChose:w(n,a,T),isNow:!1},M[P][G].isNow=0===q(D,M[P][G]),T+=1):(M[P][G]={year:E(4,g.year),month:E(2,g.month),date:E(2,O),isInThisMonth:!1,canBeChose:w(+g.year,+g.month,O),isNow:!1},M[P][G].isNow=0===q(D,M[P][G]),O+=1)}return M},Object.defineProperty(e,"__esModule",{value:!0})});