timeago.js icon indicating copy to clipboard operation
timeago.js copied to clipboard

Limit timeago to parse after a specific timestamp

Open gamesover opened this issue 6 years ago • 4 comments

Basically I want to achieve the same thing as https://github.com/jgraichen/rails-timeago#usage. Actually the function requested in https://github.com/rmm5t/jquery-timeago/issues/95

gamesover avatar May 21 '19 20:05 gamesover

Means when timestamp > someValue, then parse it?

hustcc avatar Jun 17 '19 06:06 hustcc

I also like this feature. If the date is over one year, I want to display the real date: 20-01-2017. How can I do it? Many thanks

saosangmo avatar Aug 06 '19 10:08 saosangmo

This will contains two options?

  • limit
  • format

hustcc avatar Oct 12 '19 03:10 hustcc

For anyone looking for this, i wrote myself a simple function with help of stackoverflow:

import { format } from 'timeago.js'

export const formatDate = (date) => {
  let current_date = new Date();
  let timeDiff = Math.abs(current_date.getTime() - date.getTime());
  let diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
  return (diffDays > 2 ? dateToYMD(date) :format(date))
}

function dateToYMD(date) {
  let strArray=[
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December'
  ];
  let d = date.getDate();
  let m = strArray[date.getMonth()];
  let y = date.getFullYear();

  return `${m} ${d} ${(new Date().getFullYear()) !== y ? y : ''}`
}

Of course you can update the locale string you want to have as your needs.

ziaulrehman40 avatar Mar 25 '20 06:03 ziaulrehman40