|
| 1 | +<!-- |
| 2 | + - SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 3 | + - SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | +--> |
| 5 | + |
| 6 | +This composable allows to format a timestamp or `Date` object either as human readable, localized, string or as relative time. |
| 7 | + |
| 8 | +## Definition |
| 9 | + |
| 10 | +```ts static |
| 11 | +/** |
| 12 | + * Format a timestamp or date object as relative time. |
| 13 | + * |
| 14 | + * This is a composable wrapper around `formatRelativeTime` from `@nextcloud/l10n`. |
| 15 | + * |
| 16 | + * @param timestamp - The timestamp to format |
| 17 | + * @param opts - Formatting options |
| 18 | + */ |
| 19 | +declare function useFormatRelativeTime(timestamp: MaybeRefOrGetter<Date | number> = Date.now(), opts: MaybeRefOrGetter<FormatRelativeTimeOptions> = {}): Readonly<Ref<string>> |
| 20 | + |
| 21 | +/** |
| 22 | + * Format a given timestamp or date object as a human readable string. |
| 23 | + * |
| 24 | + * @param timestamp - Timestamp or date object to format |
| 25 | + * @param opts - Formatting options |
| 26 | + */ |
| 27 | +declare function useFormatTime(timestamp: MaybeRefOrGetter<number | Date>, opts: MaybeRefOrGetter<FormatTimeOptions>): Readonly<Ref<string>> |
| 28 | + |
| 29 | +interface FormatRelativeTimeOptions { |
| 30 | + /** |
| 31 | + * If set and the time is only a couple of seconds then instead of the number of seconds "a few seconds ago" will be shown |
| 32 | + * |
| 33 | + * @default false |
| 34 | + */ |
| 35 | + ignoreSeconds?: boolean |
| 36 | + |
| 37 | + /** |
| 38 | + * Language to use for formatting. |
| 39 | + * |
| 40 | + * @default The current users language |
| 41 | + */ |
| 42 | + language?: string |
| 43 | + |
| 44 | + /** |
| 45 | + * The relative time formatting option to use. |
| 46 | + * |
| 47 | + * @default 'long |
| 48 | + */ |
| 49 | + relativeTime?: 'long' | 'short' | 'narrow' |
| 50 | + |
| 51 | + /** |
| 52 | + * If set to false the relative time will not be updated anymore. |
| 53 | + * |
| 54 | + * @default true - Meaning the relative time will be updated if needed |
| 55 | + */ |
| 56 | + update?: boolean |
| 57 | +} |
| 58 | + |
| 59 | +interface FormatTimeOptions { |
| 60 | + /** |
| 61 | + * Locale to use for formatting. |
| 62 | + * |
| 63 | + * @default The current users locale |
| 64 | + */ |
| 65 | + locale?: string |
| 66 | + |
| 67 | + /** |
| 68 | + * The format used for displaying. |
| 69 | + * |
| 70 | + * @default { timeStyle: 'medium', dateStyle: 'short' } |
| 71 | + */ |
| 72 | + format?: Intl.DateTimeFormatOptions |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | +## Usage |
| 78 | +```js static |
| 79 | +import { |
| 80 | + useFormatRelativeTime, |
| 81 | + useFormatTime, |
| 82 | +} from '@nextcloud/vue/composables/useFormatDateTime' |
| 83 | + |
| 84 | +const timestamp = Date.now() |
| 85 | +const date = new Date('2025-07-03T19:00:00Z') |
| 86 | + |
| 87 | +const relativeTime = useFormatRelativeTime(timestamp, relativeTimeOptions) |
| 88 | +const formattedTime = useFormatTime(date, timeOptions) |
| 89 | +``` |
| 90 | + |
| 91 | +### Example |
| 92 | + |
| 93 | +```vue |
| 94 | +<template> |
| 95 | + <div class="container"> |
| 96 | + <p class="description">Pick a time to show it formatted</p> |
| 97 | + <NcDateTimePicker v-model="date" confirm type="datetime" /> |
| 98 | + <br /> |
| 99 | + <p>Relative time: {{ relativeTime }}</p> |
| 100 | + <p>Formatted time: {{ formattedTime }}</p> |
| 101 | + </div> |
| 102 | +</template> |
| 103 | +
|
| 104 | +<script> |
| 105 | + import { ref } from 'vue' |
| 106 | + import { |
| 107 | + useFormatRelativeTime, |
| 108 | + useFormatTime, |
| 109 | + } from '../../src/composables/useFormatDateTime/index.js' |
| 110 | +
|
| 111 | + export default { |
| 112 | + setup() { |
| 113 | + const date = ref(new Date()) |
| 114 | +
|
| 115 | + const relativeTime = useFormatRelativeTime(date) |
| 116 | + const formattedTime = useFormatTime(date) |
| 117 | +
|
| 118 | + return { |
| 119 | + date, |
| 120 | + formattedTime, |
| 121 | + relativeTime, |
| 122 | + } |
| 123 | + }, |
| 124 | + } |
| 125 | +</script> |
| 126 | +
|
| 127 | +<style scoped> |
| 128 | +.description { |
| 129 | + margin-block: 10px; |
| 130 | +} |
| 131 | +</style> |
| 132 | +``` |
0 commit comments