我需要使用 moment.js 生成四个特定时间消息

I need four specific time messages to be generated using moment.js

我有一个函数要用于 return 电子签名的状态。只要用户对表单进行电子签名,就会发生这种情况。

所以我写了以下函数:

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] : '');
}

当我调用第一个函数时,我需要 return 一个基于我们正在使用 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"

不清楚您传递给 setTimeDiffStatementtime 参数从您的描述中看起来像什么,但对于我的回答,我假设您使用的是 unix (epoch) timestamp

您实际上甚至不需要使用您的 getDayNamegetOrdinalNum 方法,因为 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";
    }    
}