From dc3bc873c2ae67451e4f90613ca05e3687ce0962 Mon Sep 17 00:00:00 2001 From: ciaoca Date: Wed, 22 Jun 2022 09:28:38 +0800 Subject: [PATCH] =?UTF-8?q?fix=20formatDate=20=E6=9C=AA=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=88=B0=E8=AF=AD=E8=A8=80=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/js/cxcalendar.es.js | 11 ++++++----- dist/js/cxcalendar.js | 11 ++++++----- dist/js/cxcalendar.min.js | 4 ++-- package-lock.json | 4 ++-- package.json | 2 +- src/index.js | 9 +++++---- 6 files changed, 22 insertions(+), 19 deletions(-) diff --git a/dist/js/cxcalendar.es.js b/dist/js/cxcalendar.es.js index bb7eb5d..4136937 100644 --- a/dist/js/cxcalendar.es.js +++ b/dist/js/cxcalendar.es.js @@ -1,6 +1,6 @@ /** * cxCalendar - * @version 3.0.2 + * @version 3.0.3 * @author ciaoca * @email ciaoca@gmail.com * @site https://github.com/ciaoca/cxCalendar @@ -182,9 +182,10 @@ theTool.parseDate = function(value, mustDef) { }; // 格式化日期值 -theTool.formatDate = function(style, time) { +theTool.formatDate = function(style, time, lang) { const self = this; const theDate = self.parseDate(time); + const language = self.extend({}, cxCalendar.languages.default, lang); if (typeof style !== 'string' || !self.isDate(theDate)) { return time; @@ -207,7 +208,7 @@ theTool.formatDate = function(style, time) { attr.h = self.fillLeadZero(attr.g, 2); attr.i = self.fillLeadZero(theDate.getMinutes(), 2); attr.s = self.fillLeadZero(theDate.getSeconds(), 2); - attr.a = attr.G > 12 ? cacheApi.language.pm : cacheApi.language.am; + attr.a = attr.G > 12 ? language.pm : language.am; const keys = ['timestamp', 'Y', 'y', 'm', 'n', 'd', 'j', 'W', 'H', 'h', 'G', 'g', 'i', 's', 'a']; const reg = new RegExp('(' + keys.join('|') + ')', 'g'); @@ -1504,7 +1505,7 @@ picker.prototype.getDate = function(style) { newValue = []; break; } - newValue.push(theTool.formatDate(style, theDate.getTime())); + newValue.push(theTool.formatDate(style, theDate.getTime(), self.language)); } newValue = self.settings.mode === 'range' ? newValue.join(self.settings.rangeSymbol) : newValue.join(''); @@ -1541,7 +1542,7 @@ picker.prototype.setDate = function(value) { } else if (theTime > self.maxDate.time) { theTime = self.maxDate.time; } - newValue.push(theTool.formatDate(self.settings.format, theTime)); + newValue.push(theTool.formatDate(self.settings.format, theTime, self.language)); } newValue = self.settings.mode === 'range' ? newValue.join(self.settings.rangeSymbol) : newValue.join(''); diff --git a/dist/js/cxcalendar.js b/dist/js/cxcalendar.js index 0646908..bd8f609 100644 --- a/dist/js/cxcalendar.js +++ b/dist/js/cxcalendar.js @@ -1,6 +1,6 @@ /** * cxCalendar - * @version 3.0.2 + * @version 3.0.3 * @author ciaoca * @email ciaoca@gmail.com * @site https://github.com/ciaoca/cxCalendar @@ -188,9 +188,10 @@ theTool.parseDate = function(value, mustDef) { }; // 格式化日期值 -theTool.formatDate = function(style, time) { +theTool.formatDate = function(style, time, lang) { const self = this; const theDate = self.parseDate(time); + const language = self.extend({}, cxCalendar.languages.default, lang); if (typeof style !== 'string' || !self.isDate(theDate)) { return time; @@ -213,7 +214,7 @@ theTool.formatDate = function(style, time) { attr.h = self.fillLeadZero(attr.g, 2); attr.i = self.fillLeadZero(theDate.getMinutes(), 2); attr.s = self.fillLeadZero(theDate.getSeconds(), 2); - attr.a = attr.G > 12 ? cacheApi.language.pm : cacheApi.language.am; + attr.a = attr.G > 12 ? language.pm : language.am; const keys = ['timestamp', 'Y', 'y', 'm', 'n', 'd', 'j', 'W', 'H', 'h', 'G', 'g', 'i', 's', 'a']; const reg = new RegExp('(' + keys.join('|') + ')', 'g'); @@ -1510,7 +1511,7 @@ picker.prototype.getDate = function(style) { newValue = []; break; } - newValue.push(theTool.formatDate(style, theDate.getTime())); + newValue.push(theTool.formatDate(style, theDate.getTime(), self.language)); } newValue = self.settings.mode === 'range' ? newValue.join(self.settings.rangeSymbol) : newValue.join(''); @@ -1547,7 +1548,7 @@ picker.prototype.setDate = function(value) { } else if (theTime > self.maxDate.time) { theTime = self.maxDate.time; } - newValue.push(theTool.formatDate(self.settings.format, theTime)); + newValue.push(theTool.formatDate(self.settings.format, theTime, self.language)); } newValue = self.settings.mode === 'range' ? newValue.join(self.settings.rangeSymbol) : newValue.join(''); diff --git a/dist/js/cxcalendar.min.js b/dist/js/cxcalendar.min.js index 5066ec8..d537410 100644 --- a/dist/js/cxcalendar.min.js +++ b/dist/js/cxcalendar.min.js @@ -1,9 +1,9 @@ /** * cxCalendar - * @version 3.0.2 + * @version 3.0.3 * @author ciaoca * @email ciaoca@gmail.com * @site https://github.com/ciaoca/cxCalendar * @license Released under the MIT license */ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).cxCalendar=t()}(this,(function(){"use strict";const e={dom:{},reg:{isYear:/^\d{4}$/,isTime:/^\d{1,2}(\:\d{1,2}){1,2}$/},cacheDate:{},cxId:1,bindFuns:{},isElement:function(e){return!(!e||"function"!=typeof HTMLElement&&"object"!=typeof HTMLElement||!(e instanceof HTMLElement))||!(!e||!e.nodeType||1!==e.nodeType)},isInteger:function(e){return"string"==typeof e&&/^\-?\d+$/.test(e)&&(e=parseInt(e,10)),"number"==typeof e&&isFinite(e)},isObject:function(e){return null!=e&&"[object Object]"===Object.prototype.toString.call(e)&&!(e.constructor&&!Object.prototype.hasOwnProperty.call(e.constructor.prototype,"isPrototypeOf"))},isDate:function(e){return(e instanceof Date||"[object Date]"===Object.prototype.toString.call(e))&&isFinite(e.getTime())}};let t;e.extend=function(e,...t){const a=this;if(a.isObject(e)){for(let n of t)if(a.isObject(n))for(let t in n)Array.isArray(n[t])?e[t]=[].concat(n[t]):a.isObject(n[t])?(a.isObject(e[t])||(e[t]={}),a.extend(e[t],n[t])):e[t]=n[t];return e}},e.fillLeadZero=function(e,t){let a=String(e);return a.length4?864e5*(8-i):864e5*(1-i),aparseInt(e,10))),t.length<4?t=t.concat(Array(4-t.length).fill(0)):t.length>4&&(t.length=4),n.setHours.apply(n,t)):(e=e.replace(/[\.\/]/g,"-"),/^\d{1,2}-\d{1,2}/.test(e)?e=n.getFullYear()+"-"+e:/^\d{4}-\d{1,2}$/.test(e)&&(e+="-1"),t=e.split(/[\-\sT\:]/).map((e=>parseInt(e,10))),t.length>1&&(t[1]-=1),t.length<7?t=t.concat(Array(7-t.length).fill(0)):t.length>7&&(t.length=7),n.setFullYear.apply(n,t.slice(0,3)),n.setHours.apply(n,t.slice(3)))}else n=null;return!0!==t||a.isDate(n)||(n=new Date),n},e.formatDate=function(e,a){const n=this,s=n.parseDate(a);if("string"!=typeof e||!n.isDate(s))return a;const i={Y:s.getFullYear(),n:s.getMonth()+1,j:s.getDate(),G:s.getHours(),timestamp:s.getTime()};i.y=i.Y.toString(10).slice(-2),i.m=n.fillLeadZero(i.n,2),i.d=n.fillLeadZero(i.j,2),i.W=n.getWeekNum(s),i.H=n.fillLeadZero(i.G,2),i.g=i.G>12?i.G-12:i.G,i.h=n.fillLeadZero(i.g,2),i.i=n.fillLeadZero(s.getMinutes(),2),i.s=n.fillLeadZero(s.getSeconds(),2),i.a=i.G>12?t.language.pm:t.language.am;const o=["timestamp","Y","y","m","n","d","j","W","H","h","G","g","i","s","a"],r=new RegExp("("+o.join("|")+")","g");let l=e;l=l.replace(/([\{\}])/g,"\\$1"),l=l.replace(r,((e,t)=>"{{"+t+"}}")),l=l.replace(/\\\{\{(.)\}\}/g,"$1");for(let e of o)l=l.replace(new RegExp("{{"+e+"}}","g"),i[e]);return l=l.replace(/\\(.)/g,"$1"),l},e.getLanguage=function(e){return this.isObject(e)?e:("string"==typeof e&&e.length||("string"==typeof navigator.language?e=navigator.language:"string"==typeof navigator.browserLanguage&&(e=navigator.browserLanguage)),"string"==typeof e&&(e=e.toLowerCase()),"string"==typeof e&&e.length&&"object"==typeof n.languages[e]?n.languages[e]:n.languages.default)},e.init=function(){this.buildStage(),this.bindEvent()},e.buildStage=function(){const e=this;e.dom.maskBg=document.createElement("div"),e.dom.maskBg.classList.add("cxcalendar_mask"),e.dom.panel=document.createElement("div"),e.dom.panel.classList.add("cxcalendar"),e.dom.head=document.createElement("div"),e.dom.head.classList.add("cxcalendar_hd"),e.dom.main=document.createElement("div"),e.dom.main.classList.add("cxcalendar_bd"),e.dom.acts=document.createElement("div"),e.dom.acts.classList.add("cxcalendar_acts"),e.dom.dateSet=document.createElement("div"),e.dom.timeSet=document.createElement("div"),e.dom.timeSet.classList.add("times"),e.dom.panel.insertAdjacentElement("beforeend",e.dom.main),document.body.insertAdjacentElement("beforeend",e.dom.panel),e.dom.panel.insertAdjacentElement("afterend",e.dom.maskBg)},e.bindEvent=function(){const e=this;e.dom.maskBg.addEventListener("click",(()=>{e.hidePanel()})),e.dom.panel.addEventListener("click",(a=>{const n=a.target,s=n.nodeName.toLowerCase();if("a"===s&&n.rel)switch(event.preventDefault(),n.rel){case"prev":e.gotoPrev();break;case"next":e.gotoNext();break;case"today":e.hidePanel(),t.setDate((new Date).getTime());break;case"clear":e.hidePanel(),t.clearDate();break;case"confirm":e.hidePanel(),"range"===t.settings.mode?e.confirmRange():e.confirmTime()}else if("li"===s&&n.dataset.date){let a=n.dataset.date;if("string"!=typeof a||!a.length)return;for(let e of n.parentNode.parentNode.querySelectorAll("li"))e.classList.remove("selected");if(n.classList.add("selected"),"range"===t.settings.mode){const t=e.parseDate(a).getTime();return"number"!=typeof e.cacheDate.startTime||e.cacheDate.startTime>=t||"number"==typeof e.cacheDate.endTime?(e.cacheDate.startTime=t,delete e.cacheDate.endTime,void n.classList.add("start")):(e.cacheDate.endTime=t,void e.gotoDate())}if("datetime"===t.settings.type)return void(e.cacheDate.time=e.parseDate(a).getTime());e.hidePanel(),t.setDate(a)}})),e.dom.panel.addEventListener("change",(t=>{const a=t.target;"select"===a.nodeName.toLowerCase()&&["year","month"].indexOf(a.name)>=0&&e.gotoDate()}))},e.getSelects=function(e,t){const a=this,n={};for(let s of a.dom.head.querySelectorAll("select"))e.indexOf(s.name)>=0&&(n[s.name]=s,a.isObject(t)&&(t[s.name]=parseInt(s.value,10)));return n},e.buildPanel=function(){const e=this;t.settings.date?(e.cacheDate={time:t.defDate.time},"number"==typeof t.defDate.start&&"number"==typeof t.defDate.end&&(e.cacheDate.startTime=t.defDate.start,e.cacheDate.endTime=t.defDate.end)):e.cacheDate={},e.dom.head.innerHTML="",e.dom.main.innerHTML="";const a=["cxcalendar","m_"+t.settings.type];"range"===t.settings.mode&&a.push("range"),"string"==typeof t.settings.baseClass&&t.settings.baseClass.length&&a.push(t.settings.baseClass),e.dom.panel.className=a.join(" ");const n="",s='',i="range"===t.settings.mode?'
':"";let o="
";if(["month","date","datetime"].indexOf(t.settings.type)>=0){o+='"}else if("year"===t.settings.type){o+='"}["date","datetime"].indexOf(t.settings.type)>=0?(o+=n,o+='',o+=n,o+="
",o+=i,o+=s,e.dom.head.innerHTML=o,e.dom.dateSet.className="days",e.dom.main.insertAdjacentElement("beforeend",e.dom.dateSet),e.dom.panel.insertAdjacentElement("afterbegin",e.dom.head),"datetime"===t.settings.type&&e.buildTimes(),e.rebuildMonthSelect(),e.gotoDate([t.defDate.year,t.defDate.month].join("-"))):"time"===t.settings.type?(e.dom.panel.contains(e.dom.head)&&e.dom.panel.removeChild(e.dom.head),e.buildTimes()):"month"===t.settings.type?(o+=n,o+="",o+=i,o+=s,e.dom.head.innerHTML=o,e.dom.dateSet.className="months",e.dom.main.insertAdjacentElement("beforeend",e.dom.dateSet),e.dom.panel.insertAdjacentElement("afterbegin",e.dom.head),e.gotoDate(t.defDate.year)):"year"===t.settings.type&&(o+="",o+=i,o+=s,e.dom.head.innerHTML=o,e.dom.dateSet.className="years",e.dom.main.insertAdjacentElement("beforeend",e.dom.dateSet),e.dom.panel.insertAdjacentElement("afterbegin",e.dom.head),e.gotoDate(t.defDate.year)),e.buildActs()},e.buildActs=function(){const e=this,a=(new Date).getTime(),n=[];!1!==t.settings.button.today&&"range"!==t.settings.mode&&t.minDate.time<=a&&t.maxDate.time>=a&&n.push("today"),!1!==t.settings.button.clear&&n.push("clear"),("range"===t.settings.mode||["datetime","time"].indexOf(t.settings.type)>=0)&&n.push("confirm");let s="";for(let e of n)s+='';s.length?(e.dom.acts.innerHTML=s,e.dom.panel.insertAdjacentElement("beforeend",e.dom.acts)):e.dom.panel.contains(e.dom.acts)&&e.dom.panel.removeChild(e.dom.acts)},e.rebuildMonthSelect=function(){const e={},a=this.getSelects(["year","month"],e);let n=1,s=12;e.year===t.minDate.year&&e.year===t.maxDate.year?(n=t.minDate.month,s=t.maxDate.month):e.year===t.minDate.year?n=t.minDate.month:e.year===t.maxDate.year&&(s=t.maxDate.month);let i="";for(let a=n;a<=s;a++)i+='