0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScript(TypeScript)で特定の時間内であるかどうか判定する処理を作ってみた

Posted at

開始時刻と終了時刻を定義し、現在時刻がその間に存在するかどうか確かめるプログラムを思いつきで書いてみたので参考になればと思い載せます。
日付を跨ぐ場合も考慮しました。

const confirmInTime = ({
  startHours,
  startMinutes,
  endHours,
  endMinutes,
  h,
  m,
}: {
  startHours: number;
  startMinutes: number;
  endHours: number;
  endMinutes: number;
  h: number; // 今の時間
  m: number; // 今の分数
}) => {
  if (startHours < endHours) {
    if (startHours === h) {
      return startMinutes <= m;
    }
    if (endHours === h) {
      return m <= endMinutes;
    }
    return startHours < h && h < endHours;
  } else if (startHours === endHours) {
    if (startMinutes === endMinutes) {
      return startHours === h && startMinutes === m;
    } else {
      return startHours === h && startMinutes <= m && m <= endMinutes;
    }
  } else {
    if (startHours === h) {
      return startMinutes <= m;
    }
    if (endHours === h) {
      return m <= endMinutes;
    }

    return h > startHours || h < endHours;
  }
};

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?