Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x 1x 1x 1x 1x 1x 1x 1x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 5x 1x 5x 1x 4x 1x 1x 5x 34x 34x 34x 12x 12x 12x 34x 34x 34x 34x 34x 34x 34x 34x 34x 2x 2x 34x 2x 2x 32x 12x 12x 2x 2x 30x 7x 7x 2x 2x 18x 6x 6x 1x 1x 11x 4x 4x 34x 10x 10x 34x 20x 20x 34x 34x | import { addTime } from './add-time.ts'; import { secondsPerDay, secondsPerHour, secondsPerMinute, ticksPerSecond } from './date.ts'; import { floor } from './floor.ts'; import { formatDate } from './format-date.ts'; import { isSameDay } from './is-same-day.ts'; import { plural } from './plural.ts'; import { space } from './unicode.ts'; /** * Options for the {@link relativeTime} function * @group Time * @category Relative Time */ export type RelativeTimeOptions = { /** Describe the time difference as a time on a nearby day */ todayTomorrowYesterday?: boolean; /** Passed to {@link formatDate} to display a time */ timeFormat?: string; /** Passed to {@link formatDate} to display a year, month and day */ ymdFormat?: string; /** Passed to {@link formatDate} to display a month and day */ mdFormat?: string; }; /** * Describe the difference between two dates in a simple format * @param input - The date * @param relativeTo - The date to compare to * @param options - see {@link RelativeTimeOptions} * @returns string describing the time difference between the two dates * @group Time * @category Relative Time */ export function relativeTime( input: Date, relativeTo: Date, { todayTomorrowYesterday = false, timeFormat = 'H:mm TT', ymdFormat = 'MMMM D YYYY', mdFormat = 'MMMM D', }: RelativeTimeOptions = {}, ): string { const text = [] as string[]; if (todayTomorrowYesterday) { if (isSameDay(input, relativeTo)) { text.push(`today ${formatDate(input, timeFormat)} -`); } else if (isSameDay(input, addTime(relativeTo, { days: 1 }))) { text.push(`tomorrow ${formatDate(input, timeFormat)} -`); } else if (isSameDay(input, addTime(relativeTo, { days: -1 }))) { text.push(`yesterday ${formatDate(input, timeFormat)} -`); } } let diff = (input.getTime() - relativeTo.getTime()) / ticksPerSecond; let sign = 1; if (diff < 0) { sign = -1; diff = Math.abs(diff); } const d = floor(diff / secondsPerDay, { tolerance: 0.05 }); const h = floor((diff - d * secondsPerDay) / secondsPerHour, { tolerance: 0.05 }); const m = floor((diff - d * secondsPerDay - h * secondsPerHour) / secondsPerMinute, { tolerance: 0.05, }); const s = floor(diff - d * secondsPerDay - h * secondsPerHour - m * secondsPerMinute, { tolerance: 0.05, }); if (d > 90) { text.push(formatDate(input, ymdFormat)); sign = 0; } else if (d > 30) { text.push(formatDate(input, mdFormat)); sign = 0; } else if (d > 0) { text.push(plural('day', d, true)); if (d < 4 && h > 1) { text.push(plural('hour', h, true)); } } else if (h > 0) { text.push(plural('hour', h, true)); if (h < 4 && m > 0) { text.push(plural('minute', m, true)); } } else if (m > 0) { text.push(plural('minute', m, true)); if (m < 4 && s > 0) { text.push(plural('second', s, true)); } } else if (s > 0) { text.push(plural('second', s, true)); } if (sign === -1) { text.push('ago'); } if (sign === 1) { text.push(d || h || m || s ? 'from now' : 'now'); } return text.join(space); } |