时间格式化类
day.js库:dayjs.fenxianglu.cn/
moment库:momentjs.cn/
【day.js库】
Day.js 是一个轻量级的 JavaScript 日期处理库,提供了许多简单易用的 API 来处理日期和时间。以下是 Day.js 库中一些常见的语法总结:
1. 引入 Day.js
首先,你需要在项目中引入 Day.js 库:
const dayjs = require('dayjs');
// 如果使用 ES6 模块
import dayjs from 'dayjs';
2. 创建日期对象
// 当前时间
const now = dayjs();
// 指定日期
const specificDate = dayjs('2024-08-29');
// 使用 JavaScript Date 对象
const fromDate = dayjs(new Date(2024, 7, 29)); // 月份从 0 开始计数
3. 获取日期时间信息
const year = dayjs().year(); // 获取年份
const month = dayjs().month(); // 获取月份(0-11)
const date = dayjs().date(); // 获取日期(1-31)
const day = dayjs().day(); // 获取星期几(0-6,0为周日)
const hour = dayjs().hour(); // 获取小时(0-23)
const minute = dayjs().minute(); // 获取分钟(0-59)
const second = dayjs().second(); // 获取秒(0-59)
const millisecond = dayjs().millisecond(); // 获取毫秒(0-999)
4. 格式化日期
const formattedDate = dayjs().format('YYYY-MM-DD'); // 2024-08-29
const formattedTime = dayjs().format('HH:mm:ss'); // 14:35:20
const formattedFull = dayjs().format('YYYY-MM-DD HH:mm:ss'); // 2024-08-29 14:35:20
5. 操作日期
const tomorrow = dayjs().add(1, 'day'); // 加一天
const nextMonth = dayjs().add(1, 'month'); // 加一个月
const lastYear = dayjs().subtract(1, 'year'); // 减一年
const startOfDay = dayjs().startOf('day'); // 获取当天的开始时间
const endOfMonth = dayjs().endOf('month'); // 获取当月的结束时间
6. 比较日期
const isBefore = dayjs('2024-08-29').isBefore('2024-09-01'); // true
const isAfter = dayjs('2024-08-29').isAfter('2024-08-01'); // true
const isSame = dayjs('2024-08-29').isSame('2024-08-29'); // true
7. 解析与显示相对时间
import relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(relativeTime);
const fromNow = dayjs('2024-08-28').fromNow(); // "a day ago"
const toNow = dayjs('2024-08-28').toNow(); // "in a day"
8. 处理时间区间
import duration from 'dayjs/plugin/duration';
dayjs.extend(duration);
const durationInMinutes = dayjs.duration(90, 'minutes'); // 90 分钟
const durationInDays = dayjs.duration(2, 'days'); // 2 天
const humanReadable = durationInMinutes.humanize(); // "an hour and a half"
9. 设置与获取时区
如果需要处理时区,可以引入插件:
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
dayjs.extend(utc);
dayjs.extend(timezone);
const dateInUTC = dayjs().utc(); // 转换为 UTC 时间
const dateInTZ = dayjs().tz('Asia/Shanghai'); // 转换为特定时区时间
10. 自定义解析和显示
import customParseFormat from 'dayjs/plugin/customParseFormat';
dayjs.extend(customParseFormat);
const customDate = dayjs('31-12-2024', 'DD-MM-YYYY'); // 自定义解析日期
11. 插件扩展
Day.js 提供了很多插件,可以根据需要进行扩展使用。例如:
isoWeek:ISO 周localeData:多语言支持isBetween:判断日期是否在两个日期之间weekOfYear:获取一年中的第几周
12. 本地化
import 'dayjs/locale/zh-cn'; // 引入中文语言包
dayjs.locale('zh-cn'); // 设置为中文
const formattedChineseDate = dayjs().format('YYYY年MM月DD日'); // 2024年08月29日
以上是 Day.js 的一些常见语法,你可以根据项目需要使用其中的功能。Day.js 的 API 简洁且功能强大,适合在项目中进行日期处理。
【Moment库】
Moment.js 是一个功能强大的 JavaScript 日期处理库,广泛用于日期和时间的解析、验证、操作和格式化。以下是 Moment.js 库中的一些常见语法总结:
1. 引入 Moment.js
首先,你需要在项目中引入 Moment.js 库:
const moment = require('moment');
// 或使用 ES6 模块
import moment from 'moment';
2. 创建日期对象
// 当前时间
const now = moment();
// 指定日期
const specificDate = moment('2024-08-29');
// 使用 JavaScript Date 对象
const fromDate = moment(new Date(2024, 7, 29)); // 月份从 0 开始计数
3. 获取日期时间信息
const year = moment().year(); // 获取年份
const month = moment().month(); // 获取月份(0-11)
const date = moment().date(); // 获取日期(1-31)
const day = moment().day(); // 获取星期几(0-6,0为周日)
const hour = moment().hour(); // 获取小时(0-23)
const minute = moment().minute(); // 获取分钟(0-59)
const second = moment().second(); // 获取秒(0-59)
const millisecond = moment().millisecond(); // 获取毫秒(0-999)
4. 格式化日期
const formattedDate = moment().format('YYYY-MM-DD'); // 2024-08-29
const formattedTime = moment().format('HH:mm:ss'); // 14:35:20
const formattedFull = moment().format('YYYY-MM-DD HH:mm:ss'); // 2024-08-29 14:35:20
// 使用本地化格式
const formattedLocalized = moment().format('LLLL'); // Wednesday, August 29, 2024 2:35 PM
5. 操作日期
const tomorrow = moment().add(1, 'day'); // 加一天
const nextMonth = moment().add(1, 'month'); // 加一个月
const lastYear = moment().subtract(1, 'year'); // 减一年
const startOfDay = moment().startOf('day'); // 获取当天的开始时间
const endOfMonth = moment().endOf('month'); // 获取当月的结束时间
6. 比较日期
const isBefore = moment('2024-08-29').isBefore('2024-09-01'); // true
const isAfter = moment('2024-08-29').isAfter('2024-08-01'); // true
const isSame = moment('2024-08-29').isSame('2024-08-29'); // true
7. 解析与显示相对时间
const fromNow = moment('2024-08-28').fromNow(); // "a day ago"
const toNow = moment('2024-08-28').toNow(); // "in a day"
const relativeTime = moment('2024-08-29').from('2024-08-01'); // "in 28 days"
8. 处理时间区间
const durationInMinutes = moment.duration(90, 'minutes'); // 90 分钟
const durationInDays = moment.duration(2, 'days'); // 2 天
const humanReadable = durationInMinutes.humanize(); // "an hour and a half"
9. 设置与获取时区
如果需要处理时区,可以使用 moment-timezone 插件:
const moment = require('moment-timezone');
// 或使用 ES6 模块
import moment from 'moment-timezone';
const dateInUTC = moment().utc(); // 转换为 UTC 时间
const dateInTZ = moment().tz('Asia/Shanghai'); // 转换为特定时区时间
10. 自定义解析和显示
const customDate = moment('31-12-2024', 'DD-MM-YYYY'); // 自定义解析日期
11. 本地化
Moment.js 提供了多语言支持,你可以根据需要设置不同的语言:
moment.locale('zh-cn'); // 设置为中文
const formattedChineseDate = moment().format('YYYY年MM月DD日'); // 2024年08月29日
12. 日期和时间的计算
Moment.js 支持对日期和时间进行复杂的计算:
const diffInDays = moment('2024-08-29').diff('2024-08-01', 'days'); // 28 天
const diffInHours = moment('2024-08-29 14:00').diff('2024-08-29 10:00', 'hours'); // 4 小时
13. 插件扩展
Moment.js 还有很多插件扩展功能,如 moment-range 可以用于处理日期范围,moment-recur 可以处理重复日期等。
以上是 Moment.js 的一些常见语法总结。虽然 Moment.js 功能强大,但它也相对较重。近年来,轻量级的库如 Day.js 逐渐流行,因此在新项目中选择时,可以根据项目需求进行权衡。