You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Simple function that recieves a Date object that is in the future or is in the past and gives back the relative time using Intl.RelativeTimeFormat('en', { numeric: 'auto' })
Examples: [ "last year", "6 months ago", "4 weeks ago", "7 days ago", "now", "in 1 minute", "in 6 hours", "tomorrow", "in 3 days", "in 4 weeks", "next month", "in 2 months", "in 12 months", "next year" ]
💚 Examples
import{getRelativeTime}from'@feelinglovelynow/get-relative-time'constd1=newDate()d1.setDate(d1.getDate()-450)getRelativeTime(d1)// last yearconstd2=newDate()d2.setDate(d2.getDate()-180)getRelativeTime(d2)// 6 months agoconstd3=newDate()d3.setDate(d3.getDate()-30)getRelativeTime(d3)// 4 weeks agoconstd4=newDate()d4.setDate(d4.getDate()-7)getRelativeTime(d4)// 7 days agogetRelativeTime(newDate())// nowgetRelativeTime(newDate((newDate()).getTime()+60000))// in 1 minutegetRelativeTime(newDate((newDate()).getTime()+360*60000))// in 6 hoursgetRelativeTime(newDate((newDate()).getTime()+(1440*60000)))// tomorrowgetRelativeTime(newDate((newDate()).getTime()+(3*1440*60000)))// in 3 daysgetRelativeTime(newDate((newDate()).getTime()+(28*1440*60000)))// in 4 weeksgetRelativeTime(newDate((newDate()).getTime()+(35*1440*60000)))// next monthgetRelativeTime(newDate((newDate()).getTime()+(60*1440*60000)))// in 2 monthsgetRelativeTime(newDate((newDate()).getTime()+(365*1440*60000)))// in 12 monthsgetRelativeTime(newDate((newDate()).getTime()+(400*1440*60000)))// next year
🔥 Errors we may throw
getRelativeTime()
if(!(dateinstanceofDate)||date.toString()==='Invalid Date')throw{id: 'fln__get-relative-time__invalid-date',message: 'Please pass getRelativeTime() a valid date object',_errorData: { date }}