From 790f39e95bcc25cc433ba278a6f7ff37d1b5930b Mon Sep 17 00:00:00 2001 From: miteruzo Date: Sat, 14 Mar 2026 18:32:19 +0900 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E3=81=A5=E3=81=91=E4=B8=8D=E8=A9=B3?= =?UTF-8?q?=E3=81=AE=E8=A1=A8=E7=A4=BA=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/lib/utils.ts | 65 +++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/frontend/src/lib/utils.ts b/frontend/src/lib/utils.ts index 95caded..0207b5f 100644 --- a/frontend/src/lib/utils.ts +++ b/frontend/src/lib/utils.ts @@ -10,17 +10,64 @@ export const toDate = (d: string | Date): Date => typeof d === 'string' ? new Da export const cn = (...inputs: ClassValue[]) => twMerge (clsx (...inputs)) -export const dateString = (d: string | Date): string => - toDate (d).toLocaleString ('ja-JP-u-ca-japanese') +export const dateString = ( + d: string | Date, + unknown: 'month' | 'day' | 'hour' | 'minute' | 'second' | null = null, +): string => + toDate (d).toLocaleString ( + 'ja-JP-u-ca-japanese', + { era: 'long', + year: 'numeric', + month: (unknown === 'month' ? undefined : 'long'), + day: unknown != null && ['month', 'day'].includes (unknown) ? undefined : 'numeric', + weekday: unknown != null && ['month', 'day'].includes (unknown) ? undefined : 'short', + hour: unknown == null || ['second', 'minute'].includes (unknown) ? 'numeric' : undefined, + minute: unknown == null || unknown === 'second' ? 'numeric' : undefined, + second: unknown == null ? 'numeric' : undefined }) -// TODO: 表示形式きしょすぎるので何とかする export const originalCreatedAtString = ( f: string | Date | null, b: string | Date | null, -): string => - ([f ? `${ dateString (f) } 以降` : '', - b ? `${ dateString (b) } より前` : ''] - .filter (Boolean) - .join (' ')) - || '不明' +): string => { + const from = f ? toDate (f) : null + const before = b ? toDate (b) : null + + if (from && before) + { + const diff = before.getTime () - from.getTime () + + if (diff <= 60_000 /* 1 分 */) + return dateString (from, 'second') + + if (from.getMinutes () === 0 && before.getMinutes () === 0) + { + if (Math.abs (diff - 3_600_000 /* 1 時間 */) < 60_000) + return dateString (from, 'minute') + ' (分不詳)' + + if (from.getHours () === 0 && before.getHours () === 0) + { + if (Math.abs (diff - 86_400_000) < 60_000) + return dateString (from, 'hour') + ' (時刻不詳)' + + if (from.getDate () === 1 && before.getDate () === 1) + { + if (2_419_200_000 /* 28 日 */ <= diff && diff < 2_764_800_000 /* 32 日 */) + return dateString (from, 'day') + ' (日不詳)' + + if (from.getMonth () === 0 && before.getMonth () === 0 + && (31_536_000_000 /* 365 日 */ <= diff + && diff < 31_708_800_000 /* 367 日 */)) + return dateString (from, 'month') + ' (月日不詳)' + } + } + } + } + + const rtn = ([from ? `${ dateString (from, 'second') }` : '', + '~', + before ? `${ dateString (new Date (before.getTime () - 60_000), 'second') }` : ''] + .filter (Boolean) + .join (' ')) + return rtn === '~' ? '年月日不詳' : rtn +}