typescript - 我需要使用 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"
解决方案
从您的描述中不清楚time
您传递的参数是什么样的setTimeDiffStatement
,但对于我的回答,我假设您使用的是unix (epoch) timestamp。
实际上,您甚至不需要使用getDayName
andgetOrdinalNum
方法,因为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";
}
}
推荐阅读
- vue.js - Webpack 开发服务器为 wasm 提供错误的 mime 类型
- ruby - Google People API:search_person_directory_people 不返回任何数据
- jira - 无法授权使用 Rest-Assured
- c++ - 使用标题中定义的命名空间时出现“架构 x86_64 的未定义符号”
- ruby-on-rails - 让 form_with 不生成任何 ID 的原因是什么?
- python - Python:对 matplotlib 颜色和阴影使用字典
- java - Maven 不在 clean 阶段执行脚本 extern,而只在编译或安装时执行
- android - 未知验证 VALIDATE_APP_MESSAGE_NOT_PAID
- javascript - Javascript变量到html数据属性
- r - R中的万圣节糖果力量排名:哪些属性组合导致胜利率增加