|
| 1 | +# 时间标签 |
| 2 | + |
| 3 | +## 日期时间格式化标签 |
| 4 | + |
| 5 | +支持日期时间格式化标签,可以自定义日期时间格式。 |
| 6 | + |
| 7 | +::: tabs |
| 8 | + |
| 9 | +== 示例 |
| 10 | + |
| 11 | +<Example> |
| 12 | + <p>现在是:<strong zui-create="timespan" zui-create-timespan="{time: Date.now(), format: 'yyyy年MM月dd日 HH:mm'}"></strong></p> |
| 13 | + <p>不合法的时间:<u zui-create="timespan" zui-create-timespan="{time: '2026-1x23 10:00:00', format: 'yyyy年MM月dd日 HH:mm'}"></u></p> |
| 14 | +</Example> |
| 15 | + |
| 16 | +== HTML |
| 17 | + |
| 18 | +```html |
| 19 | +<p>现在是:<strong zui-create="timespan" zui-create-timespan="{time: Date.now(), format: 'yyyy年MM月dd日 HH:mm'}"></strong></p> |
| 20 | +<p>不合法的时间:<u zui-create="timespan" zui-create-timespan="{time: '2026-1x23 10:00:00', format: 'yyyy年MM月dd日 HH:mm'}"></u></p> |
| 21 | +``` |
| 22 | + |
| 23 | +::: |
| 24 | + |
| 25 | +## 相对时间标签 |
| 26 | + |
| 27 | +支持将给定时间显示为相对当前时间的描述文本,例如:1天前、1小时前、1分钟前、刚刚等。 |
| 28 | + |
| 29 | +::: tabs |
| 30 | + |
| 31 | +== 示例 |
| 32 | + |
| 33 | +<Example> |
| 34 | + <p>页面加载于: <strong zui-create="timeago" zui-create-timeago="{time: Date.now()}"></strong></p> |
| 35 | + <p>功能开发于: <strong zui-create="timeago" zui-create-timeago="{time: '2026-1-22 10:00:00'}"></strong></p> |
| 36 | + <p>不合法的时间: <strong zui-create="timeago" zui-create-timeago="{time: '2026-1x22 10:00:00', hint: '2026-1-22 10:00:00'}"></strong></p> |
| 37 | +</Example> |
| 38 | + |
| 39 | +== HTML |
| 40 | + |
| 41 | +```html |
| 42 | +<p>页面加载于: <strong zui-create="timeago" zui-create-timeago="{time: Date.now()}"></strong></p> |
| 43 | +<p>功能开发于: <strong zui-create="timeago" zui-create-timeago="{time: '2026-1-22 10:00:00'}"></strong></p> |
| 44 | +<p>不合法的时间: <strong zui-create="timeago" zui-create-timeago="{time: '2026-1x22 10:00:00', hint: '2026-1-22 10:00:00'}"></strong></p> |
| 45 | +``` |
| 46 | + |
| 47 | +::: |
| 48 | + |
| 49 | +除了支持之前的时间,还支持指定未来的时间,例如:1天后、1小时后、1分钟后、1秒后等。 |
| 50 | + |
| 51 | +::: tabs |
| 52 | + |
| 53 | +== 示例 |
| 54 | + |
| 55 | +<Example> |
| 56 | +<p>3 小时后: <strong zui-create="timeago" zui-create-timeago="{time: Date.now() + 1000 * 60 * 60 * 3 + 100}"></strong></p> |
| 57 | +<p>一天后: <strong zui-create="timeago" zui-create-timeago="{time: Date.now() + 1000 * 60 * 60 * 25}"></strong></p> |
| 58 | +<p>一年后: <strong zui-create="timeago" zui-create-timeago="{time: Date.now() + 1000 * 60 * 60 * 25 * 365}"></strong></p> |
| 59 | +</Example> |
| 60 | + |
| 61 | +== HTML |
| 62 | + |
| 63 | +```html |
| 64 | +<p>3 小时后: <strong zui-create="timeago" zui-create-timeago="{time: Date.now() + 1000 * 60 * 60 * 3 + 100}"></strong></p> |
| 65 | +<p>一天后: <strong zui-create="timeago" zui-create-timeago="{time: Date.now() + 1000 * 60 * 60 * 25}"></strong></p> |
| 66 | +<p>一年后: <strong zui-create="timeago" zui-create-timeago="{time: Date.now() + 1000 * 60 * 60 * 25 * 365}"></strong></p> |
| 67 | +``` |
| 68 | + |
| 69 | +::: |
| 70 | + |
| 71 | +## 在 JS 中使用 |
| 72 | + |
| 73 | +在 ZAI 中提供了 `timeago` 方法,用于手动生成相对时间文本,该方法定义如下: |
| 74 | + |
| 75 | +```ts |
| 76 | +function timeago(time: DateLike, now?: DateLike, lang?: string): string; |
| 77 | +``` |
| 78 | + |
| 79 | +**参数**: |
| 80 | + |
| 81 | +* `time`:日期时间对象或者日期时间字符串或者时间戳; |
| 82 | +* `now`:当前时间对象或者日期时间字符串或者时间戳,如果不指定则使用当前时间; |
| 83 | +* `lang`:语言,支持:简体中文(`zh-cn`)、繁体中文(`zh-tw`)、英文(`en`)。 |
| 84 | + |
| 85 | +**返回值**:`string`:相对时间文本。 |
| 86 | + |
| 87 | +示例: |
| 88 | + |
| 89 | +```ts |
| 90 | +const timeagoText = zui.timeago(new Date('2026-1-22 10:00:00'), new Date(), 'zh-cn'); |
| 91 | +console.log(timeagoText); // 1天前 |
| 92 | +``` |
| 93 | + |
| 94 | +## 选项 |
| 95 | + |
| 96 | +### TimeSpan 选项 |
| 97 | + |
| 98 | +<Props> |
| 99 | +/** 日期时间对象或者日期时间字符串或者时间戳。 */ |
| 100 | +time: DateLike; |
| 101 | +/** 日期时间格式,支持[日期辅助方法 `formatDate`](/lib/helpers/helpers/date-helper.html#formatdate) 中的所有格式。 */ |
| 102 | +format?: string | DateFormatter; |
| 103 | +/** 无效日期时间对象时的显示值,如果不指定则显示原始值。 */ |
| 104 | +invalidText?: string; |
| 105 | +</Props> |
| 106 | + |
| 107 | +### TimeAgo 选项 |
| 108 | + |
| 109 | +<Props> |
| 110 | +/** 日期时间对象或者日期时间字符串或者时间戳。 */ |
| 111 | +time: DateLike; |
| 112 | +/** 当前时间对象或者日期时间字符串或者时间戳,如果不指定则使用当前时间。 */ |
| 113 | +now?: DateLike; |
| 114 | +/** 语言,支持:简体中文(`zh-cn`)、繁体中文(`zh-tw`)、英文(`en`)。 */ |
| 115 | +lang?: string; |
| 116 | +/** 无效日期时间对象时的显示值,如果不指定则显示原始值。 */ |
| 117 | +invalidText?: string; |
| 118 | +/** 鼠标悬停提示文本,如果不指定则显示相对时间文本。 */ |
| 119 | +hint?: string | boolean; |
| 120 | +/** 提示文本格式,支持[日期辅助方法 `formatDate`](/lib/helpers/helpers/date-helper.html#formatdate) 中的所有格式。 */ |
| 121 | +hintFormat?: string; |
| 122 | +</Props> |
0 commit comments