Open gamesover opened 5 years ago
Means when timestamp > someValue, then parse it?
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
This will contains two options?
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.
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