Time from now using Moment JS
Add Moment Script
Use the below moment script to get time from now.
Let’s use the below method to display time that is handled by moment. Moment has two different methods
.fromNow(Boolean) to get output.
// Get current datetime
moment().format('lll'); // May 27, 2020 9:19 PM
moment([2020, 0, 29]).fromNow(); // 4 months ago
moment().fromNow(); // a few seconds ago
moment('25-02-2020', 'DD-MM-YYYY').fromNow(); // 3 months ago
moment('25-02-2020', 'DD-MM-YYYY').fromNow(true); // 3 months
// Use future date
moment('25-08-2020', 'DD-MM-YYYY').fromNow(); // in 3 months
moment('25-08-2020', 'DD-MM-YYYY').fromNow(true); // 3 months
.fromNow(Boolean) method will be used to get a value without a suffix. We have already shown you the sample code in above snippets.
Check out the link for more information about the time range and output.
Convert local time to another timezone using Moment JS
That’s it for today.
Thank you for reading. Happy Coding..!!
Thanks for this piece.
I want to ask if there is an algorithm that can help get yesterday’s date or last week date in a read-only using react, if a user enter today’s date.
subtractmethod to get the previous dates. Refer to the following link for more information.
Disable the past and future dates