All files / src/utils date.ts

91.95% Statements 80/87
86.66% Branches 52/60
100% Functions 8/8
91.95% Lines 80/87

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126  1x     1x   1x 3x 3x 3x 3x 3x   1x 10x 10x 10x 10x   6x 10x 4x 4x 4x 4x 4x 4x   2x 10x 10x         1x 12x   11x 9x 3x 9x 2x 2x   1x 1x 1x 1x               1x 5x   5x 3x 2x 3x 1x 1x   1x 1x 1x 1x               1x 5x   5x 3x 2x 3x 1x 1x   1x 1x 1x 1x                   1x 6x 6x 6x 6x 5x     6x   4x 4x 4x   4x 6x 2x 6x 1x 6x 1x 6x   6x 6x  
// YYYY-MM-DD形式かどうか
export const isYmd = (s: string) => /^\d{4}-\d{2}-\d{2}$/.test(s)
 
// YYYY-MM-DDを文字列比較で前後判定(形式が正しい前提)
export const isBeforeYmd = (a: string, b: string) => a < b
 
const toLocalYmd = (date: Date): string => {
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  return `${year}-${month}-${day}`
}
 
export const parsePickerDate = (
  value: string | Date | null
): Date | undefined => {
  if (!value) return undefined
  if (value instanceof Date) return Number.isNaN(value.getTime()) ? undefined : value
 
  const ymdMatch = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value)
  if (ymdMatch) {
    const year = Number(ymdMatch[1])
    const month = Number(ymdMatch[2]) - 1
    const day = Number(ymdMatch[3])
    const date = new Date(year, month, day)
    return Number.isNaN(date.getTime()) ? undefined : date
  }
 
  const date = new Date(value)
  return Number.isNaN(date.getTime()) ? undefined : date
}
 
/**
 * Date, string, null を受け取って 'YYYY-MM-DD' に正規化
 */
export const normalizeToDateString = (v: unknown): string | null => {
  if (!v) return null
 
  if (typeof v === 'string') {
    if (/^\d{4}-\d{2}-\d{2}$/.test(v)) return v
    const d = new Date(v)
    if (Number.isNaN(d.getTime())) return null
    return toLocalYmd(d)
  }
 
  if (v instanceof Date) {
    if (Number.isNaN(v.getTime())) return null
    return toLocalYmd(v)
  }
 
  return null
}
 
/**
 * Date, string, null を受け取って 'HH:mm' に正規化
 */
export const normalizeToTimeString = (v: unknown): string | null => {
  if (!v) return null
 
  if (typeof v === 'string') {
    if (/^\d{2}:\d{2}$/.test(v)) return v
    const d = new Date(v)
    if (Number.isNaN(d.getTime())) return null
    return d.toISOString().slice(11, 16)
  }
 
  if (v instanceof Date) {
    if (Number.isNaN(v.getTime())) return null
    return v.toISOString().slice(11, 16)
  }
 
  return null
}
 
/**
 * Date, string, null を受け取って 'YYYY-MM-DDTHH:mm' に正規化
 */
export const normalizeToDateTimeString = (v: unknown): string | null => {
  if (!v) return null
 
  if (typeof v === 'string') {
    if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/.test(v)) return v
    const d = new Date(v)
    if (Number.isNaN(d.getTime())) return null
    return d.toISOString().slice(0, 16)
  }
 
  if (v instanceof Date) {
    if (Number.isNaN(v.getTime())) return null
    return v.toISOString().slice(0, 16)
  }
 
  return null
}
 
/**
 * 日付文字列を指定されたフォーマットで整形する
 * @param dateStr 日付文字列
 * @param format 出力フォーマット (デフォルト: yyyy-mm)
 */
export const formatDate = (
  dateStr: string | undefined,
  format: 'yyyy-mm' | 'mm-dd' | 'yyyy-mm-dd' = 'yyyy-mm',
) => {
  if (!dateStr) return ''
  const date = new Date(dateStr)
 
  // 無効な日付の場合は空文字を返す
  if (isNaN(date.getTime())) return ''
 
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
 
  switch (format) {
    case 'yyyy-mm':
      return `${year}-${month}`
    case 'mm-dd':
      return `${month}-${day}`
    case 'yyyy-mm-dd':
      return `${year}-${month}-${day}`
    default:
      return `${year}-${month}`
  }
}