首页 > 解决方案 > 我需要使用 moment.js 生成四个特定的时间消息

问题描述

我有一个函数,我想用它来返回电子签名的状态。每当用户对表单进行电子签名时,都会发生这种情况。

所以我写了以下函数:

setTimeDiffStatement(time: number): string {
  const timeMsgArray = new Array(4);

  // Within the hour, return 
  timeMsgArray[0] = ' 15 minutes ago';
  // Within the date, return 
  timeMsgArray[1] = ' at 2:35 pm';
  // Within the week return
  timeMsgArray[2] = ' on Monday';
  // Outside the week return 
  timeMsgArray[3] = ' on ' + this.theDate.getMonth() + this.theDate.getDay() + this.getOrdinalNum(this.theDate.getDay()) + ' ' + this.theDate.getFullYear();

  const timeMsg = '';

  return '';

}

这是 getOrdinalNum 的代码

getOrdinalNum(n: number): string {
  return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10] : '');
}

当我调用第一个函数时,我需要根据我们使用 Moment.js 的时间返回一个 timeArrayMessages

所以,使用这个函数,我得到了日期名称,我想将时间传递给上面的第一个函数,然后像下面的函数一样构建它:

getDayName(daynbr: number): string {

  const weekday = new Array(7);
  weekday[0] = 'Sunday';
  weekday[1] = 'Monday';
  weekday[2] = 'Tuesday';
  weekday[3] = 'Wednesday';
  weekday[4] = 'Thursday';
  weekday[5] = 'Friday';
  weekday[6] = 'Saturday';

  const n = weekday[daynbr];
  return n;
}

请让我知道实现这些业务规则的简单方法:

当表单被电子签名时,然后在伪代码中

Within the hour, return "15 minutes ago"
Within the date, return "at 2:35 pm"
Within the week return "on Monday"
Outside the week return "on July 10th"

标签: typescriptangular6momentjs

解决方案


从您的描述中不清楚time您传递的参数是什么样的setTimeDiffStatement,但对于我的回答,我假设您使用的是unix (epoch) timestamp

实际上,您甚至不需要使用getDayNameandgetOrdinalNum方法,因为momentjs库会为您处理这一切。

我提出的解决方案计算现在和电子签名时间之间的时间差,然后根据该差计算输出。我使用秒作为计算的基础,但您可以根据需要的精度进行更改。我还在评论中参考了下面的官方文档,这可能有助于您理解该库。

setTimeDiffStatement(time: number): string {
    const eSignatureSignedTime = time  //assuming this is a unix (epoch) timestamp with milliseconds, eg. 1563868827000
    const currentTime = moment().local();
    const timeDifference = currentTime.diff(moment(eSignatureSignedTime), "seconds");

    //Number of seconds after one week:
    const AFTER_ONE_WEEK = 604801;

    //Number of seconds between one day & one week:
    const BEFORE_ONE_WEEK = 604800;
    const AFTER_ONE_DAY = 86401;

    //Number of seconds between one hour & one week:
    const BEFORE_ONE_DAY = 86400;
    const AFTER_ONE_HOUR = 3601;

    //Number of seconds between the starting time & one hour:
    const BEFORE_ONE_HOUR = 3560;
    const STARTING_TIME = 0;

    if (timeDifference >= STARTING_TIME && timeDifference < BEFORE_ONE_HOUR) {
        return moment(eSignatureSignedTime).fromNow();
        //Outputs "15 minutes ago"
        //Documentation: https://momentjs.com/docs/#/displaying/fromnow/

    } else if (timeDifference >= AFTER_ONE_HOUR && timeDifference < BEFORE_ONE_DAY) {
        return "at " + moment(eSignatureSignedTime).format("LT");
        //Outputs "at 2:35 PM"
        //Documentation: https://momentjs.com/docs/#/parsing/string-format/

    } else if (timeDifference >= AFTER_ONE_DAY && timeDifference < BEFORE_ONE_WEEK) {
        return "on " + moment(eSignatureSignedTime).format("dddd");
        //Outputs "on Monday"
        //Documentation: https://momentjs.com/docs/#/parsing/string-format/

    } else if (timeDifference >= AFTER_ONE_WEEK) {
        return "on " + moment(eSignatureSignedTime).format("MMMM Mo");
        //Outputs "on July 10th"
        //Documentation: https://momentjs.com/docs/#/parsing/string-format/

    } else {
        return "Invalid date/time";
    }    
}

推荐阅读