-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathNcDateTime.vue
More file actions
146 lines (123 loc) Ā· 3.49 KB
/
NcDateTime.vue
File metadata and controls
146 lines (123 loc) Ā· 3.49 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!--
- SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<docs>
### General description
This components purpose is to display a timestamp in the users local time format.
It also supports relative time, for examples *6 seconds ago*.
#### Standard usage
Without any optional parameters the timestamp is displayed as a relative datetime and a title with the full date is added.
```vue
<template>
<NcDateTime :timestamp="timestamp" />
</template>
<script>
export default {
data() {
return {
timestamp: Date.now(),
}
},
}
</script>
```
#### Ignore seconds
If you do not want the seconds to be counted up until minutes are reached you can simply use the `ignore-seconds` property.
```vue
<template>
<NcDateTime :timestamp="timestamp" :ignore-seconds="true" />
</template>
<script>
export default {
data() {
return {
timestamp: Date.now(),
}
},
}
</script>
```
#### Custom date or time format
The component allows to format the full date for the title by settings the `format` property.
It is also possible to disable relative time by setting the `relativeTime` property to `false`.
```vue
<template>
<div>
<h4>Short relative time</h4>
<NcDateTime :timestamp="timestamp" relative-time="short" />
<h4>Custom title format</h4>
<NcDateTime :timestamp="timestamp" :format="timeFormat" />
<h4>Without relative time</h4>
<NcDateTime :timestamp="timestamp" :format="timeFormat" :relative-time="false" />
</div>
</template>
<script>
export default {
data() {
return {
timestamp: Date.now(),
/** For allowed formats see the Intl.DateTimeFormat options */
timeFormat: {
dateStyle: 'short',
timeStyle: 'full'
},
}
},
}
</script>
<style>
h4 {
font-weight: var(--font-weight-heading, bold);
margin-top: 12px;
}
</style>
```
</docs>
<template>
<span
class="nc-datetime"
dir="auto"
:data-timestamp="timestamp"
:title
v-text="formattedTime" />
</template>
<script setup lang="ts">
import { computed, toRef } from 'vue'
import { useFormatRelativeTime, useFormatTime } from '../../composables/useFormatDateTime/index.ts'
const props = withDefaults(defineProps<{
/**
* The timestamp to display, either an unix timestamp (in milliseconds) or a Date object
*/
timestamp: Date | number
/**
* The format used for displaying, or if relative time is used the format used for the title (optional)
*/
format?: Intl.DateTimeFormatOptions
/**
* Wether to display the timestamp as time from now (optional)
*
* - `false`: Disable relative time
* - `'long'`: Long text, like *2 seconds ago* (default)
* - `'short'`: Short text, like *2 sec. ago*
* - `'narrow'`: Even shorter text (same as `'short'` on some languages)
*/
relativeTime?: false | 'long' | 'short' | 'narrow'
/**
* Ignore seconds when displaying the relative time and just show `a few seconds ago`
*/
ignoreSeconds?: boolean
}>(), {
format: () => ({ timeStyle: 'medium', dateStyle: 'short' }),
relativeTime: 'long',
})
const timeOptions = computed(() => ({ format: props.format }))
const relativeTimeOptions = computed(() => ({
ignoreSeconds: props.ignoreSeconds,
relativeTime: props.relativeTime || 'long',
update: props.relativeTime !== false,
}))
const title = useFormatTime(toRef(() => props.timestamp), timeOptions)
const relativeTime = useFormatRelativeTime(toRef(() => props.timestamp), relativeTimeOptions)
const formattedTime = computed(() => props.relativeTime ? relativeTime.value : title.value)
</script>