hustcc / timeago.js

:clock8: :hourglass: timeago.js is a tiny(2.0 kb) library used to format date with `*** time ago` statement.
https://timeago.org
MIT License
5.32k stars 410 forks source link

Limit timeago to parse after a specific timestamp #183

Open gamesover opened 5 years ago

gamesover commented 5 years ago

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

hustcc commented 5 years ago

Means when timestamp > someValue, then parse it?

saosangmo commented 5 years ago

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

hustcc commented 5 years ago

This will contains two options?

ziaulrehman40 commented 4 years ago

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.