Installation
npm install myjs-common
导入模块
import { MyDate, DATE_FORMATTER, DATE_ENUM, DATE_PROP_TYPE } from "myjs-common";
format
格式化时间
// 2019-09-26 17:15:52.423
let now: MyDate = new MyDate();
/** 使用系统自带的格式器 */
// 2019
console.log(now.format(DATE_FORMATTER.YEAR_FORMAT));
// 2019-09
console.log(now.format(DATE_FORMATTER.MONTH_FORMAT));
// 2019-09-26
console.log(now.format(DATE_FORMATTER.DATE_FORMAT));
// 2019-09-26 17
console.log(now.format(DATE_FORMATTER.HOUR_FORMAT));
// 2019-09-26 17:15
console.log(now.format(DATE_FORMATTER.MINUTE_FORMAT));
// 2019-09-26 17:15:52
console.log(now.format(DATE_FORMATTER.SECONDS_FORMAT));
// 2019-09-26 17:15:52.423
console.log(now.format(DATE_FORMATTER.DATETIME_FORMAT));
/** 使用自定义格式器字符串 */
// 等价于 DATE_FORMATTER.DATETIME_FORMAT
// 2019-09-26 17:15:52.423
console.log(now.format("yyyy-MM-dd HH:mm:ss.S"));
// 19-09-26 17:15:52.423
console.log(now.format("yy-MM-dd HH:mm:ss.S"));
prev / after
日期和指定日期进行前后对比
let date: Date = new Date("2019-09-20");
// false
console.log(now.prev(date));
// true
console.log(now.after(date));
diff
返回两个日期之间相差的年数、月份数、天数(默认
)、小时数、分钟数、秒数
let date: Date = new Date("2019-09-20");
// 6
console.log(now.diff(date));
// 6
console.log(now.diff(date, DATE_ENUM.DAY));
// 153
console.log(now.diff(date, DATE_ENUM.HOUR));
add
在当前日期中增加或减去指定的时间间隔(其中时间可以是年、月、日[默认
]、时、分、秒)
let add: MyDate = now.add(10, DATE_PROP_TYPE.HOUR);
// 2019-09-27 03:15:52.423
console.log(add.format(DATE_FORMATTER.DATETIME_FORMAT));
Reference