support datetime

This commit is contained in:
Timi
2026-10-12 16:31:23 +08:00
parent 5e4b7404d8
commit 4910a277b8
2 changed files with 15 additions and 7 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
# PassTimeLabel 时间标签 # PassTimeLabel 时间标签
把时间戳显示为「多久以前」,点击可切换为完整日期时间。 接收时间戳或 `yyyy-MM-dd HH:mm:ss`、`yyyy-MM-ddTHH:mm:ss` 格式的日期时间字符串,显示为「多久以前」,点击可切换为完整日期时间。
## 基础用法 ## 基础用法
+14 -6
View File
@@ -1,6 +1,6 @@
<template> <template>
<span <span
v-if="timestamp" v-if="currentTimestamp"
class="tui-passtime-label" class="tui-passtime-label"
:class="{ underline }" :class="{ underline }"
role="button" role="button"
@@ -20,6 +20,7 @@ defineOptions({
const props = withDefaults(defineProps<{ const props = withDefaults(defineProps<{
timestamp?: number; timestamp?: number;
datetime?: string;
onlyTime?: boolean; onlyTime?: boolean;
onlyDate?: boolean; onlyDate?: boolean;
underline?: boolean; underline?: boolean;
@@ -29,20 +30,27 @@ const props = withDefaults(defineProps<{
underline: false underline: false
}); });
const { timestamp, onlyTime, onlyDate } = toRefs(props); const { timestamp, datetime, onlyTime, onlyDate } = toRefs(props);
const showingDetail = ref(false); const showingDetail = ref(false);
const currentTimestamp = computed(() => {
if (!datetime.value) {
return timestamp.value;
}
return new Date(datetime.value.replace(" ", "T")).getTime();
});
const content = computed(() => { const content = computed(() => {
if (!showingDetail.value) { if (!showingDetail.value) {
return Time.toPassedDateTime(timestamp.value); return Time.toPassedDateTime(currentTimestamp.value);
} }
if (onlyTime.value) { if (onlyTime.value) {
return Time.toTime(timestamp.value); return Time.toTime(currentTimestamp.value);
} }
if (onlyDate.value) { if (onlyDate.value) {
return Time.toDate(timestamp.value); return Time.toDate(currentTimestamp.value);
} }
return Time.toDateTime(timestamp.value); return Time.toDateTime(currentTimestamp.value);
}); });
function toggle(event: MouseEvent | KeyboardEvent): void { function toggle(event: MouseEvent | KeyboardEvent): void {