Reputation: 7770
I have a date & time format: 2019-11-25 09:49:19
. How can we compare this from current date & time and can display like: 3 days ago
or weeks/ hours/ min ago
Upvotes: 11
Views: 21642
Reputation: 577
As of 2022 Moment
is a legacy project, now in maintenance mode. You probably can use dayjs
. It is a drop-in replacement of moment
.
npm install dayjs
and then
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import localeEn from "dayjs/locale/en"; // With a custom alias for the locale object
const daysago = (postDate) => {
dayjs.extend(relativeTime).locale(localeEn)
var fromNowOn = dayjs(postDate).fromNow();
return(fromNowOn)
};
{daysago("2022-11-07T09:50:00"")}
Upvotes: 2
Reputation: 201
react-time-ago
https://www.npmjs.com/package/react-time-ago
react-time-ago does exactly what you want and more straight out of the box
<ReactTimeAgo date={createdAt} />
Key features that made me happy:
Upvotes: 0
Reputation: 7770
After many hit and trails I found exact solution:
moment.utc("2019-12-04 12:00:24").local().startOf('seconds').fromNow()
Output be like:
30 minutes ago
1 hour ago
2 weeks ago
Upvotes: 27
Reputation: 88
Install moment module
npm install moment
Then, you can find the time ago using the following code (replace time you want to convert with created_at
) .
import moment from 'moment';
const dateTimeAgo = moment(new Date(created_at)).fromNow();
Upvotes: 5
Reputation: 521
This is function i used in my app.Don't need any of the libraries
export const findDaysDiffrent = (fromDate) => {
let CreatedDate = new Date(fromDate)
let today = new Date()
let requiredDiffrentDays
const oneDay = 24 * 60 * 60 * 1000;
const diffDays = Math.round(Math.abs((CreatedDate - today) / oneDay));
if (diffDays >= 360) {
requiredDiffrentDays = Math.floor(diffDays / 360) == 1 ? `${Math.floor(diffDays / 365)} year ago` : `${Math.floor(diffDays / 365)} years ago`
} else if (diffDays >= 30) {
requiredDiffrentDays = Math.floor(diffDays / 30) == 1 ? `${Math.floor(diffDays / 30)} month ago` : `${Math.floor(diffDays / 30)} months ago`
} else if (diffDays < 30) {
requiredDiffrentDays = (diffDays == 1 || diffDays == 0) ? `${diffDays} day ago` : `${diffDays} days ago`
}
return requiredDiffrentDays;
}
Upvotes: 1
Reputation: 637
https://momentjs.com/docs/#/get-set/day/
moment().day(-7); // last Sunday (0 - 7)
moment().day(0); // this Sunday (0)
moment().day(7); // next Sunday (0 + 7)
moment().day(10); // next Wednesday (3 + 7)
moment().day(24); // 3 Wednesdays from now (3 + 7 + 7 + 7)
If the locale assigns Monday as the first day of the week, moment().weekday(0) will be Monday. If Sunday is the first day of the week, moment().weekday(0) will be Sunday.
// when Monday is the first day of the week
moment().weekday(-7); // last Monday
moment().weekday(7); // next Monday
// when Sunday is the first day of the week
moment().weekday(-7); // last Sunday
moment().weekday(7); // next Sunday
Upvotes: 1
Reputation: 821
Currently you can solve your question using moment
. But I provide you some code of VainillaJS to solve your issue too.
const DAYS_OF_WEEK = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const MONTHS_OF_YEAR = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const daysBetween = (date1, date2) => {
const ONE_DAY_ON_SECONDS = 1000 * 60 * 60 * 24;
const date1Ms = date1.getTime();
const date2Ms = date2.getTime();
const differenceMs = date2Ms - date1Ms;
return Math.round(differenceMs / ONE_DAY_ON_SECONDS);
}
const getHoursFromDate = (date) => {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
return hours + ':' + minutes + ' ' + ampm;
}
const dateFromNow = (date) => {
const currentDate = new Date();
if(date.getUTCDate() === currentDate.getUTCDate() && date.getUTCMonth() === currentDate.getUTCMonth() && date.getUTCFullYear() === currentDate.getUTCFullYear()) {
const hours = Math.floor(Math.abs(date - currentDate) / 36e5);
if (hours === 0) {
const minutes = Math.round(((Math.abs(date - currentDate) % 86400000) % 3600000) / 60000);
return minutes <= 1 ? 'A while ago' : `${minutes} minutes ago.`
} else {
return `${Math.floor(hours)} hours ago`;
}
} else {
if (date.getUTCFullYear() < currentDate.getUTCFullYear() || daysBetween(date, currentDate) > 6) {
return `${date.getDate()}/${MONTHS_OF_YEAR[date.getMonth()]} /${date.getFullYear()}`;
} else {
return `${DAYS_OF_WEEK[date.getDay()]} at ${getHoursFromDate(date)}`;
}
}
}
Upvotes: 2
Reputation: 2143
You can use npm i moment
package from npm and use the diff function as below:
moment.duration(nowMoment.diff(updateMoment)).as("hours")
.
Difference can be calculated in terms of days, hours or minutes.
Upvotes: 1
Reputation: 1276
You can use moment.js
library. moment.js
moment("20111031", "YYYYMMDD").fromNow(); // 8 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 7 years ago
moment().startOf('day').fromNow(); // 16 hours ago
moment().endOf('day').fromNow(); // in 8 hours
moment().startOf('hour').fromNow(); // an hour ago
Upvotes: 2