在使用JS内置对象Date时,觉得颇多不便,便基于Date的一些方法来封装一个time.js的时间库;
本篇文章仅作学习和尝试封装的简单记录,其中也记录了封装过程中遇到的一些bug和调试/解决方法
function time(...params) {
//接受多个参数,把参数打包为数组
if (!(this instanceof time)) {
return new time(...params)
}//如果当前对象不是time函数构造出来的,就new一个
if(arguments.length>1){
params[1] -= 1//月份
}
this.date = new Date(...params) //自有属性
}
time.prototype = {//共有属性
constructor: time,
//重载,传参
parts(attrs){//读取
if(attrs===undefined){
console.log(this.date)
//output:当前的中国标准时间
return{
year: this.date.getFullYear(),
month: this.date.getMonth()+1,
//月份index从0开始,加一调成现实月份
weekday: this.date.getDay(),
day: this.date.getDate(),
//Uncaught TypeError: Cannot read properties of undefined (reading 'getDate')
//已解决,把date(日期)写成data(数据)
hour: this.date.getHours(),//前面修改后这里报错了,后来发现是前面结尾漏了逗号
minute: this.date.getMinutes(),
second: this.date.getSeconds(),
ms: this.date.getMilliseconds()
}
}else{//设置
const { year, month, weekday, day, hour, minute,second, ms} = attrs//传参
// if(year){
// this.date.setFullYear(year)
// }缩写如下
year !== undefined && this.date.setFullYear(year)//存在且不为undefined后执行
//0是假值,会判断为不存在;设为undefined会把排除范围缩小,放出0
month !== undefined && this.date.setMonth(month - 1)
weekday !== undefined && this.date.setDay(weekday)
//Uncaught ReferenceError: weekday is not defined//在上面传参,已解决
day !== undefined && this.date.setDate(day)
hour !== undefined && this.date.setHours(hour)
minute !== undefined && this.date.setMinutes(minute)
second !== undefined && this.date.setSeconds(second)
ms !== undefined && this.date.setMilliseconds(ms)
}
},
add(n, unit) {//日期增加; 传入 数字+日期单位
const valid ='year month day hour minute second ms'.split(' ').includes(unit)
console.log(valid)
console.log(unit)
if(!valid){//“防御性”编程
throw new Error('输入的单位错误? 我不认识'+unit)
}
const table ={//map
ms: 1,
second: 1000,
minute: 1000*60,
hour: 1000*60*60,
day: 1000*60*60*24
}
if( unit === 'year'){
this.date.setFullYear(this.date.getFullYear()+n)
}else if(unit ==='month'){
this.date.setMonth(this.date.getMonth()+n)
}else{//加上对应的毫秒数
this.date = new Date(this.date-0+table[unit]*n)
}
},
isLeapYear() {//判断是否闰年
const year = this.date.getFullYear()
if(year%4===0){
if(year%100===0){
if(year%400===0){
return true
}else {
return false
}
} else{
return true//被4整除但不被100整除
}
}else {
return false
}
},
lastDayOdfMonth(){//判断是否是每月最后一天
const {year, month,hour,minute,second,ms} =this.parts()
return new time(year, month+1, 0,hour,minute,second,ms)
},
format(pattern){//格式化输出
console.log(pattern)
const {year, month, day, hour, minute, second} =this.parts()
console.log(this.parts())
// 支持的占位符有 yyyy MM dd HH mm ss
return pattern.replace(/yyyy/g, year)
.replace(/MM/g, padLeftZero(month))
.replace(/dd/g, padLeftZero(day))
.replace(/HH/g, padLeftZero(hour))
.replace(/mm/g, padLeftZero(minute))
.replace(/ss/g, padLeftZero(second))
}
}
function padLeftZero(n){
return n > 10? ''+n: '0'+n
}
//使用
const t1 = time('2020-02-1')//t1不是时间本身,是用来操作时间的api
//格式化输出
console.log(t1.format('yyyy年MM月dd日 HH:mm'))
//写入
t1.parts({
year:2022,
month:10,
day:19
})
const {year, month, day} = t1.parts()
console.log(year, month, day)
//add
t1.add(2,'month')
console.log(t1.date.toLocaleString())
t1.add(3, 'day')
console.log(t1.date.toLocaleString())
//TypeError: Cannot read properties of undefined (reading 'toLocaleString')
console.log(t1.format('yyyy年MM月dd日 HH:mm'))
//找本月最后一天
const t2 = time('2001-01-01')
console.log(t2.date.toLocaleString())
console.log(t2.lastDayOdfMonth().date.toLocaleDateString())