blob: 0bed4cf060c60ae31ba45a19e8b3f2572262310f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
<script setup>
import { ref, computed, onMounted } from "vue"
import {datetime} from "../../assets/javascripts/lib/datetime";
const props = defineProps({
timestamp: {
type: Number,
default: 0
},
iso: {
type: String,
default: null
},
relative: {
type: Boolean,
default: false
},
date_only: {
type: Boolean,
default: false
}
})
const now = ref(Date.now())
const date = computed(() => {
if (Number.isInteger(props.timestamp) && props.timestamp !== 0) {
return new Date(props.timestamp * 1000)
} else if (props.iso) {
const parsed = new Date(props.iso)
return isNaN(parsed.getTime()) ? null : parsed
}
return null
})
const current_datetime = computed(() => (date.value ? date.value.toISOString() : ''))
const displayRelative = () => {
if (!date.value || !props.relative) {
return false
}
return now.value - date.value.getTime() < 12 * 60 * 60 * 1000
}
const title = computed(() => (displayRelative() ? formattedDate(true) : null))
const formattedDate = (forceAbsolute = false) => {
if (!date.value) {
return 'Invalid date'
}
const relativeValue = !forceAbsolute && props.relative && displayRelative()
return datetime.getStudipDate(date.value, relativeValue, props.date_only)
}
onMounted(() => {
window.setInterval(() => {
now.value = Date.now()
}, 1000)
})
</script>
<template>
<time :datetime="current_datetime" v-if="date" :title="title">
{{ formattedDate() }}
</time>
</template>
|