Skip to content

Home > sculp-js > dateToStart

dateToStart() function ​

将日期转换为指定粒度的起始时间,粒度小于等于天时低于粒度的单位全部归零, 粒度大于等于小时时毫秒一并归零。默认按「日」重置(00:00:00.000),与原行为一致。

Signature:

typescript
export declare function dateToStart(value: DateValue, unit?: DateUnit): Date;

Parameters ​

Parameter

Type

Description

value

DateValue

时间戳、字符串或 Date 对象

unit

DateUnit

(Optional) 重置粒度,默认 'day'

Returns:

Date

{Date} 该粒度起始时间的 Date 对象

Example ​

ts
dateToStart('2024-06-15 18:30:45.123'); // => 2024-06-15 00:00:00.000(默认,按日)
dateToStart('2024-06-15 18:30:45.123', 'year'); // => 2024-01-01 00:00:00.000
dateToStart('2024-06-15 18:30:45.123', 'month'); // => 2024-06-01 00:00:00.000
dateToStart('2024-06-15 18:30:45.123', 'hour'); // => 2024-06-15 18:00:00.000
dateToStart('2024-06-15 18:30:45.123', 'minute'); // => 2024-06-15 18:30:00.000
dateToStart('2024-06-15 18:30:45.123', 'second'); // => 2024-06-15 18:30:45.000

Released under the MIT License.