Get the time difference between two datetimes

前端 未结 19 2400
花落未央
花落未央 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:01

    Typescript: following should work,

    export const getTimeBetweenDates = ({
      until,
      format
    }: {
      until: number;
      format: 'seconds' | 'minutes' | 'hours' | 'days';
    }): number => {
      const date = new Date();
      const remainingTime = new Date(until * 1000);
      const getFrom = moment([date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()]);
      const getUntil = moment([remainingTime.getUTCFullYear(), remainingTime.getUTCMonth(), remainingTime.getUTCDate()]);
      const diff = getUntil.diff(getFrom, format);
      return !isNaN(diff) ? diff : null;
    };
    

提交回复
热议问题