0.4.4 • Published 11 months ago

@dh-react-hooks/use-timeago v0.4.4

Weekly downloads
7
License
ISC
Repository
github
Last release
11 months ago

@dh-react-hooks/use-timeago

NPM version codecov

react hooks for timeago.js

Installation

npm i @dh-react-hooks/use-timeago

Arguments

propstyperequireddefaultdescription
dateTimeDate / number / stringtrueundefinedstart date
optionsobjectfalseundefinedoptions
  • Options

    optionstypedefaultdescription
    localestringen_USlocale
    localeRegisterfunctionundefinedcustom local register function
    intervalnumber1000update interval duration in milliseconds

Way to Ride

  • Common jS
const useTimeAgo = require('@dh-react-hooks/use-timeago')

  const timeago = useTimeAgo.default(
    Date.now(), 
    {
      interval: 30000
    }
  )
  • ESM
import useTimeAgo from '@dh-react-hooks/use-timeago'
  
  const timeago = useTimeAgo(
    Date.now(), 
    {
      interval: 30000
    }
  )
  • Custom Local
import useTimeAgo from '@dh-react-hooks/use-timeago'

  const localeRegister: function(number, index) {
    return [
      ['剛剛', '片刻後'],
      ['%s 秒前', '%s 秒後'],
      ['1 分鐘前', '1 分鐘後'],
      ['%s 分鐘前', '%s 分鐘後'],
      ['1 小時前', '1 小時後'],
      ['%s 小時前', '%s 小時後'],
      ['1 天前', '1 天後'],
      ['%s 天前', '%s 天後'],
      ['1 週前', '1 週後'],
      ['%s 週前', '%s 週後'],
      ['1 個月前', '1 個月後'],
      ['%s 個月前', '%s 個月後'],
      ['1 年前', '1 年後'],
      ['%s 年前', '%s 年後']
    ][index]
  }

  const timeago = useTimeAgo(
    Date.now(), 
    {
      locale: 'zh_TW',
      localeRegister
    }
  )

Reference

0.4.4

11 months ago

0.4.1

3 years ago

0.4.0

3 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago