datex.js 1.0.8 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/datex.min.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * datex.js v1.0.7
2
+ * datex.js v1.0.9
3
3
  * a datetime format library
4
4
  * https://passer-by.com/datex/
5
5
  *
@@ -10,4 +10,4 @@
10
10
  *
11
11
  * Created on: 2023-05-10
12
12
  */
13
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).datex=e()}(this,(function(){"use strict";let t={"en-US":{MMM:["Jan.","Feb.","Mar.","Apr.","May.","Jun.","Jul.","Aug.","Sept.","Oct.","Nov.","Dec."],MMMM:["January","February","March","April","May","June","July","August","September","October","November","December"],Do:["1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th","13th","14th","15th","16th","17th","18th","19th","20th","21st","22nd","23rd","24th","25th","26th","27th","28th","29th","30th","31st"],WW:["Sun.","Mon.","Tues.","Wed.","Thur.","Fri.","Sat."],WWW:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]},"zh-CN":{MMM:["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],MMMM:["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],Do:["1日","2日","3日","4日","5日","6日","7日","8日","9日","10日","11日","12日","13日","14日","15日","16日","17日","18日","19日","20日","21日","22日","23日","24日","25日","26日","27日","28日","29日","30日","31日"],WW:["周日","周一","周二","周三","周四","周五","周六"],WWW:["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]}},e="en-US";"undefined"!=typeof self&&self.navigator&&(e=self.navigator.language);let n=Intl.DateTimeFormat().resolvedOptions().timeZone;const i=["year","month","day","hour","minute","second","millsecond"],a=[1970,1,1,0,0,0,0],s=(t,e)=>new Date(t.toLocaleString("en-US",{timeZone:e}));function r(...t){return new r.prototype.init(...t)}function h(t){return t instanceof r?t:r(t)}return r.setLanguage=function(e,n={}){return t[e]=Object.assign(t[e]||{},n),this},r.switchLanguage=function(t){return e=t,this},r.now=Date.now,r.switchTimezone=function(t){n=t},r.getTimezone=function(){return n},r.prototype={_date:null,_langMap:{},_lang:null,_timezone:null,init:function(...t){if(t.length)if(t[0]instanceof Date)this._date=t[0];else{if(Array.isArray(t[0])?t=a.map(((e,n)=>t[0][n]||e)):null==(e=t[0])||"object"!=typeof e&&"function"!=typeof e||(t=a.map(((e,n)=>t[0][i[n]]||e))),1==t.length&&"string"==typeof t[0]){let e=t[0].match(/(\d{1,4})[\-\/](\d{1,2})[\-\/](\d{1,2})([\sT](\d{1,2})?:(\d{1,2})?(:(\d{1,2}))?(\.(\d{1,3}))?)?/),n=t[0].match(/(\d{1,2})[\-\/](\d{1,2})[\-\/](\d{3,4})([\sT](\d{1,2})?:(\d{1,2})?(:(\d{1,2}))?(\.(\d{1,3}))?)?/),i=t[0].match(/^([12]\d{3})(\d{2})(\d{2})(\d{2})?(\d{2})?(\d{2})?(\d{1,3})?/);e&&!n?t=[1,2,3,5,6,8,10].map((function(t,n){return+(e[t]||a[n])})):n?t=[3,1,2,5,6,8,10].map((function(t,e){return+(n[t]||a[e])})):i&&(t=[1,2,3,4,5,6,7].map((function(t,e){return+(i[t]||a[e])})))}t.length>=3&&t[1]--,this._date=new Date(...t),t.length>=2&&!isNaN(t[0])&&t[0]<100&&this._date.setFullYear(t[0])}else this._date=new Date;var e;return this._date=s(this._date,this._timezone||n),this},setLanguage(t,e={}){return this._langMap[t]=Object.assign(this._langMap[t]||{},e),this},switchLanguage(t){return this._lang=t,this},switchTimezone(t){return this._timezone=t,this._date=s(this._date,this._timezone||n),this},getTimezone(){return this._timezone||n},getTime(){return this._date.getTime()},getUnix(){return~~(this._date.getTime()/1e3)},clone(){return r(this.getTime())},toDate(){return this._date},toObject(){let t=this._date;return{year:t.getFullYear(),month:t.getMonth()+1,day:t.getDate(),hour:t.getHours(),minute:t.getMinutes(),second:t.getSeconds(),millsecond:t.getMilliseconds(),timestamp:t.getTime(),week:t.getDay()}},toArray(){let t=this.toObject();return i.map((e=>t[e]))},toString(){return this._date.toString()},toISOString(){return this._date.toISOString()},set(t,e){let n=this._date,i=this.toObject();switch(t){case"year":n.setFullYear(e);break;case"month":n.setMonth(e-1);break;case"day":n.setDate(e);break;case"hour":n.setHours(e);break;case"minute":n.setMinutes(e);break;case"second":n.setSeconds(e);break;case"millsecond":n.setMilliseconds(e);break;case"timestamp":n.setTime(e);break;case"week":n.setDate(i.day-i.week+e)}return this},get(t){return this.toObject()[t]},change(t,e){let n=this.toObject();return this.set(t,n[t]+e)},format(n="YYYY-MM-DD HH:mm:ss"){let i=this._date,a=this.toObject(),s=i.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/),r={YYYY:""+a.year,YY:(""+a.year).padStart(2,"0"),MM:(""+a.month).padStart(2,"0"),M:""+a.month,DD:(""+a.day).padStart(2,"0"),D:""+a.day,HH:(""+a.hour).padStart(2,"0"),H:""+a.hour,hh:(""+a.hour%12).padStart(2,"0"),h:""+a.hour%12,mm:(""+a.minute).padStart(2,"0"),m:""+a.minute,ss:(""+a.second).padStart(2,"0"),s:""+a.second,S:""+~~(a.millsecond%1e3/100),SS:""+~~(a.millsecond%1e3/10),SSS:""+a.millsecond%1e3,Z:s[1]+s[2]+":"+s[3],ZZ:s[1]+s[2]+s[3],A:["AM","PM"][~~(a.hour/12)],a:["am","pm"][~~(a.hour/12)],X:a.timestamp,x:~~(a.timestamp/1e3),Q:""+~~(a.month/3),W:a.week},h=Object.assign({},t,this._langMap)[this._lang||e];return r.MMM=h.MMM[a.month-1],r.MMMM=h.MMMM[a.month-1],r.Do=h.Do[a.day-1],r.WW=h.WW[a.week],r.WWW=h.WWW[a.week],n.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|Do|A|a|X|x|Q|W+/g,(function(t){return r[t]||""}))},startOf(t){let e=this.toObject(),n=null,s=i.indexOf(t)+1,h=this.toArray(),o=a.slice(s);return h.splice(s,o.length,...o),n="timestamp"==t?this.clone():"week"==t?r(e.year,e.month,e.day-e.week,0,0,0,0):r(...h),n},endOf(t){return this.startOf(t).change(t,"week"==t?7:1).change("millsecond",-1)},diffWith(t,e){if(!(t=h(t)).isValid())return!1;let n={day:864e5,hour:36e5,minute:6e4,second:1e3,millsecond:1},a=this.getTime()-t.getTime(),s=0;if(e){if(n[e])s=~~(a/n[e]);else if("month"==e){s=12*(this.get("year")-1)+this.get("month")-(12*(t.get("year")-1)+t.get("month")),s<0&&this.get("day")>t.get("day")?s+=1:s>0&&this.get("day")<t.get("day")&&(s-=1)}else"year"==e&&(s=this.get("year")-t.get("year"),s<0&&(this.get("month")>t.get("month")||this.get("month")==t.get("month")&&this.get("day")>t.get("day"))?s+=1:s>0&&(this.get("month")<t.get("month")||this.get("month")==t.get("month")&&this.get("day")<t.get("day"))&&(s-=1));return s}{let e=this.clone(),n={};return i.forEach((function(i){n[i]=e.diffWith(t,i),e.set(i,t.get(i))})),n}},isBefore(t,e="timestamp"){return t=h(t),this.get(e)<t.get(e)},isAfter(t,e="timestamp"){return t=h(t),this.get(e)>t.get(e)},isSame(t,e="timestamp"){return t=h(t),this.get(e)==t.get(e)},isBetween(t,e,n="timestamp"){return t=h(t),e=h(e),this.get(n)>t.get(n)&&this.get(n)<e.get(n)},isValid(){return!isNaN(this.getTime())}},r.prototype.init.prototype=r.prototype,r}));
13
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).datex=e()}(this,(function(){"use strict";let t={"en-US":{MMM:["Jan.","Feb.","Mar.","Apr.","May.","Jun.","Jul.","Aug.","Sept.","Oct.","Nov.","Dec."],MMMM:["January","February","March","April","May","June","July","August","September","October","November","December"],Do:["1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th","11th","12th","13th","14th","15th","16th","17th","18th","19th","20th","21st","22nd","23rd","24th","25th","26th","27th","28th","29th","30th","31st"],WW:["Sun.","Mon.","Tues.","Wed.","Thur.","Fri.","Sat."],WWW:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]},"zh-CN":{MMM:["1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],MMMM:["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],Do:["1日","2日","3日","4日","5日","6日","7日","8日","9日","10日","11日","12日","13日","14日","15日","16日","17日","18日","19日","20日","21日","22日","23日","24日","25日","26日","27日","28日","29日","30日","31日"],WW:["周日","周一","周二","周三","周四","周五","周六"],WWW:["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]}},e="en-US";"undefined"!=typeof self&&self.navigator&&(e=self.navigator.language);let n=Intl.DateTimeFormat().resolvedOptions().timeZone,i=0;const a=["year","month","day","hour","minute","second","millsecond"],s=[1970,1,1,0,0,0,0],r=(t,e)=>new Date(t.toLocaleString("en-US",{timeZone:e}));function o(...t){return new o.prototype.init(...t)}function h(t){return t instanceof o?t:o(t)}return o.setLanguage=function(e,n={}){return t[e]=Object.assign(t[e]||{},n),this},o.switchLanguage=function(t){return e=t,this},o.now=Date.now,o.switchTimezone=function(t){n=t,i=r(new Date("1970/1/1"),n).getTime()-new Date("1970/1/1").getTime()},o.getTimezone=function(){return n},o.prototype={_date:null,_langMap:{},_lang:null,_timezone:null,_offset:0,init:function(...t){if(t.length){if(t[0]instanceof Date)this._date=t[0];else{if(Array.isArray(t[0])?t=s.map(((e,n)=>t[0][n]||e)):null==(e=t[0])||"object"!=typeof e&&"function"!=typeof e||(t=s.map(((e,n)=>t[0][a[n]]||e))),1==t.length&&"string"==typeof t[0]){let e=t[0].match(/(\d{1,4})[\-\/](\d{1,2})[\-\/](\d{1,2})([\sT](\d{1,2})?:(\d{1,2})?(:(\d{1,2}))?(\.(\d{1,3}))?)?/),n=t[0].match(/(\d{1,2})[\-\/](\d{1,2})[\-\/](\d{3,4})([\sT](\d{1,2})?:(\d{1,2})?(:(\d{1,2}))?(\.(\d{1,3}))?)?/),i=t[0].match(/^([12]\d{3})(\d{2})(\d{2})(\d{2})?(\d{2})?(\d{2})?(\d{1,3})?/);e&&!n?t=[1,2,3,5,6,8,10].map((function(t,n){return+(e[t]||s[n])})):n?t=[3,1,2,5,6,8,10].map((function(t,e){return+(n[t]||s[e])})):i&&(t=[1,2,3,4,5,6,7].map((function(t,e){return+(i[t]||s[e])})))}t.length>=3&&t[1]--,this._date=new Date(...t),t.length>=2&&!isNaN(t[0])&&t[0]<100&&this._date.setFullYear(t[0])}i&&this._date.setTime(this._date.getTime()-i)}else this._date=new Date;var e;return this},setLanguage(t,e={}){return this._langMap[t]=Object.assign(this._langMap[t]||{},e),this},switchLanguage(t){return this._lang=t,this},switchTimezone(t){return this._timezone=t,this._offset=r(new Date("1970/1/1"),this._timezone).getTime()-new Date("1970/1/1").getTime(),this},getTimezone(){return this._timezone||n},getTime(){return this._date.getTime()},getUnix(){return~~(this._date.getTime()/1e3)},clone(){return o(this.getTime())},toDate(){return this._date},toObject(){let t=this._date;return{year:t.getFullYear(),month:t.getMonth()+1,day:t.getDate(),hour:t.getHours(),minute:t.getMinutes(),second:t.getSeconds(),millsecond:t.getMilliseconds(),timestamp:t.getTime(),week:t.getDay()}},toArray(){let t=this.toObject();return a.map((e=>t[e]))},toString(){return this._date.toString()},toISOString(){return this._date.toISOString()},set(t,e){let n=this._date,i=this.toObject();switch(t){case"year":n.setFullYear(e);break;case"month":n.setMonth(e-1);break;case"day":n.setDate(e);break;case"hour":n.setHours(e);break;case"minute":n.setMinutes(e);break;case"second":n.setSeconds(e);break;case"millsecond":n.setMilliseconds(e);break;case"timestamp":n.setTime(e);break;case"week":n.setDate(i.day-i.week+e)}return this},get(t){return this.toObject()[t]},change(t,e){let n=this.toObject();return this.set(t,n[t]+e)},format(n="YYYY-MM-DD HH:mm:ss"){let a=this.clone(),s=this._offset||i;a._date.setTime(this._date.getTime()+s);let r=a._date,o=a.toObject(),h=r.toTimeString().match(/GMT([\+\-])(\d{2})(\d{2})/),d={YYYY:""+o.year,YY:(""+o.year).padStart(2,"0"),MM:(""+o.month).padStart(2,"0"),M:""+o.month,DD:(""+o.day).padStart(2,"0"),D:""+o.day,HH:(""+o.hour).padStart(2,"0"),H:""+o.hour,hh:(""+o.hour%12).padStart(2,"0"),h:""+o.hour%12,mm:(""+o.minute).padStart(2,"0"),m:""+o.minute,ss:(""+o.second).padStart(2,"0"),s:""+o.second,S:""+~~(o.millsecond%1e3/100),SS:""+~~(o.millsecond%1e3/10),SSS:""+o.millsecond%1e3,Z:h[1]+h[2]+":"+h[3],ZZ:h[1]+h[2]+h[3],A:["AM","PM"][~~(o.hour/12)],a:["am","pm"][~~(o.hour/12)],X:o.timestamp,x:~~(o.timestamp/1e3),Q:""+~~(o.month/3),W:o.week},m=Object.assign({},t,this._langMap)[this._lang||e];return d.MMM=m.MMM[o.month-1],d.MMMM=m.MMMM[o.month-1],d.Do=m.Do[o.day-1],d.WW=m.WW[o.week],d.WWW=m.WWW[o.week],n.replace(/Y+|M+|D+|H+|h+|m+|s+|S+|Z+|Do|A|a|X|x|Q|W+/g,(function(t){return d[t]||""}))},startOf(t){let e=this.toObject(),n=null,i=a.indexOf(t)+1,r=this.toArray(),h=s.slice(i);return r.splice(i,h.length,...h),n="timestamp"==t?this.clone():"week"==t?o(e.year,e.month,e.day-e.week,0,0,0,0):o(...r),n},endOf(t){return this.startOf(t).change(t,"week"==t?7:1).change("millsecond",-1)},diffWith(t,e){if(!(t=h(t)).isValid())return!1;let n={day:864e5,hour:36e5,minute:6e4,second:1e3,millsecond:1},i=this.getTime()-t.getTime(),s=0;if(e){if(n[e])s=~~(i/n[e]);else if("month"==e){s=12*(this.get("year")-1)+this.get("month")-(12*(t.get("year")-1)+t.get("month")),s<0&&this.get("day")>t.get("day")?s+=1:s>0&&this.get("day")<t.get("day")&&(s-=1)}else"year"==e&&(s=this.get("year")-t.get("year"),s<0&&(this.get("month")>t.get("month")||this.get("month")==t.get("month")&&this.get("day")>t.get("day"))?s+=1:s>0&&(this.get("month")<t.get("month")||this.get("month")==t.get("month")&&this.get("day")<t.get("day"))&&(s-=1));return s}{let e=this.clone(),n={};return a.forEach((function(i){n[i]=e.diffWith(t,i),e.set(i,t.get(i))})),n}},isBefore(t,e="timestamp"){return t=h(t),this.get(e)<t.get(e)},isAfter(t,e="timestamp"){return t=h(t),this.get(e)>t.get(e)},isSame(t,e="timestamp"){return t=h(t),this.get(e)==t.get(e)},isBetween(t,e,n="timestamp"){return t=h(t),e=h(e),this.get(n)>t.get(n)&&this.get(n)<e.get(n)},isValid(){return!isNaN(this.getTime())}},o.prototype.init.prototype=o.prototype,o}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datex.js",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "a datetime format library",
5
5
  "author": "HaoLe Zheng",
6
6
  "license": "MIT",
package/src/datex.js CHANGED
@@ -49,6 +49,7 @@ datex.switchTimezone = function(timezone){
49
49
  datex.getTimezone = function(){
50
50
  return _timezone;
51
51
  };
52
+ datex.supportedTimezones = Intl.supportedValuesOf('timeZone');
52
53
 
53
54
  datex.prototype = {
54
55
  _date:null,
@@ -100,22 +101,6 @@ datex.prototype = {
100
101
  }
101
102
  return this;
102
103
  },
103
- setLanguage(lang,data={}){
104
- this._langMap[lang] = Object.assign(this._langMap[lang]||{},data);
105
- return this;
106
- },
107
- switchLanguage(lang){
108
- this._lang = lang;
109
- return this;
110
- },
111
- switchTimezone(timezone){
112
- this._timezone = timezone;
113
- this._offset = convertTimeZone(new Date('1970/1/1'),this._timezone).getTime() - (new Date('1970/1/1')).getTime();
114
- return this;
115
- },
116
- getTimezone(){
117
- return this._timezone||_timezone;
118
- },
119
104
  getTime(){
120
105
  return this._date.getTime();
121
106
  },
@@ -320,6 +305,25 @@ datex.prototype = {
320
305
  endDate = getInstance(endDate);
321
306
  return this.get(unit)>startDate.get(unit)&&this.get(unit)<endDate.get(unit);
322
307
  },
308
+ setLanguage(lang,data={}){
309
+ this._langMap[lang] = Object.assign(this._langMap[lang]||{},data);
310
+ return this;
311
+ },
312
+ switchLanguage(lang){
313
+ this._lang = lang;
314
+ return this;
315
+ },
316
+ switchTimezone(timezone){
317
+ this._timezone = timezone;
318
+ this._offset = convertTimeZone(new Date('1970/1/1'),this._timezone).getTime() - (new Date('1970/1/1')).getTime();
319
+ return this;
320
+ },
321
+ getTimezone(){
322
+ return this._timezone||_timezone;
323
+ },
324
+ getTimezoneOffset(){
325
+ return this.getTimezone() - (this._offset||_offset)/60000;
326
+ },
323
327
  isValid(){
324
328
  return !isNaN(this.getTime());
325
329
  }