Skip to content

soonerAndLater[times]

获取对应时间最早一刻与最晚一刻

函数

函数名说明
soonerAndLaterToday今天最早一刻到最晚一刻
soonerAndLaterYesterday昨天最早一刻到最晚一刻
soonerAndLaterTomorrow明天最早一刻到最晚一刻
soonerAndLaterDay一天最早一刻到最晚一刻
soonerAndLaterHour一小时最早一刻到最晚一刻
soonerAndLaterMinute一分钟最早一刻到最晚一刻
soonerAndLaterSecond一秒钟最早一刻到最晚一刻
soonerAndLaterMonth一个月最早一刻到最晚一刻
soonerAndLaterYear一年最早一刻到最晚一刻
soonerAndLaterISOWeekYearISO周年最早一刻到最晚一刻
soonerAndLaterWeek一周最早一刻到最晚一刻
soonerAndLaterISOWeekISO周最早一刻到最晚一刻
soonerAndLaterDecade十年最早一刻到最晚一刻
soonerAndLaterQuarter一季度最早一刻到最晚一刻

用法

ts
import {
  soonerAndLaterDay,
  soonerAndLaterDecade,
  soonerAndLaterISOWeek,
  soonerAndLaterISOWeekYear,
  soonerAndLaterMinute,
  soonerAndLaterMonth,
  soonerAndLaterQuarter,
  soonerAndLaterSecond,
  soonerAndLaterToday,
  soonerAndLaterTomorrow,
  soonerAndLaterWeek,
  soonerAndLaterYear,
  soonerAndLaterYesterday,

} from 'mortise-tenon-tool';

soonerAndLaterToday('yyyy/MM/dd HH:mm:ss');
// => [今天最早一刻,今天最晚一刻]

soonerAndLaterYesterday('yyyy/MM/dd HH:mm:ss');
// => 昨天最早一刻,昨天最晚一刻]

soonerAndLaterTomorrow('yyyy/MM/dd HH:mm:ss');
// => [明天最早一刻,明天最晚一刻]

soonerAndLaterDay(['2024-12-02 12:20', '2024-12-03 13:10'], 'yyyy/MM/dd HH:mm:ss');
// => ['2024/12/02 12:00:00', '2024/12/03 13:59:59']

soonerAndLaterMinute(['2024-12-02 12:20', '2024-12-03 13:10'], 'yyyy/MM/dd HH:mm:ss');
// => ['2024/12/02 12:20:00', '2024/12/03 13:10:59']

soonerAndLaterSecond(['2024-12-02 12:20:00', '2024-12-03 13:10:40'], 'yyyy/MM/dd HH:mm:ss:SSS');
// => ['2024/12/02 12:20:00:000', '2024/12/03 13:10:40:999']

soonerAndLaterMonth(['2024-11-02 12:20:10', '2024/12/03 13:10:20'], 'yyyy/MM/dd HH:mm:ss');
// => ['2024/11/01 00:00:00', '2024/12/31 23:59:59']

soonerAndLaterYear(['2024-11-02 12:20:10', '2024-12-03 13:10:29'], 'yyyy/MM/dd HH:mm:ss');
// => ['2024/01/01 00:00:00', '2024/12/31 23:59:59']

soonerAndLaterISOWeekYear(['2024-11-02 12:20:10', '2024-12-03 13:10:29'], 'yyyy/MM/dd HH:mm:ss');
// => ['2024/01/01 00:00:00', '2024/12/29 23:59:59']

soonerAndLaterWeek(
  ['2024-11-02', '2024-12-03'],
  'yyyy/MM/dd HH:mm:ss eeee(e)',
  { weekStartsOn: 1 },
);
// => ['2024/10/28 00:00:00 Monday(1)', '2024/12/08 23:59:59 Sunday(7)']

soonerAndLaterISOWeek(
  ['2024-11-02', '2024-12-03'],
  'yyyy/MM/dd HH:mm:ss eeee(e)',
  { weekStartsOn: 1 },
);
// => ['2024/10/28 00:00:00 Monday(1)', '2024/12/08 23:59:59 Sunday(7)']

soonerAndLaterDecade(['2024-11-02 12:20:10', '2024-12-03 13:10:29'], 'yyyy/MM/dd HH:mm:ss');
// => ['2020/01/01 00:00:00', '2029/12/31 23:59:59']

soonerAndLaterQuarter(['2024-11-02 12:20:10', '2024-12-03 13:10:29'], 'yyyy/MM/dd HH:mm:ss');
// => ['2024/10/01 00:00:00', '2024/12/31 23:59:59']

日期格式

仅展示部分常用格式,在 这里 可查看更多 date-fns 支持的日期格式。

WARNING

请注意大小写

格式含义备注举例
yyyy2017
M不补 01
MM01
W不补 01
WW01
d不补 02
dd02
H小时24 小时制;不补 03
HH小时24 小时制03
h小时12 小时制;不补 03
hh小时12 小时制03
m分钟不补 04
mm分钟04
s不补 05
ss05
aAM、PMAM
aaaam、pmam
T毫秒时间戳1483326245000
t秒时间戳1483326245

特殊日期格式

部分特殊日期格式,如'Y'与'D'的含义与'y'与'd'的含义是不同的,启用的话需要额外配置options,详情请查看format

ts
import { soonerAndLaterDay } from 'mortise-tenon-tool';

soonerAndLaterDay('2024-12-03', 'D*H*m*s', { useAdditionalDayOfYearTokens: true });
// => ['338*0*0*0', '338*23*59*59']

参数类型

ts
import { FormatOptions } from 'date-fns';
/**
 * 函数的参数
 * @param_0 时间格式,默认为时间戳,注意大小写(https://date-fns.org/v4.1.0/docs/format)
 * @param_1 格式选项(https://date-fns.org/v4.1.0/docs/format#types/FormatOptions/630)
 */
type SoonerAndLaterParams = [string?, FormatOptions?];
/**
 * 获取日期的今天最早一刻到最晚一刻
 * @params 参数:(时间格式,格式选项) - {@link SoonerAndLaterParams}
 * @returns [开始时间, 结束时间]
 */
export declare function soonerAndLaterToday(...params: SoonerAndLaterParams): string[] | undefined;
/**
 * 获取日期的昨天最早一刻到最晚一刻
 * @params 参数:(时间格式,格式选项) - {@link SoonerAndLaterParams}
 * @returns [开始时间, 结束时间]
 */
export declare function soonerAndLaterYesterday(...params: SoonerAndLaterParams): string[] | undefined;
/**
 * 获取日期的明天最早一刻到最晚一刻
 * @params 参数:(时间格式,格式选项) - {@link SoonerAndLaterParams}
 * @returns [开始时间, 结束时间]
 */
export declare function soonerAndLaterTomorrow(...params: SoonerAndLaterParams): string[] | undefined;
export {};
ts
import { FormatOptions } from 'date-fns';
/** 时间类型 */
export type TimeDate = Date | string | number | null | undefined;
/** 传入的时间参数 */
export type TimeParam = TimeDate[] | TimeDate;
/**
 * 函数的参数
 * @param_0 时间
 * @param_1 时间格式,默认为时间戳,注意大小写(https://date-fns.org/v4.1.0/docs/format)
 * @param_2 时间格式选项(https://date-fns.org/v4.1.0/docs/format#types/FormatOptions/630)
 */
type SoonerAndLaterParams = [TimeParam?, string?, FormatOptions?];
/**
 * 获取日期的一天最早一刻到最晚一刻
 * @params 参数:(时间,格式,格式选项) - {@link SoonerAndLaterParams}
 * @returns [开始时间, 结束时间]
 */
export declare function soonerAndLaterDay(...params: SoonerAndLaterParams): string[] | undefined;
/**
 * 获取日期的一小时最早一刻到最晚一刻
 * @params 参数:(时间,格式,格式选项) - {@link SoonerAndLaterParams}
 * @returns [开始时间, 结束时间]
 */
export declare function soonerAndLaterHour(...params: SoonerAndLaterParams): string[] | undefined;
/**
 * 获取日期的一分钟最早一刻到最晚一刻
 * @params 参数:(时间,格式,格式选项) - {@link SoonerAndLaterParams}
 * @returns [开始时间, 结束时间]
 */
export declare function soonerAndLaterMinute(...params: SoonerAndLaterParams): string[] | undefined;
/**
 * 获取日期的一秒钟最早一刻到最晚一刻
 * @params 参数:(时间,格式,格式选项) - {@link SoonerAndLaterParams}
 * @returns [开始时间, 结束时间]
 */
export declare function soonerAndLaterSecond(...params: SoonerAndLaterParams): string[] | undefined;
/**
 * 获取日期的一个月最早一刻到最晚一刻
 * @params 参数:(时间,格式,格式选项) - {@link SoonerAndLaterParams}
 * @returns [开始时间, 结束时间]
 */
export declare function soonerAndLaterMonth(...params: SoonerAndLaterParams): string[] | undefined;
/**
 * 获取对应的一年最早一刻到最晚一刻
 * @params 参数:(时间,格式,格式选项) - {@link SoonerAndLaterParams}
 * @returns [开始时间, 结束时间]
 */
export declare function soonerAndLaterYear(...params: SoonerAndLaterParams): string[] | undefined;
/**
 * 获取对应的一ISO周年最早一刻到最晚一刻
 * @see ISO: https://zh.wikipedia.org/wiki/ISO%E9%80%B1%E6%97%A5%E6%9B%86
 * @params 参数:(时间,格式,格式选项) - {@link SoonerAndLaterParams}
 * @returns [开始时间, 结束时间]
 */
export declare function soonerAndLaterISOWeekYear(...params: SoonerAndLaterParams): string[] | undefined;
/**
 * 获取对应的一周最早一刻到最晚一刻
 * @params 参数:(时间,格式,格式选项) - {@link SoonerAndLaterParams}
 * @returns [开始时间, 结束时间]
 */
export declare function soonerAndLaterWeek(...params: SoonerAndLaterParams): string[] | undefined;
/**
 * 获取对应的一ISO周最早一刻到最晚一刻
 * @see ISO: https://zh.wikipedia.org/wiki/ISO%E9%80%B1%E6%97%A5%E6%9B%86
 * @params 参数:(时间,格式,格式选项) - {@link SoonerAndLaterParams}
 * @returns [开始时间, 结束时间]
 */
export declare function soonerAndLaterISOWeek(...params: SoonerAndLaterParams): string[] | undefined;
/**
 * 获取对应的十年最早一刻到最晚一刻
 * @params 参数:(时间,格式,格式选项) - {@link SoonerAndLaterParams}
 * @returns [开始时间, 结束时间]
 */
export declare function soonerAndLaterDecade(...params: SoonerAndLaterParams): string[] | undefined;
/**
 * 获取对应的季度最早一刻到最晚一刻
 * @params 参数:(时间,格式,格式选项) - {@link SoonerAndLaterParams}
 * @returns [开始时间, 结束时间]
 */
export declare function soonerAndLaterQuarter(...params: SoonerAndLaterParams): string[] | undefined;
export {};

源码

源代码