依赖包安装
# 安装 myjs-common 包
npm install [email protected]
格式器表达式
- YEAR_FORMAT: 年格式化-yyyy
- MONTH_FORMAT: 月格式化-yyyy-MM
- DATE_FORMAT: 日期格式化-yyyy-MM-dd
- HOUR_FORMAT: 时格式化-yyyy-MM-dd HH
- MINUTE_FORMAT: 分格式化-yyyy-MM-dd HH:mm
- SECONDS_FORMAT: 秒格式化-yyyy-MM-dd HH:mm:ss
- DATETIME_FORMAT: 时间格式化-yyyy-MM-dd HH:mm:ss.S
程序演示
import { SimpleDateFormat, DATE_FORMATTER } from "myjs-common";
// 对当前日期进行格式化
console.log(SimpleDateFormat.formatNow(DATE_FORMATTER.YEAR_FORMAT));
console.log(SimpleDateFormat.formatNow(DATE_FORMATTER.MONTH_FORMAT));
console.log(SimpleDateFormat.formatNow(DATE_FORMATTER.DATE_FORMAT));
console.log(SimpleDateFormat.formatNow(DATE_FORMATTER.HOUR_FORMAT));
console.log(SimpleDateFormat.formatNow(DATE_FORMATTER.MINUTE_FORMAT));
console.log(SimpleDateFormat.formatNow(DATE_FORMATTER.SECONDS_FORMAT));
console.log(SimpleDateFormat.formatNow(DATE_FORMATTER.DATETIME_FORMAT));
console.log("\n");
// 对当前日期进行格式化
console.log(SimpleDateFormat.formatDate(new Date(), DATE_FORMATTER.YEAR_FORMAT));
console.log(SimpleDateFormat.formatDate(new Date(), DATE_FORMATTER.MONTH_FORMAT));
console.log(SimpleDateFormat.formatDate(new Date(), DATE_FORMATTER.DATE_FORMAT));
console.log(SimpleDateFormat.formatDate(new Date(), DATE_FORMATTER.HOUR_FORMAT));
console.log(SimpleDateFormat.formatDate(new Date(), DATE_FORMATTER.MINUTE_FORMAT));
console.log(SimpleDateFormat.formatDate(new Date(), DATE_FORMATTER.SECONDS_FORMAT));
console.log(SimpleDateFormat.formatDate(new Date(), DATE_FORMATTER.DATETIME_FORMAT));
console.log("\n");
// 对指定日期进行格式化
console.log(SimpleDateFormat.formatDate(new Date("2019-9-20 22:33:51"), DATE_FORMATTER.YEAR_FORMAT));
console.log(SimpleDateFormat.formatDate(new Date("2019-9-20 22:33:51"), DATE_FORMATTER.MONTH_FORMAT));
console.log(SimpleDateFormat.formatDate(new Date("2019-9-20 22:33:51"), DATE_FORMATTER.DATE_FORMAT));
console.log(SimpleDateFormat.formatDate(new Date("2019-9-20 22:33:51"), DATE_FORMATTER.HOUR_FORMAT));
console.log(SimpleDateFormat.formatDate(new Date("2019-9-20 22:33:51"), DATE_FORMATTER.MINUTE_FORMAT));
console.log(SimpleDateFormat.formatDate(new Date("2019-9-20 22:33:51"), DATE_FORMATTER.SECONDS_FORMAT));
console.log(SimpleDateFormat.formatDate(new Date("2019-9-20 22:33:51"), DATE_FORMATTER.DATETIME_FORMAT));
console.log("\n");
运行输出结果
2019
2019-09
2019-09-20
2019-09-20 22
2019-09-20 22:34
2019-09-20 22:34:58
2019-09-20 22:34:58.314
2019
2019-09
2019-09-20
2019-09-20 22
2019-09-20 22:34
2019-09-20 22:34:58
2019-09-20 22:34:58.318
2019
2019-09
2019-09-20
2019-09-20 18
2019-09-20 18:40
2019-09-20 18:40:20
2019-09-20 18:40:20.0