Check time difference in Javascript

后端 未结 18 1162
梦毁少年i
梦毁少年i 2020-11-22 04:27

How would you check time difference from two text-boxes in Javascript?

18条回答
  •  心在旅途
    2020-11-22 04:49

    You can set your custom difference by inserting values in end and updatedAt

    getDifference(theDate: string): string {
    let end = moment(moment(moment()).valueOf());
    let updatedAt = moment(new Date(theDate).valueOf());
    let diff = end.diff(updatedAt, "hour", false);
    if (diff > 8760) {
      diff = end.diff(updatedAt, "years", false);
      return diff > 1 ? diff + " years ago" : diff + " year ago";
    } else if (diff > 730) {
      diff = end.diff(updatedAt, "months", false);
      return diff > 1 ? diff + " months ago" : diff + " month ago";
    } else if (diff > 24) {
      diff = end.diff(updatedAt, "days", false);
      return diff > 1 ? diff + " days ago" : diff + " day ago";
    } else if (diff <= 0) {
      diff = end.diff(updatedAt, "minutes", false);
      return diff > 1 ? diff + " minutes ago" : diff + " minute ago";
    } else return diff > 1 ? diff + " hours ago" : diff + " hour ago";
    

    }

提交回复
热议问题