@nextcloud/vue
Version:
Nextcloud vue components
2 lines (1 loc) • 2.02 kB
JavaScript
;const s=require("@nextcloud/l10n"),n=require("../chunks/l10n-46d0c1c0.cjs"),m=require("../chunks/_plugin-vue2_normalizer-764a4c12.cjs"),l={long:n.t("a few seconds ago"),short:n.t("seconds ago"),narrow:n.t("sec. ago")},d={name:"NcDatetime",props:{timestamp:{type:[Date,Number],required:!0},format:{type:Object,default:()=>({timeStyle:"medium",dateStyle:"short"})},relativeTime:{type:[Boolean,String],default:"long",validator:t=>t===!1||["long","short","narrow"].includes(t)},ignoreSeconds:{type:Boolean,default:!1}},data(){return{currentTime:Date.now(),intervalId:void 0}},computed:{dateObject(){return new Date(this.timestamp)},formattedTime(){if(this.relativeTime!==!1){const t=new Intl.RelativeTimeFormat(s.getCanonicalLocale(),{numeric:"auto",style:this.relativeTime}),e=(this.dateObject-new Date(this.currentTime))/1e3;if(Math.abs(e)<=90)return this.ignoreSeconds?l[this.relativeTime]:t.format(Math.round(e),"second");const a=e/60;if(Math.abs(a)<=90)return t.format(Math.round(a),"minute");const i=a/60;if(Math.abs(i)<=72)return t.format(Math.round(i),"hour");const r=i/24;if(Math.abs(r)<=6)return t.format(Math.round(r),"day");const o=r/7;return Math.abs(o)<=52?t.format(Math.round(o),"week"):t.format(Math.round(r/365),"year")}return this.formattedFullTime},formattedFullTime(){return new Intl.DateTimeFormat(s.getCanonicalLocale(),this.format).format(this.dateObject)}},watch:{relativeTime(t,e){window.clearInterval(this.intervalId),this.intervalId=void 0,t&&(this.intervalId=window.setInterval(this.setCurrentTime,1e3))}},mounted(){this.relativeTime!==!1&&(this.intervalId=window.setInterval(this.setCurrentTime,1e3))},destroyed(){window.clearInterval(this.intervalId)},methods:{setCurrentTime(){this.currentTime=Date.now()}}};var u=function(){var t=this,e=t._self._c;return e("span",{staticClass:"nc-datetime",attrs:{"data-timestamp":t.timestamp,title:t.formattedFullTime}},[t._v(t._s(t.formattedTime))])},h=[],c=m.normalizeComponent(d,u,h,!1,null,null,null,null);const f=c.exports;module.exports=f;