APP攻防-资产收集篇FridaHookJS技术综合信息提取双向证书绕过
2026/6/5 21:24:21
RelativeTime 是 DevUI (Angular) 组件库中一个非常实用的日期转换管道(Pipe)。它能够将标准的日期时间字符串,智能地转换为我们日常使用的、易于理解的相对时间表述,如“刚刚”、“2个月前”、“3年后”等,极大地提升了应用的用户体验。
该组件的核心价值在于其**“人性化”**的转换逻辑。它自动判断给定时间与当前时间(或您指定的时间)的跨度,并选择最合适的口语化单位进行展示,避免了用户手动计算时间差的麻烦。
根据官方文档,其主要功能包括:
这是最常见的场景。在模板中,您可以直接对日期数据使用dRelativeTime管道。
<!-- 在您的组件模板中 --><p>发布时间:{{ yourDateObject | dRelativeTime | async }}</p>关键说明:
async管道:因为dRelativeTime是一个异步管道(处理时区和国际化可能涉及异步操作),所以在使用时必须与 Angular 的async管道联用。yourDateObject可以是Date 对象、数字时间戳或ISO 8601 日期字符串(如2025-12-11T00:59:21Z)。转换效果示例(假设当前时间为 2025-12-11 01:00:00):
| 原始时间 (before) | 转换结果 (after translate) |
|---|---|
| 2025-12-11 00:58:51 | 刚刚 |
| 2025-12-11 00:30:00 | 30分钟前 |
| 2025-12-10 12:00:00 | 昨天 |
| 2025-12-15 00:59:21 | 4天后 |
| 2026-02-11 00:59:21 | 2个月后 |
| 2023-12-11 00:59:21 | 2年前 |
如果您需要计算相对于某个特定时间点(而非现在)的相对时间,可以传入第二个参数compareTime。
<p>计划时间:{{ targetDate | dRelativeTime: limit: customCompareDate | async }}</p>在您的组件类(.ts文件)中,需要定义用于比较的日期:
exportclassYourComponent{// 要转换的目标日期targetDate:Date=newDate('2015-06-20T12:00:00Z');// 自定义的比较基准时间customCompareDate:Date=newDate('2015-05-20T12:00:00Z');// 可选的限制参数(见下文)limit:number=10;}转换效果示例(以customCompareDate为 ‘2015-05-20 12:00:00’ 基准):
| 原始时间 (before) | 转换结果 (after translate) |
|---|---|
| 2015-05-20 12:00:30 | 稍后 |
| 2015-05-20 12:18:00 | 18分钟后 |
| 2015-05-19 12:00:00 | 昨天 |
| 2015-06-20 12:00:00 | 下个月 |
| 2014-05-20 12:00:00 | 去年 |
limit第一个可选参数limit用于控制显示完整日期的阈值。
limit值时,管道将不再返回“X年前/后”的相对表述,而是直接返回该日期的完整字符串(调用Date.prototype.toString()的结果)。limit设置为 10,而目标日期2011-05-20与比较日期2015-05-20相差4年(<10),所以仍然显示相对时间“4年前”。如果相差超过10年,则会显示完整的日期字符串Fri May 20 2011 12:00:00 GMT+0000 (GMT)。以下是一个整合了上述功能的 Angular 组件示例:
// your-component.component.tsimport{Component}from'@angular/core';import{RelativeTimeModule}from'ng-devui/relative-time';@Component({selector:'app-your-component',templateUrl:'./your-component.component.html',styleUrls:['./your-component.component.scss']})exportclassYourComponent{// 示例数据 - 用于与‘当前时间’比较datesForCurrent=[newDate('2025-12-11T00:58:51Z'),// 刚刚newDate('2025-12-11T01:42:21Z'),// 42分钟后newDate('2025-12-15T00:59:21Z'),// 4天后newDate('2023-12-11T00:59:21Z'),// 2年前];// 示例数据与自定义比较基准datesForCustom=[newDate('2015-05-20T12:00:30Z'),newDate('2015-05-20T12:18:00Z'),newDate('2015-05-19T12:00:00Z'),newDate('2015-06-20T12:00:00Z'),];customCompareDate:Date=newDate('2015-05-20T12:00:00Z');// 设置阈值yearLimit:number=5;getStringName(date:Date):string{returndate.toUTCString();}}<!-- your-component.component.html --><h3>相对于当前时间</h3><table><tr*ngFor="let date of datesForCurrent"><td>{{ getStringName(date) }}</td><td>{{ date | dRelativeTime | async }}</td></tr></table><h3>相对于自定义时间 (2015-05-20 12:00:00)</h3><table><tr*ngFor="let date of datesForCustom"><td>{{ getStringName(date) }}</td><!-- 传入limit和比较时间 --><td>{{ date | dRelativeTime: yearLimit : customCompareDate | async }}</td></tr></table>总之,DevUI的RelativeTime组件通过简洁的管道式API,将复杂的日期计算与本地化表达封装起来,是构建现代化、友好型Angular应用不可或缺的工具之一。
MateChat:https://gitcode.com/DevCloudFE/MateChat
MateChat官网:https://matechat.gitcode.com
DevUI官网:https://devui.design/home