@pengzhanbo/utils - v3.12.0
    Preparing search index...

    Function isSameDay

    • Check if two dates fall on the same calendar day in the local time zone (time components are ignored).

      检查两个日期是否在本地时区的同一天(忽略时间部分)。

      Parameters

      • date1: string | number | Date

        The first date to compare. 第一个要比较的日期

      • Optionaldate2: string | number | Date

        The second date to compare. 第二个要比较的日期

      Returns boolean

      True if both dates are on the same local calendar day, false otherwise. 如果两个日期在本地时区的同一天则返回 true,否则返回 false

      The comparison is based on the local calendar day (Date.prototype.toDateString), not on the UTC day: the question this function answers is "are these two instants the same day for the user?". The time zone therefore changes the result — 2024-01-14T16:00:00Z and 2024-01-15T15:59:00Z are the same day in UTC+8 but different days in UTC.

      String inputs are converted with new Date(value), which parses a date-only string such as '2024-01-15' as UTC midnight; in time zones west of UTC that instant belongs to the previous local day. Pass a Date object or a timestamp when the local calendar day matters.

      比较基于本地日历日(Date.prototype.toDateString),而非 UTC 日:本函数要回答的是"这两个时刻 对用户而言是不是同一天",因此结果随时区变化——2024-01-14T16:00:00Z 与 2024-01-15T15:59:00Z 在 UTC+8 属于同一天,在 UTC 则不是。

      字符串入参通过 new Date(value) 解析,'2024-01-15' 这类纯日期字符串会被当作 UTC 午夜, 在 UTC 以西的时区会落到前一个本地日。若关心本地日历日,请传入 Date 对象或时间戳。

      // Date / 日期对象
      isSameDay(new Date(2024, 0, 15, 10, 30), new Date(2024, 0, 15, 23, 59)) // => true
      isSameDay(new Date(2024, 0, 15), new Date(2024, 0, 16)) // => false

      // number - timestamp in milliseconds / 毫秒时间戳
      const noon = new Date(2024, 0, 15, 12).getTime()
      isSameDay(noon, noon + 60 * 60 * 1000) // => true
      isSameDay(noon, noon + 24 * 60 * 60 * 1000) // => false

      // string - ISO 8601 / ISO 字符串
      // date-only strings parse as UTC midnight, see above / 纯日期字符串按 UTC 午夜解析,见 @remarks
      isSameDay('2024-01-15', '2024-01-15T00:00:00Z') // => true
      isSameDay('2024-01-15', '2024-01-16') // => false