日期函数每次取年月日都要调用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("/"));

  

  

__defineGetter__和__defineSetter__在日期中的应用的更多相关文章

  1. oracle中从指定日期中获取月份或者部分数据

    从指定日期中获取部分数据: 如月份: select to_CHAR(sysdate,'MM') FROM DUAL; 或者: select extract(month from sysdate) fr ...

  2. MySQL数据库中日期中包涵零值的问题

    默认情况下MySQL是可以接受在日期中插入0值,对于现实来说日期中的0值又没有什么意义.调整MySQL的sql_mode变量就能达到目的. set @@global.sql_mode='STRICT_ ...

  3. SQL根据出生日期精确计算年龄、获取日期中的年份、月份

    第一种: 一张人员信息表里有一人生日(Birthday)列,跟据这个列,算出该人员的年龄 datediff(year,birthday,getdate()) 例:birthday = '2003-3- ...

  4. JS[获取两个日期中所有的月份]

    //------[获取两个日期中所有的月份中] function getMonthBetween(start,end){ var result = []; var s = start.split(&q ...

  5. 【HANA系列】SAP HANA SQL从给定日期中获取月份

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL从给定日 ...

  6. 【HANA系列】SAP HANA SQL从给定日期中获取分钟

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL从给定日 ...

  7. 【HANA系列】SAP HANA SQL从给定日期中获取年份

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL从给定日 ...

  8. 面试题1 -- Java 中,怎么在格式化的日期中显示时区?

    使用SimpleDateFormat来实现格式化日期 import java.text.SimpleDateFormat; import java.util.Date; public class Da ...

  9. Excel日期中那个著名的bug

    一个软件中的bug能够持续多久?答案不一,大多数bug在软件测试阶段就已经被干掉,又有许多死在Preview阶段,抑或正式上线后不久被干掉,有些则伴随软件终生,直到下一代产品发布才寿终正寝,而Exce ...

随机推荐

  1. 2019-8-8-WPF-非客户区的触摸和鼠标点击响应

    title author date CreateTime categories WPF 非客户区的触摸和鼠标点击响应 lindexi 2019-08-08 16:48:31 +0800 2019-07 ...

  2. TZOJ 2478 How many 0's?(数位DP)

    描述 A Benedict monk No.16 writes down the decimal representations of all natural numbers between and ...

  3. 谷歌浏览器flash被禁用解决方法

    谷歌浏览器访问设置:chrome://settings/content/flash 把要启动flash插件的网址添加进去

  4. CentOS 6.5之zabbix2.2的简单部署

    Zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.(目前,官方长期维护的稳定版本有2.2和3.0) 其他常见的监控工具还有: (1)Cacti:Cacti是一 ...

  5. MySQL8.0.17 - Multi-Valued Indexes 简述

    本文主要简单介绍下8.0.17新引入的功能multi-valued index, 顾名思义,索引上对于同一个Primary key, 可以建立多个二级索引项,实际上已经对array类型的基础功能做了支 ...

  6. JS倒计时-毫秒

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...

  7. 详解WPF DockPanel的LastChildFill属性

    MSDN解释: 获取或设置一个值,该值指示 System.Windows.Controls.DockPanel 中的最后一个子元素是否拉伸以填充剩余的可用空间. 返回: 如果最后一个子元素拉伸以填充剩 ...

  8. 10.30NOIP集训总结

    1.第一眼是优先队列,然后发现n很大. 然后再观察一下,发现就可以二分,然后套用莫比乌斯反演里面的分块. 然后卡卡常就过了. 发现一个严肃的事情,就是说long long相除(相模)都会占用很多时间. ...

  9. ubuntu安装搜狗输入法后无法使用goland的快捷键 ctrl+alt+B

    安装了搜狗拼音后,其快捷键ctrl+alt+b会启动软键盘,造成与其他编辑器快捷键的冲突. 为了禁止使用ctrl+alt+b启动软键盘,可以: 1. 在搜狗拼音输入法选择设置 2. 高级设置 3. 高 ...

  10. .Net Core 授权系统组件解析

    前面关于.Net Core如何进行用户认证的核心流程介绍完毕之后,.Net Core 认证系统之Cookie认证源码解析远程认证暂时不介绍,后期有时间,我会加上.接下去介绍认证组件是如何和认证组件一起 ...