|
| 1 | +/*! |
| 2 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { getLanguage } from './locale.ts' |
| 7 | + |
| 8 | +export interface FormatDateOptions { |
| 9 | + /** |
| 10 | + * If set then instead of showing seconds since the timestamp show the passed message. |
| 11 | + * @default false |
| 12 | + */ |
| 13 | + ignoreSeconds?: string | false |
| 14 | + |
| 15 | + /** |
| 16 | + * The relative time formatting option to use |
| 17 | + * @default 'long |
| 18 | + */ |
| 19 | + relativeTime?: 'long' | 'short' | 'narrow' |
| 20 | + |
| 21 | + /** |
| 22 | + * Language to use |
| 23 | + * @default 'current language' |
| 24 | + */ |
| 25 | + language?: string |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Format a given time as "relative time" also called "humanizing". |
| 30 | + * |
| 31 | + * @param timestamp Timestamp or Date object |
| 32 | + * @param opts Options for the formatting |
| 33 | + */ |
| 34 | +export function formatRelativeTime( |
| 35 | + timestamp: Date|number = Date.now(), |
| 36 | + opts: FormatDateOptions = {}, |
| 37 | +): string { |
| 38 | + const options: Required<FormatDateOptions> = { |
| 39 | + ignoreSeconds: false, |
| 40 | + language: getLanguage(), |
| 41 | + relativeTime: 'long' as const, |
| 42 | + ...opts, |
| 43 | + } |
| 44 | + |
| 45 | + /** ECMA Date object of the timestamp */ |
| 46 | + const date = new Date(timestamp) |
| 47 | + |
| 48 | + const formatter = new Intl.RelativeTimeFormat([options.language, getLanguage()], { numeric: 'auto', style: options.relativeTime }) |
| 49 | + const diff = date.getTime() - Date.now() |
| 50 | + const seconds = diff / 1000 |
| 51 | + |
| 52 | + if (Math.abs(seconds) < 59.5) { |
| 53 | + return options.ignoreSeconds |
| 54 | + || formatter.format(Math.round(seconds), 'second') |
| 55 | + } |
| 56 | + |
| 57 | + const minutes = seconds / 60 |
| 58 | + if (Math.abs(minutes) <= 59) { |
| 59 | + return formatter.format(Math.round(minutes), 'minute') |
| 60 | + } |
| 61 | + const hours = minutes / 60 |
| 62 | + if (Math.abs(hours) < 23.5) { |
| 63 | + return formatter.format(Math.round(hours), 'hour') |
| 64 | + } |
| 65 | + const days = hours / 24 |
| 66 | + if (Math.abs(days) < 6.5) { |
| 67 | + return formatter.format(Math.round(days), 'day') |
| 68 | + } |
| 69 | + if (Math.abs(days) < 27.5) { |
| 70 | + const weeks = days / 7 |
| 71 | + return formatter.format(Math.round(weeks), 'week') |
| 72 | + } |
| 73 | + |
| 74 | + // For everything above we show year + month like "August 2025" or month + day if same year like "May 12" |
| 75 | + // This is based on a Nextcloud design decision: https://github.com/nextcloud/server/issues/29807#issuecomment-2431895872 |
| 76 | + const months = days / 30 |
| 77 | + const format: Intl.DateTimeFormatOptions = Math.abs(months) < 11 |
| 78 | + ? { month: options.relativeTime, day: 'numeric' } |
| 79 | + : { year: options.relativeTime === 'narrow' ? '2-digit' : 'numeric', month: options.relativeTime } |
| 80 | + |
| 81 | + const dateTimeFormatter = new Intl.DateTimeFormat([options.language, getLanguage()], format) |
| 82 | + return dateTimeFormatter.format(date) |
| 83 | +} |
0 commit comments