Gathering detailed insights and metrics for moment-ex-tool
Gathering detailed insights and metrics for moment-ex-tool
Gathering detailed insights and metrics for moment-ex-tool
Gathering detailed insights and metrics for moment-ex-tool
moment's extended tool library does not change moment's original logic, it just subtly calls moment.
npm install moment-ex-tool
Typescript
Module System
Node Version
NPM Version
TypeScript (93.79%)
HTML (5.29%)
JavaScript (0.92%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
41 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Apr 11, 2022
Latest Version
1.0.14
Package Id
moment-ex-tool@1.0.14
Unpacked Size
1.18 MB
Size
279.31 kB
File Count
84
NPM Version
8.3.0
Node Version
17.3.1
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Moment Ex Tool
封装了一些方便计算、调用mement
的方法,并没有修改moment
的逻辑。
npm:
1npm i moment-ex-tool
yarn:
1yarn add moment-ex-tool
浏览器无其他依赖:
1 2<script src="https://unpkg.com/moment-ex-tool@latest/moment-ex-tool.iife.min.js"></script>
浏览器有其他依赖:
1<script src="https://cdn.jsdelivr.net/npm/moment@latest/moment.min.js"></script> 2<script src="https://unpkg.com/moment-ex-tool@latest/moment-ex-tool.onlib.iife.min.js"></script> 3 4<script> 5 console.log(MET) 6</script>
MET
下导出了许多按命名分类的方法。
to
类将某种类型数据转换为另一种类型数据
toStr
将时间适配为'YYYY-MM-DD HH:mm:ss:SSS'
。
如果是无效参数,则会返回'Invalid date'
。
该方法旨在于快速显示可读的时间格式,如果需要自定义还是请使用moment().format()
1toStr(moment()); 2// '2022-04-14 12:04:14:414'
toMoment
解析字符串'YYYY-MM-DD HH:mm:ss:SSS'
为Moment对象
该方法旨在于快速解析toStr()
生成的字符串
1toMoment('2022-04-14 12:04:14:414');
is
类判断指定参数是否符合预期
isIn
判断t1
是否包含在t2
、t3
中。
1import type { MetType } from "moment-ex-tool"; 2 3const startTime = toMoment('2022-04-13 00:00:00:000'); 4const endTime = toMoment('2022-04-15 00:00:00:000'); 5 6const t1 = toMoment('2022-04-13 00:00:00:000'); 7isIn(t1, startTime, endTime) // 默认'[]', true 8isIn(t1, startTime, endTime, '(]'); // 大于、小于等于, false 9isIn(t1, startTime, endTime, '[)'); // 大于等于、小于, true 10 11const t2 = toMoment('2022-04-12 00:00:00:000'); 12isIn(t2, startTime, endTime); // 不在范围内, false 13 14const t3 = toMoment('2022-04-14 00:00:00:000'); 15const inType: MetType.InType = '()'; 16isIn(t3, startTime, endTime, inType); // 大于、小于, true
isPeriodIn
时间段版的isIn
。
1const startTime = toMoment('2022-04-13 00:00:00:000'); 2const endTime = toMoment('2022-04-15 00:00:00:000'); 3 4 5const t1 = toMoment('2022-04-13 00:00:00:000'); 6const t2 = toMoment('2022-04-14 00:00:00:000'); 7isPeriodIn(t1, t2, startTime, endTime, '(]'); 8// false 9isPeriodIn(t1, t2, startTime, endTime); 10// true
isIntersect
判断时间段[t1, t2]
和时间段[t3, t4]
是否相交,不包含单个端点重合情况(如需要请使用isIntersectSame
)。
1const t1 = toMoment('2022-04-13 00:00:00:000'); 2const t2 = toMoment('2022-04-15 00:00:00:000'); 3 4const t3 = toMoment('2022-04-14 00:00:00:000'); 5const t4 = toMoment('2022-04-16 00:00:00:000'); 6isIntersect(t1, t2, t3, t4); // true 7isIntersect(t1, t2, t2, t4); // false
isIntersectSame
同isIntersect
,但包含单个端点重合情况。
1const t1 = toMoment('2022-04-13 00:00:00:000'); 2const t2 = toMoment('2022-04-15 00:00:00:000'); 3 4const t3 = toMoment('2022-04-14 00:00:00:000'); 5const t4 = toMoment('2022-04-16 00:00:00:000'); 6isIntersect(t1, t2, t3, t4); // true 7isIntersect(t1, t2, t2, t4); // true
isWeekend
是否为周末。
1const t1 = toMoment('2022-04-15 00:00:00:000'); // 星期五 2isWeekend(t1); // false 3 4const t2 = toMoment('2022-04-16 00:00:00:000'); // 星期六 5isWeekend(t2); // true
get
类获取根据参数经过某些计算后的结果
getAsc
/getDesc
获取一个新的排序过的数组(每一项都经过clone
过的)。
1const t1 = toMoment('2022-04-13 00:00:00:000'); 2const t2 = toMoment('2022-04-15 00:00:00:000'); 3const t3 = toMoment('2022-04-14 00:00:00:000'); 4const t4 = toMoment('2022-04-16 00:00:00:000'); 5 6getAsc([t1, t2, t3, t4]); // [t1, t3, t2, t4] 7getDesc([t1, t2, t3, t4]); // [t4, t2, t3, t1]
getMin
/getMax
/getMinMax
获取数组中最大值最小值。
1const t1 = toMoment('2022-04-13 00:00:00:000'); 2const t2 = toMoment('2022-04-15 00:00:00:000'); 3const t3 = toMoment('2022-04-14 00:00:00:000'); 4const t4 = toMoment('2022-04-16 00:00:00:000'); 5 6getMin([t1, t2, t3, t4]); // t1 7getMax([t1, t2, t3, t4]); // t4 8getMinMax([t1, t2, t3, t4]); // [t1, t4] 9getMin([]); // undefined 10getMax([]); // undefined 11getMinMax([]); // [undefined, undefined]
getSliceTime
/getSliceNum
/getSliceTimeWithFormat
/getSliceNumWithFormat
获取t1
、t2
之间的切片。如果计算切片通过opt
来配置。
getSliceTime
: 获取所有切片结果。getSliceNum
: getSliceTime(...)length
。getSliceTimeWithFormat
: 同getSliceTime
, 但opt.startUnit
为'd'
。getSliceNumWithFormat
: getSliceTimeWithFormat(...)length
。更多案例,参考项目的测试用例。
1import type { MetType } from "moment-ex-tool"; 2const opt: Partial<MetType.SliceNumOpt> = { 3 /** 4 * 是否包含结尾时间 5 * 6 * 默认: true 7 * 8 * ex: 2022-10-10~2022-10-13 9 * 一般不包含结尾,则只有3天,包含则有4天 10 */ 11 includEnd: true, 12 /** 13 * 当包含结尾时,迭加等于结尾时,后续是否继续叠加。 14 * 15 * 默认: false 16 * 17 * ex: 2022-10-10 00:00:000~2022-10-12 00:00:000 18 * true: 19 * [ 20 * ['2022-10-10 00:00:000', '2022-10-11 00:00:000'], 21 * ['2022-10-11 00:00:000', '2022-10-12 00:00:000'], 22 * ['2022-10-12 00:00:000', '2022-10-13 00:00:000'], 23 * ['2022-10-13 00:00:000', '2022-10-14 00:00:000'], 24 * ] 25 * 26 * false: 27 * [ 28 * ['2022-10-10 00:00:000', '2022-10-11 00:00:000'], 29 * ['2022-10-11 00:00:000', '2022-10-12 00:00:000'], 30 * ['2022-10-12 00:00:000', '2022-10-13 00:00:000'], 31 * ] 32 */ 33 isSameEnd: false, 34 /** 35 * 切片长度是多少 36 * 37 * 默认: 1 38 */ 39 silceNum: 1, 40 /** 41 * 以什么单位算添加时间 42 * 43 * 默认: 'd' 44 */ 45 addUnit: 'd', 46 /** 47 * 以什么单位算开始时间 48 * 49 * 默认: undefined 50 */ 51 startUnit: 'd', 52 /** 53 * 排除的时间段 54 * 返回true,则不要指定时间段 55 * 56 * 默认: () => false 57 */ 58 exclude: (startTime, endTime) => false, 59}
getTime
获取毫秒时间戳,如果你要获取秒数时间戳,请使用moment().unix()
。
如果是非法参数,则会返回NaN
如果需要Tree Shaking,只要不全部import
即可。
1// 👍good 2import { toStr } from "moment-ex-tool"; 3 4// 👎bad 5import MET from "moment-ex-tool";
No vulnerabilities found.
No security vulnerabilities found.