【问题标题】:I need four specific time messages to be generated using moment.js我需要使用 moment.js 生成四个特定的时间消息
【发布时间】:2019-07-22 05:11:58
【问题描述】:

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

所以我写了以下函数:

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"

【问题讨论】:

    标签: typescript angular6 momentjs


    【解决方案1】:

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

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

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

    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";
        }    
    }
    

    【讨论】:

    • 卡拉德韦,谢谢。在演示中很热很重。今天将实施并提供反馈。
    • @PeterTheAngularDude 如果您有任何问题,请告诉我
    • 卡拉德维,谢谢你,我会的。 6号之前放假。到时候我会告诉你的。请原谅延迟。
    • 完美!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-10-06
    • 2018-07-02
    • 2020-10-14
    • 1970-01-01
    • 2010-12-22
    • 2013-08-29
    • 1970-01-01
    • 2019-05-12
    相关资源
    最近更新 更多