Skip to content

Date

Basic

ts
const date = new Date('2023-01-01T00:00:00.000Z')

const year = date.getFullYear()
const month = date.getMonth()
const day = date.getDate()

// 2023 0 1
console.log(year, month, day)

YYYY-MM

ts
const date = new Date('2023-01-01T00:00:00.000Z')

// 2023-01
function makeYyyyMm(date: Date) {
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}`
}

console.log(makeYyyyMm(date))

翌月1日0時を表す日時を生成

ts
const date = new Date('2023-01-01T00:00:00.000Z')

export function nextMonthlyResetDate(date: Date): Date {
  // date.getFullYear() 元の年
  // date.getMonth() 翌月(0始まりなので+1)
  // 1 日付 → 1日
  // 0, 0, 0, 0 時・分・秒・ミリ秒 → すべて0
  return new Date(date.getFullYear(), date.getMonth() + 1, 1, 0, 0, 0, 0)
}

// 2023-02-01T00:00:00.000Z
console.log(nextMonthlyResetDate(date))

トライアル終了日時

ts
const date = new Date('2023-01-01T00:00:00.000Z')

const FREE_TRIAL_DAYS = 7

export function trialEndsAt(emailVerifiedDate: Date): Date {
  return new Date(emailVerifiedDate.getTime() + FREE_TRIAL_DAYS * 24 * 60 * 60 * 1000)
}

// 2023-01-08T00:00:00.000Z
console.log(trialEndsAt(date))