@nexusui/components
Version:
These are custom components specially-developed for NexusUI applications. They will make your life easier by giving you out-of-the-box implementations for various high-level UI elements that you can drop directly into your application.
2 lines (1 loc) • 1.3 kB
JavaScript
import{useContext as e,useMemo as o,useCallback as r}from"react";import{ThemeContext as t}from"@emotion/react";import{useTranslate as n}from"../../locales/index.js";import a from"./locale.json.js";import{supportedLanguage as m}from"../../locales/i18n.js";const u=6e4,i=36e5,c=864e5,s=7*c,h=30*c,f=365*c;var l;!function(e){e.minute="minute",e.hour="hour",e.day="day",e.week="week",e.month="month",e.year="year"}(l||(l={}));const w={minute:u,hour:i,day:c,week:s,month:h,year:f},y=(e,o)=>{const r=w[e];return Math.round(o/r)},d=(e,o)=>o?`${e}s`:e,g=(e,o)=>{const r=Date.now(),t=e.getTime(),n=Math.abs(r-t);let a=0,m="";return n<6e4?(a="a few",m="seconds"):n<36e5?(a=y(l.minute,n),m=d(l.minute,a>1)):n<1*c?(a=y(l.hour,n),m=d(l.hour,a>1)):n<1*s?(a=y(l.day,n),m=d(l.day,a>1)):n<1*h?(a=y(l.week,n),m=d(l.week,a>1)):n<1*f?(a=y(l.month,n),m=d(l.month,a>1)):(a=y(l.year,n),m=d(l.year,a>1)),o(`${"a few"===a?"a few":1===a?"1":"{{0}}"} ${m} ${r-t>0?"ago":"from now"}`,{0:a})},p=()=>{const u=e(t).language,i=o((()=>m.includes(u)?u:"en-US"),[u]),c=n(a);return{fromNowFormat:r((e=>e?g(new Date(e),c):""),[c]),fullTimeFormat:r((e=>{if(!e)return"";return new Date(e).toLocaleString(i,{hour:"2-digit",minute:"numeric",hour12:!0,month:"long",day:"numeric",year:"numeric"})}),[i])}};export{g as fromNow,p as useTimeFormat};