forked from noahcooper/SimpleDateFormatJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleDateFormat.js
2 lines (2 loc) · 3.89 KB
/
SimpleDateFormat.js
1
2
/* SimpleDateFormatJS | Version: 1.4 (26-JAN-2019) */
function simpleDateFormat(e,p){this.locale=!p||"es_US"!=p&&"en_CA"!=p&&"fr_CA"!=p&&"en_GB"!=p&&"en_AU"!=p?"en_US":p,this.applyPattern=function(e){this.pattern=e||("en_CA"==this.locale||"fr_CA"==this.locale||"en_GB"==this.locale||"en_AU"==this.locale?"d/M/yy":"M/d/yy")},this.applyPattern(e),this.format=function(e){if(!((e=e||new Date)instanceof Date)){var r=e.split("T")[0].split("-"),a=1<e.split("T").length?e.split("T")[1].split(".")[0].split("Z")[0].split("-")[0].split(":"):["00","00","00"];e=new Date(r[0],r[1]-1,r[2],a[0],a[1],a[2])}var l=function(e){return 0==(e=""+e).indexOf("0")&&"0"!=e?e.substring(1):e},u=function(e){return e=Number(e),isNaN(e)?"00":(e<10?"0":"")+e},o={month:u(e.getMonth()+1),date:u(e.getDate()),year:u(e.getFullYear()),day:e.getDay(),hour24:e.getHours(),hour12:e.getHours(),minutes:u(e.getMinutes()),ampm:"AM"};11<o.hour24&&(o.ampm="PM"),o.hour24=u(o.hour24),0==o.hour12&&(o.hour12=12),12<o.hour12&&(o.hour12=o.hour12-12),o.hour12=u(o.hour12);var t=function(e){var r=e.replace(/yy+(?=y)/g,"yy").replace(/MMM+(?=M)/g,"MMM").replace(/d+(?=d)/g,"d").replace(/EEE+(?=E)/g,"EEE").replace(/a+(?=a)/g,"").replace(/k+(?=k)/g,"k").replace(/h+(?=h)/g,"h").replace(/m+(?=m)/g,"m").replace(/yyy/g,o.year).replace(/yy/g,o.year.substring(2)).replace(/y/g,o.year).replace(/dd/g,o.date).replace(/d/g,l(o.date)),a=function(e,r,a){for(var t=1;t<e.length;t++)if(!isNaN(e[t].substring(0,1))){var i=e[t].substring(0,2);e[t]=e[t].substring(2),isNaN(i.substring(1))&&(e[t]=i.substring(1)+e[t],i=i.substring(0,1)),23<(i=Number(i))&&(i=23);var n="+"==a?i:0-i;"kk"==r||"k"==r?24<(n=Number(o.hour24)+n)?n-=24:n<0&&(n+=24):(24<(n=Number(o.hour12)+n)?n-=24:n<0&&(n+=24),12<n&&(n-=12)),n=""+n,"kk"!=r&&"hh"!=r||(n=u(n)),("h"==r&&0==n||"hh"==r&&"00"==n)&&(n="12"),e[t]=n+e[t]}return e.join("")};-1!=r.indexOf("k+")&&(r=a((r=a(r.split("kk+"),"kk","+")).split("k+"),"k","+")),-1!=r.indexOf("k-")&&(r=a((r=a(r.split("kk-"),"kk","-")).split("k-"),"k","-")),-1!=(r=r.replace(/kk/g,o.hour24).replace(/k/g,l(o.hour24))).indexOf("h+")&&(r=a((r=a(r.split("hh+"),"hh","+")).split("h+"),"h","+")),-1!=r.indexOf("h-")&&(r=a((r=a(r.split("hh-"),"hh","-")).split("h-"),"h","-")),r=(r=(r=r.replace(/hh/g,o.hour12<12&&o.hour12.indexOf&&0!=o.hour12.indexOf("0")?"0"+o.hour12:o.hour12).replace(/h/g,l(o.hour12))).replace(/mm/g,o.minutes).replace(/m/g,l(o.minutes))).replace(/a/g,"A");var t=["January","February","march","april","may","June","July","august","September","October","November","December"];"es_US"==p&&(t=["enero","febrero","marzo","abril","mayo","junio","julio","agosto","septiembre","octubre","noviembre","diciembre"]),"fr_CA"==p&&(t=["janvier","février","mars","avril","mai","juin","juillet","août","septembre","octobre","novembre","décembre"]);var i=t[Number(o.month)-1].substring(0,3);"fr_CA"==p&&("f&#"==i?i="fév":"ao&"==i?i="aoû":"d&#"==i&&(i="déc")),r=r.replace(/MMMM/g,t[Number(o.month)-1]).replace(/MMM/g,i).replace(/MM/g,o.month).replace(/M/g,l(o.month));var n=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];"es_US"==p&&(n=["domingo","lunes","martes","miércoles","jueves","viernes","sábado"]),"fr_CA"==p&&(n=["dimanche","lundi","mardi","mercredi","jeudi","vendredi","samedi"]);var s=n[o.day].substring(0,3);return"es_US"==p&&("mi&"==s?s="mié":"s&a"==s&&(s="sáb")),r=(r=r.replace(/EEEE/g,n[o.day]).replace(/EEE/g,s).replace(/EE/g,s).replace(/E/g,s)).replace(/A/g,o.ampm).replace(/apr/g,"Apr").replace(/aug/g,"Aug"),"es_US"!=p&&"fr_CA"!=p&&(r=r.replace(/mar/g,"Mar").replace(/may/g,"May")),r};if(-1!=this.pattern.indexOf("'")){var i=this.pattern.replace(/\'+(?=\')/g,"''").split("''");if(1==i.length){i=this.pattern.split("'");for(var n=0;n<i.length;n++)n%2==0&&(i[n]=t(i[n]));return i.join("")}for(n=0;n<i.length;n++){for(var s=i[n].split("'"),h=0;h<s.length;h++)h%2==0&&(s[h]=t(s[h]));i[n]=s.join("")}return i.join("'")}return t(this.pattern)}}