Files
hossein-por-shop/frontend/composables/global/usePersianTimeAgo.ts
T
2025-02-28 23:12:23 +03:30

26 lines
639 B
TypeScript

// composables/usePersianTimeAgo.ts
import { ref, onMounted, onUnmounted } from "vue";
import { formatDistance, toDate } from "date-fns-jalali";
import { faIR } from "date-fns-jalali/locale";
export function usePersianTimeAgo(date: Date) {
const timeAgo = ref("");
const updateTimeAgo = () => {
timeAgo.value = formatDistance(toDate(date), new Date(), {
addSuffix: true,
locale: faIR,
});
};
onMounted(() => {
updateTimeAgo();
const interval = setInterval(updateTimeAgo, 60000);
onUnmounted(() => clearInterval(interval));
});
return timeAgo;
}