Get the time difference between two datetimes

前端 未结 19 2264
花落未央
花落未央 2020-11-22 05:25

I know I can do anything and some more envolving Dates with momentjs. But embarrassingly, I\'m having a hard time trying to do something that seems simple: geting the differ

19条回答
  •  一个人的身影
    2020-11-22 06:04

    If you want a localized number of days between two dates (startDate, endDate):

    var currentLocaleData = moment.localeData("en");
    var duration = moment.duration(endDate.diff(startDate));
    var nbDays = Math.floor(duration.asDays()); // complete days
    var nbDaysStr = currentLocaleData.relativeTime(returnVal.days, false, "dd", false);
    

    nbDaysStr will contain something like '3 days';

    See https://momentjs.com/docs/#/i18n/changing-locale/ for information on how to display the amount of hours or month, for example.

提交回复
热议问题