建网站的软件,所见即所得的网站开发软件,有哪些网站是做采购招标的,江苏省建设厅网站首页文章目录1. 初始化日期 / 时间2. 格式化日期 / 时间3. 加 / 减4. 获取某年某月的第一天或最后一天5. 获取星期几6. 获取毫秒数7. 获取时间差#xff08;默认输出的差值单位是毫秒#xff09;8. 获取时、分、秒9. 将毫秒转为时分秒10. 判断一个日期是否在另外一个日期之后 isA…
文章目录1. 初始化日期 / 时间2. 格式化日期 / 时间3. 加 / 减4. 获取某年某月的第一天或最后一天5. 获取星期几6. 获取毫秒数7. 获取时间差默认输出的差值单位是毫秒8. 获取时、分、秒9. 将毫秒转为时分秒10. 判断一个日期是否在另外一个日期之后 isAfter11. 判断一个日期是否在另外一个日期之前 isBefore12. 判断两个日期是否相同 isSame13. 判断一个日期是否在两个日期之间 isBetweenDay.js官方文档1. 初始化日期 / 时间
dayjs().format(YYYY-MM-DD); // 初始化日期
dayjs().format(YYYY-MM-DD HH:mm:ss); // 初始化日期时间2. 格式化日期 / 时间
dayjs(value).format(YYYY-MM-DD); // 初始化日期
dayjs(value).format(YYYY-MM-DD HH:mm:ss); // 初始化日期时间3. 加 / 减
dayjs().add / dayjs().subtract 代表在当前时间上去加减 dayjs(value).add / dayjs(value).subtract 代表在指定时间value上去加减
dayjs().add(7, day).format(YYYY-MM-DD); // 2022-04-27 今天2022-04-20加上7天
dayjs().add(1, month).format(YYYY-MM-DD); // 2022-05-20 今天2022-04-20加上一月dayjs().subtract(2, year).format(YYYY-MM-DD); // 2020-05-20 今天2022-04-20减去2年
dayjs().subtract(2, hour).format(YYYY-MM-DD HH:mm:ss); // 2022-04-20 14:03:39 今天现在2022-04-20 16:03:39减去2小时所有可用单位列表 4. 获取某年某月的第一天或最后一天
获取某年某月的第一天
dayjs().startOf(year).format(YYYY-MM-DD HH:mm:ss) // 2022-01-01 00:00:00 第一天格式化出来的时分秒都是0
dayjs().startOf(month).format(YYYY-MM-DD) // 2022-04-01获取某年某月的最后一天
dayjs().endOf(year).format(YYYY-MM-DD HH:mm:ss) // 2022-12-31 23:59:59 最后时间 格式化出来的时分秒是23:59:59
dayjs().endOf(month).format(YYYY-MM-DD) // 2022-04-305. 获取星期几
dayjs().day() : 返回0(星期日)到6(星期六)的数字
设置时也只能接受 0-6 的数字
dayjs().day(6).format(YYYY-MM-DD)获取最近周六的日期 2022-04-23
dayjs().day(0).format(YYYY-MM-DD)获取最近周日的日期 2022-04-176. 获取毫秒数
dayjs(2019-01-25).valueOf() 或 dayjs().valueOf()7. 获取时间差默认输出的差值单位是毫秒
dayjs(2019-01-25).diff(2018-06-05, month); // 7
dayjs(2019-01-25).diff(dayjs(2018-06-05), month); // 7所有可用输出单位列表
8. 获取时、分、秒
当前时间2022-04-20 16:55:55 以下大部分方法都会往前溢出如毫秒超过999将持续到秒秒超过59将持续到分这边情况在设置时特别突出 console.log(-----获取年, dayjs().year()); // 2022console.log(-----获取月, dayjs().month()); // 0到11的数字 3console.log(-----获取星期, dayjs().day()); // 0(星期日)到6(星期六)的数字 3console.log(-----获取天, dayjs().date()); // 1到31的数字 20console.log(-----获取小时, dayjs().hour()); // 0到23的数字 16console.log(-----获取分钟, dayjs().minute());// 0到59的数字 55console.log(-----获取秒, dayjs().second()); // 0到59的数字 55console.log(-----获取毫秒, dayjs().millisecond()); // 0到999的数字 333
9. 将毫秒转为时分秒
// 下面毫秒数代表2022-04-20 17:43:20
const timestr 1650447800731; // 毫秒值必须是number类型如果是string结果可能和你想的不一样
console.log(将毫秒转为年-月-日 时:分:秒, dayjs(timestr).format(YYYY-MM-DD HH:mm:ss));
console.log(获取年, dayjs(timestr).year()); //
console.log(获取月, dayjs(timestr).month());
console.log(获取天, dayjs(timestr).date());
console.log(获取时, dayjs(timestr).hour());
console.log(获取分, dayjs(timestr).minute());注意这里 year()、month()、date()、hour()、minute()、second()、millisecond() 等方法均可使用
10. 判断一个日期是否在另外一个日期之后 isAfter
// day.js 为 2022-04-20
console.log(isAfter, dayjs().isAfter(dayjs(2011-01-01))) // true
console.log(isAfter, dayjs(2022-04-20).isAfter(dayjs(2022-04-21))) // false
console.log(isAfter, dayjs(2022-04-20).isAfter(dayjs(2022-04-20))) // 相同也为false11. 判断一个日期是否在另外一个日期之前 isBefore
// day.js 为 2022-04-20
console.log(isBefore, dayjs().isBefore(dayjs(2011-01-01))) // false
console.log(isBefore, dayjs(2022-04-20).isBefore(dayjs(2022-04-21))) // true
console.log(isBefore, dayjs(2022-04-20).isBefore(dayjs(2022-04-20))) // 日期相同时也为false12. 判断两个日期是否相同 isSame
// day.js 为 2022-04-20
console.log(isSame, dayjs().isSame(dayjs(2011-01-01))) // false
console.log(isSame, dayjs(2022-04-20).isSame(dayjs(2022-04-21))) // false
console.log(isSame, dayjs(2022-04-20).isSame(dayjs(2022-04-20))) // true13. 判断一个日期是否在两个日期之间 isBetween
注意: 此功能依赖IsBetween插件 此处也将演示如何使用 Day.js 的插件
import dayjs from dayjs // 引入dayjs
import isBetween from dayjs/plugin/isBetween // 引入相关插件created() {dayjs.extend(isBetween); // 挂载插件// 使用插件console.log(isBetween, dayjs(2010-10-20).isBetween(2010-10-19, dayjs(2010-10-25)) )
}Day.js 里面有 相同或之前 IsSameOrBefore 和 相同或之后 IsSameOrAfter的方法可根据实际需求取用但这两个方法需要依赖相应的插件
注意 isAfter、isBefore、isSame、IsBetween 默认都是通过将日期转为milliseconds去比较的所以这两个方法有第二个参数。即指定比较的粒度
console.log(isBefore, dayjs(2022-04-20).isBefore(2015-01-01, year))所有可用单位列表