-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-elements.min.js
1 lines (1 loc) · 6.27 KB
/
time-elements.min.js
1
(function(){"use strict";function n(e){return("0"+e).slice(-2)}function r(i,s){var o=i.getDay();var u=i.getDate();var a=i.getMonth();var f=i.getFullYear();var l=i.getHours();var c=i.getMinutes();var h=i.getSeconds();return s.replace(/%([%aAbBcdeHIlmMpPSwyYZz])/g,function(s){var p;var d=s[1];switch(d){case"%":return"%";case"a":return e[o].slice(0,3);case"A":return e[o];case"b":return t[a].slice(0,3);case"B":return t[a];case"c":return i.toString();case"d":return n(u);case"e":return u;case"H":return n(l);case"I":return n(r(i,"%l"));case"l":if(l===0||l===12){return 12}else{return(l+12)%12}break;case"m":return n(a+1);case"M":return n(c);case"p":if(l>11){return"PM"}else{return"AM"}break;case"P":if(l>11){return"pm"}else{return"am"}break;case"S":return n(h);case"w":return o;case"y":return n(f%100);case"Y":return f;case"Z":p=i.toString().match(/\((\w+)\)$/);return p?p[1]:"";case"z":p=i.toString().match(/\w([+-]\d\d\d\d) /);return p?p[1]:""}})}function i(e){this.date=e}function s(){if(o!==null){return o}if(!("Intl"in window)){return false}var e={day:"numeric",month:"short"};var t=new window.Intl.DateTimeFormat(undefined,e);var n=t.format(new Date(0));o=!!n.match(/^\d/);return o}function u(){if(a!==null){return a}if(!("Intl"in window)){return true}var e={day:"numeric",month:"short",year:"numeric"};var t=new window.Intl.DateTimeFormat(undefined,e);var n=t.format(new Date(0));a=!!n.match(/\d,/);return a}function f(e){var t=new Date;return t.getUTCFullYear()===e.getUTCFullYear()}function h(){var e,t,n;for(t=0,n=l.length;t<n;t++){e=l[t];e.textContent=e.getFormattedDate()}}function g(e){var t={weekday:{"short":"%a","long":"%A"},day:{numeric:"%e","2-digit":"%d"},month:{"short":"%b","long":"%B"},year:{numeric:"%Y","2-digit":"%y"}};var n=s()?"weekday day month year":"weekday month day, year";for(var i in t){var o=t[i][e.getAttribute(i)];n=n.replace(i,o||"")}n=n.replace(/(\s,)|(,\s$)/,"");return r(e._date,n).replace(/\s+/," ").trim()}function y(e){var t={hour:e.getAttribute("hour"),minute:e.getAttribute("minute"),second:e.getAttribute("second")};for(var n in t){if(!t[n]){delete t[n]}}if(Object.keys(t).length===0){return}if("Intl"in window){var i=new window.Intl.DateTimeFormat(undefined,t);return i.format(e._date)}var s=t.second?"%H:%M:%S":"%H:%M";return r(e._date,s)}var e=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];var t=["January","February","March","April","May","June","July","August","September","October","November","December"];i.prototype.toString=function(){var e=this.timeElapsed();if(e){return e}else{return"on "+this.formatDate()}};i.prototype.timeElapsed=function(){var e=(new Date).getTime()-this.date.getTime();var t=Math.round(e/1e3);var n=Math.round(t/60);var r=Math.round(n/60);var i=Math.round(r/24);if(e<0){return"just now"}else if(t<10){return"just now"}else if(t<45){return t+" seconds ago"}else if(t<90){return"a minute ago"}else if(n<45){return n+" minutes ago"}else if(n<90){return"an hour ago"}else if(r<24){return r+" hours ago"}else if(r<36){return"a day ago"}else if(i<30){return i+" days ago"}else{return null}};i.prototype.timeAgo=function(){var e=(new Date).getTime()-this.date.getTime();var t=Math.round(e/1e3);var n=Math.round(t/60);var r=Math.round(n/60);var i=Math.round(r/24);var s=Math.round(i/30);var o=Math.round(s/12);if(e<0){return"just now"}else if(t<10){return"just now"}else if(t<45){return t+" seconds ago"}else if(t<90){return"a minute ago"}else if(n<45){return n+" minutes ago"}else if(n<90){return"an hour ago"}else if(r<24){return r+" hours ago"}else if(r<36){return"a day ago"}else if(i<30){return i+" days ago"}else if(i<45){return"a month ago"}else if(s<12){return s+" months ago"}else if(s<18){return"a year ago"}else{return o+" years ago"}};i.prototype.microTimeAgo=function(){var e=(new Date).getTime()-this.date.getTime();var t=e/1e3;var n=t/60;var r=n/60;var i=r/24;var s=i/30;var o=s/12;if(n<1){return"1m"}else if(n<60){return Math.round(n)+"m"}else if(r<24){return Math.round(r)+"h"}else if(i<365){return Math.round(i)+"d"}else{return Math.round(o)+"y"}};var o=null;var a=null;i.prototype.formatDate=function(){var e=s()?"%e %b":"%b %e";if(!f(this.date)){e+=u()?", %Y":" %Y"}return r(this.date,e)};i.prototype.formatTime=function(){if("Intl"in window){var e=new window.Intl.DateTimeFormat(undefined,{hour:"numeric",minute:"2-digit"});return e.format(this.date)}else{return r(this.date,"%l:%M%P")}};var l=[];var c;var p;if("HTMLTimeElement"in window){p=Object.create(window.HTMLTimeElement.prototype)}else{p=Object.create(window.HTMLElement.prototype)}p.attributeChangedCallback=function(e,t,n){if(e==="datetime"){var r=Date.parse(n);this._date=isNaN(r)?null:new Date(r)}var i=this.getFormattedTitle();if(i){this.setAttribute("title",i)}var s=this.getFormattedDate();if(s){this.textContent=s}};p.getFormattedTitle=function(){if(!this._date){return}if(this.hasAttribute("title")){return this.getAttribute("title")}if("Intl"in window){var e={day:"numeric",month:"short",year:"numeric",hour:"numeric",minute:"2-digit",timeZoneName:"short"};var t=new window.Intl.DateTimeFormat(undefined,e);return t.format(this._date)}return this._date.toLocaleString()};var d=Object.create(p);d.createdCallback=function(){var e=this.getAttribute("datetime");if(e){this.attributeChangedCallback("datetime",null,e)}};d.getFormattedDate=function(){if(this._date){return(new i(this._date)).toString()}};d.attachedCallback=function(){l.push(this);if(!c){h();c=setInterval(h,60*1e3)}};d.detachedCallback=function(){var e=l.indexOf(this);if(e!==-1){l.splice(e,1)}if(!l.length){if(c){clearInterval(c);c=null}}};var v=Object.create(d);v.getFormattedDate=function(){if(this._date){var e=this.getAttribute("format");if(e==="micro"){return(new i(this._date)).microTimeAgo()}else{return(new i(this._date)).timeAgo()}}};var m=Object.create(p);m.createdCallback=function(){var e;if(e=this.getAttribute("datetime")){this.attributeChangedCallback("datetime",null,e)}if(e=this.getAttribute("format")){this.attributeChangedCallback("format",null,e)}};m.getFormattedDate=function(){if(!this._date){return}var e=g(this)||"";var t=y(this)||"";return(e+" "+t).trim()};window.RelativeTimeElement=document.registerElement("relative-time",{prototype:d,"extends":"time"});window.TimeAgoElement=document.registerElement("time-ago",{prototype:v,"extends":"time"});window.LocalTimeElement=document.registerElement("local-time",{prototype:m,"extends":"time"})})()