soonerAndLater[times]
获取对应时间最早一刻与最晚一刻
函数
函数名 | 说明 |
---|---|
soonerAndLaterToday | 今天最早一刻到最晚一刻 |
soonerAndLaterYesterday | 昨天最早一刻到最晚一刻 |
soonerAndLaterTomorrow | 明天最早一刻到最晚一刻 |
soonerAndLaterDay | 一天最早一刻到最晚一刻 |
soonerAndLaterHour | 一小时最早一刻到最晚一刻 |
soonerAndLaterMinute | 一分钟最早一刻到最晚一刻 |
soonerAndLaterSecond | 一秒钟最早一刻到最晚一刻 |
soonerAndLaterMonth | 一个月最早一刻到最晚一刻 |
soonerAndLaterYear | 一年最早一刻到最晚一刻 |
soonerAndLaterISOWeekYear | 一ISO周年最早一刻到最晚一刻 |
soonerAndLaterWeek | 一周最早一刻到最晚一刻 |
soonerAndLaterISOWeek | 一ISO周最早一刻到最晚一刻 |
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
请注意大小写
格式 | 含义 | 备注 | 举例 |
---|---|---|---|
yyyy | 年 | 2017 | |
M | 月 | 不补 0 | 1 |
MM | 月 | 01 | |
W | 周 | 不补 0 | 1 |
WW | 周 | 01 | |
d | 日 | 不补 0 | 2 |
dd | 日 | 02 | |
H | 小时 | 24 小时制;不补 0 | 3 |
HH | 小时 | 24 小时制 | 03 |
h | 小时 | 12 小时制;不补 0 | 3 |
hh | 小时 | 12 小时制 | 03 |
m | 分钟 | 不补 0 | 4 |
mm | 分钟 | 04 | |
s | 秒 | 不补 0 | 5 |
ss | 秒 | 05 | |
a | AM、PM | AM | |
aaa | am、pm | am | |
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 {};