__defineGetter__和__defineSetter__在日期中的应用

日期函数每次取年月日都要调用Date的函数,有点麻烦,通过__defineGetter__可以处理一下,就能通过Date的实例对象直接获取年月日,例如 date.year获取日期对象date的年份。月份因为与正常月份差一个月,可以通过函数自动校正一下,使用起来就更符合习惯了。很多时候我们需要显示一个日期、时间或者日期时间,就可以通过__defineGetter__处理好之后,直接返回对应的数据。

let { log } = console;

Date.prototype.__defineGetter__('year', function() {return this.getFullYear();});  
Date.prototype.__defineSetter__('year', function(y) {this.setFullYear(y)});  
Date.prototype.__defineGetter__('month', function() {return this.getMonth() + 1;});  
Date.prototype.__defineSetter__('month', function(m) {this.setMonth(m-1)});  
Date.prototype.__defineGetter__('day', function() {return this.getDate();});  
Date.prototype.__defineSetter__('day', function(d) {this.setDate(d)});    
Date.prototype.__defineGetter__('hour', function() {return this.getHours();});  
Date.prototype.__defineSetter__('hour', function(h) {this.setHours(h)});    
Date.prototype.__defineGetter__('minute', function() {return this.getMinutes();});  
Date.prototype.__defineSetter__('minute', function(m) {this.setMinutes(m)});    
Date.prototype.__defineGetter__('seconds', function() {return this.getSeconds();});  
Date.prototype.__defineSetter__('seconds', function(s) {this.setSeconds(s)});

Date.prototype.__defineGetter__("date", function (){return `${this.year}-${(this.month.dbl())}-${this.day.dbl()}`});
Date.prototype.__defineGetter__("time", function (){return `${this.hour.dbl()}:${this.minute.dbl()}:${this.seconds.dbl()}`});
Date.prototype.__defineGetter__("datetime", function (){return `${this.date} ${this.time}`});
  
// 将数字转换成2位的字符串,不足两位的在前面补0
Number.prototype.dbl = function (){
	return String(this).padStart(2, 0);
}

let num = 2;
log(num.dbl());

function doubleNum(n){
	return String(n).padStart(2, 0);
}

var now = new Date;  
log("%O",now); // 这样打印可以看到日期的属性和方法
let { year: y, month: m, day: d } = now;

log("年:%s",y) // 年:2019
log(y, m, d); // 2019 6 20
log(now.date); // 2019-06-20
log(now.time); // 10:56:53
log(now.datetime); // 2019-06-20 10:56:53

  上面这种写法已经过时了,现在已经不推荐使用__defineGetter__和__defineSetter__。因此可以使用Object.defineProperty来实现,下面是代码

// 将数字转换成2位的字符串,不足两位的在前面补0
Number.prototype.dbl = function (){
	return String(this).padStart(2, 0);
}

Object.defineProperty(Date.prototype, "year", {
	enumerable : true,
  	configurable : true,
	get: function (){
		return this.getFullYear();
	},
	set: function (y){
		this.setFullYear(y);
	}	
});

Object.defineProperty(Date.prototype, "month", {
	enumerable : true,
  	configurable : true,
	get: function (){
		return this.getMonth() + 1;
	},
	set: function (m){
		this.setMonth(m - 1);
	}	
});

Object.defineProperty(Date.prototype, "day", {
	enumerable : true,
  	configurable : true,
	get: function (){
		return this.getDate();
	},
	set: function (d){
		this.setDate(d);
	}	
});

Object.defineProperty(Date.prototype, "hour", {
	enumerable : true,
  	configurable : true,
	get: function (){
		return this.getHours();
	},
	set: function (h){
		this.setHours(h);
	}	
});

Object.defineProperty(Date.prototype, "minutes", {
	enumerable : true,
  	configurable : true,
	get: function (){
		return this.getMinutes();
	},
	set: function (m){
		this.setMinutes(m);
	}	
});

Object.defineProperty(Date.prototype, "seconds", {
	enumerable : true,
  	configurable : true,
	get: function (){
		return this.getSeconds();
	},
	set: function (s){
		this.setSeconds(s);
	}	
});

Object.defineProperty(Date.prototype, "y", {
	get: function (){
		return this.year;
	}
});
Object.defineProperty(Date.prototype, "m", {
	get: function (){
		return this.month;
	}
});
Object.defineProperty(Date.prototype, "d", {
	get: function (){
		return this.day;
	}
});
Object.defineProperty(Date.prototype, "h", {
	get: function (){
		return this.hour;
	}
});
Object.defineProperty(Date.prototype, "min", {
	get: function (){
		return this.minutes;
	}
});
Object.defineProperty(Date.prototype, "s", {
	get: function (){
		return this.seconds;
	}
});

Object.defineProperty(Date.prototype, "date", {
	get: function (){
		// return `${this.y}-${this.m.dbl()}-${this.d.dbl()}`;
		const that = this;
		return function (sep = "-"){
			return `${that.y}${sep}${that.m.dbl()}${sep}${that.d.dbl()}`;
		}
	}
});

Object.defineProperty(Date.prototype, "time", {
	get: function (){
		return `${this.h.dbl()}:${this.min.dbl()}:${this.s.dbl()}`;
	}
});

Object.defineProperty(Date.prototype, "datetime", {
	get: function (){
		// return `${this.date} ${this.time}`;
		const that = this;
		return function (sep = "-"){
			return `${this.date(sep)} ${this.time}`;
		}
	}
});

let d = new Date();
console.log(d.date());
console.log(d.time);
console.log(d.datetime("/"));

  

  

转载于:https://www.cnblogs.com/zhaodesheng/p/11057708.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
2 400 index.vue:399 QiniuRequestError {name: 'RequestError', message: 'xhr request failed, code: 400 response: {"error":"…field CompleteMultipart.mimeType of type string"}', stack: 'Error\n at QiniuRequestError.QiniuError (webpack…node_modules/qiniu-js/esm/utils/helper.js:248:24)', code: 400, reqId: 'Mm0AAACDvvroC2YX', …} code : 400 data : error : "json: cannot unmarshal array into Go struct field CompleteMultipart.mimeType of type string" [[Prototype]] : Object constructor : ƒ Object() hasOwnProperty : ƒ hasOwnProperty() isPrototypeOf : ƒ isPrototypeOf() propertyIsEnumerable : ƒ propertyIsEnumerable() toLocaleString : ƒ toLocaleString() toString : ƒ toString() valueOf : ƒ valueOf() __defineGetter__ : ƒ __defineGetter__() __defineSetter__ : ƒ __defineSetter__() __lookupGetter__ : ƒ __lookupGetter__() __lookupSetter__ : ƒ __lookupSetter__() __proto__ : (...) get __proto__ : ƒ __proto__() set __proto__ : ƒ __proto__() isRequestError : true message : "xhr request failed, code: 400 response: {\"error\":\"json: cannot unmarshal array into Go struct field CompleteMultipart.mimeType of type string\"}" name : "RequestError" reqId : "Mm0AAACDvvroC2YX" stack : "Error\n at QiniuRequestError.QiniuError (webpack-internal:///./node_modules/qiniu-js/esm/errors/index.js:47:22)\n at new QiniuRequestError (webpack-internal:///./node_modules/qiniu-js/esm/errors/index.js:55:28)\n at xhr.onreadystatechange (webpack-internal:///./node_modules/qiniu-js/esm/utils/helper.js:248:24)" [[Prototype]] : QiniuError constructor : ƒ QiniuRequestError(code, reqId, message, data) [[Prototype]] : Object
最新发布
06-07

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值