2022-09-12 22:47:27 +00:00
|
|
|
<template>
|
|
|
|
{{ value }}
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2022-12-10 05:57:57 +00:00
|
|
|
type DateTimeFormat = "relative" | "long" | "short" | "human";
|
|
|
|
|
|
|
|
function ordinalIndicator(num: number) {
|
|
|
|
if (num > 3 && num < 21) return "th";
|
|
|
|
switch (num % 10) {
|
|
|
|
case 1:
|
|
|
|
return "st";
|
|
|
|
case 2:
|
|
|
|
return "nd";
|
|
|
|
case 3:
|
|
|
|
return "rd";
|
|
|
|
default:
|
|
|
|
return "th";
|
|
|
|
}
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-10 05:57:57 +00:00
|
|
|
const months = [
|
|
|
|
"January",
|
|
|
|
"February",
|
|
|
|
"March",
|
|
|
|
"April",
|
|
|
|
"May",
|
|
|
|
"June",
|
|
|
|
"July",
|
|
|
|
"August",
|
|
|
|
"September",
|
|
|
|
"October",
|
|
|
|
"November",
|
|
|
|
"December",
|
|
|
|
];
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
const value = computed(() => {
|
|
|
|
if (!props.date) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const dt = typeof props.date === "string" ? new Date(props.date) : props.date;
|
|
|
|
if (!dt) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:03:24 +00:00
|
|
|
if (!validDate(dt)) {
|
2022-09-12 22:47:27 +00:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (props.format) {
|
2022-12-10 05:57:57 +00:00
|
|
|
case "relative":
|
2022-09-12 22:47:27 +00:00
|
|
|
return useTimeAgo(dt).value + useDateFormat(dt, " (MM-DD-YYYY)").value;
|
2022-12-10 05:57:57 +00:00
|
|
|
case "long":
|
2022-11-01 02:43:30 +00:00
|
|
|
return useDateFormat(dt, "MM-DD-YYYY (dddd)").value;
|
2022-12-10 05:57:57 +00:00
|
|
|
case "short":
|
2022-11-01 02:43:30 +00:00
|
|
|
return useDateFormat(dt, "MM-DD-YYYY").value;
|
2022-12-10 05:57:57 +00:00
|
|
|
case "human":
|
|
|
|
// January 1st, 2021
|
|
|
|
return `${months[dt.getMonth()]} ${dt.getDate()}${ordinalIndicator(dt.getDate())}, ${dt.getFullYear()}`;
|
2022-09-12 22:47:27 +00:00
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
|
|
date: {
|
|
|
|
type: [Date, String],
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
format: {
|
|
|
|
type: String as () => DateTimeFormat,
|
|
|
|
default: "relative",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
</script>
|