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

  1. let { log } = console;
  2.  
  3. Date.prototype.__defineGetter__('year', function() {return this.getFullYear();});
  4. Date.prototype.__defineSetter__('year', function(y) {this.setFullYear(y)});
  5. Date.prototype.__defineGetter__('month', function() {return this.getMonth() + 1;});
  6. Date.prototype.__defineSetter__('month', function(m) {this.setMonth(m-1)});
  7. Date.prototype.__defineGetter__('day', function() {return this.getDate();});
  8. Date.prototype.__defineSetter__('day', function(d) {this.setDate(d)});
  9. Date.prototype.__defineGetter__('hour', function() {return this.getHours();});
  10. Date.prototype.__defineSetter__('hour', function(h) {this.setHours(h)});
  11. Date.prototype.__defineGetter__('minute', function() {return this.getMinutes();});
  12. Date.prototype.__defineSetter__('minute', function(m) {this.setMinutes(m)});
  13. Date.prototype.__defineGetter__('seconds', function() {return this.getSeconds();});
  14. Date.prototype.__defineSetter__('seconds', function(s) {this.setSeconds(s)});
  15.  
  16. Date.prototype.__defineGetter__("date", function (){return `${this.year}-${(this.month.dbl())}-${this.day.dbl()}`});
  17. Date.prototype.__defineGetter__("time", function (){return `${this.hour.dbl()}:${this.minute.dbl()}:${this.seconds.dbl()}`});
  18. Date.prototype.__defineGetter__("datetime", function (){return `${this.date} ${this.time}`});
  19.  
  20. // 将数字转换成2位的字符串,不足两位的在前面补0
  21. Number.prototype.dbl = function (){
  22. return String(this).padStart(2, 0);
  23. }
  24.  
  25. let num = 2;
  26. log(num.dbl());
  27.  
  28. function doubleNum(n){
  29. return String(n).padStart(2, 0);
  30. }
  31.  
  32. var now = new Date;
  33. log("%O",now); // 这样打印可以看到日期的属性和方法
  34. let { year: y, month: m, day: d } = now;
  35.  
  36. log("年:%s",y) // 年:2019
  37. log(y, m, d); // 2019 6 20
  38. log(now.date); // 2019-06-20
  39. log(now.time); // 10:56:53
  40. log(now.datetime); // 2019-06-20 10:56:53

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

  1. // 将数字转换成2位的字符串,不足两位的在前面补0
  2. Number.prototype.dbl = function (){
  3. return String(this).padStart(2, 0);
  4. }
  5.  
  6. Object.defineProperty(Date.prototype, "year", {
  7. enumerable : true,
  8. configurable : true,
  9. get: function (){
  10. return this.getFullYear();
  11. },
  12. set: function (y){
  13. this.setFullYear(y);
  14. }
  15. });
  16.  
  17. Object.defineProperty(Date.prototype, "month", {
  18. enumerable : true,
  19. configurable : true,
  20. get: function (){
  21. return this.getMonth() + 1;
  22. },
  23. set: function (m){
  24. this.setMonth(m - 1);
  25. }
  26. });
  27.  
  28. Object.defineProperty(Date.prototype, "day", {
  29. enumerable : true,
  30. configurable : true,
  31. get: function (){
  32. return this.getDate();
  33. },
  34. set: function (d){
  35. this.setDate(d);
  36. }
  37. });
  38.  
  39. Object.defineProperty(Date.prototype, "hour", {
  40. enumerable : true,
  41. configurable : true,
  42. get: function (){
  43. return this.getHours();
  44. },
  45. set: function (h){
  46. this.setHours(h);
  47. }
  48. });
  49.  
  50. Object.defineProperty(Date.prototype, "minutes", {
  51. enumerable : true,
  52. configurable : true,
  53. get: function (){
  54. return this.getMinutes();
  55. },
  56. set: function (m){
  57. this.setMinutes(m);
  58. }
  59. });
  60.  
  61. Object.defineProperty(Date.prototype, "seconds", {
  62. enumerable : true,
  63. configurable : true,
  64. get: function (){
  65. return this.getSeconds();
  66. },
  67. set: function (s){
  68. this.setSeconds(s);
  69. }
  70. });
  71.  
  72. Object.defineProperty(Date.prototype, "y", {
  73. get: function (){
  74. return this.year;
  75. }
  76. });
  77. Object.defineProperty(Date.prototype, "m", {
  78. get: function (){
  79. return this.month;
  80. }
  81. });
  82. Object.defineProperty(Date.prototype, "d", {
  83. get: function (){
  84. return this.day;
  85. }
  86. });
  87. Object.defineProperty(Date.prototype, "h", {
  88. get: function (){
  89. return this.hour;
  90. }
  91. });
  92. Object.defineProperty(Date.prototype, "min", {
  93. get: function (){
  94. return this.minutes;
  95. }
  96. });
  97. Object.defineProperty(Date.prototype, "s", {
  98. get: function (){
  99. return this.seconds;
  100. }
  101. });
  102.  
  103. Object.defineProperty(Date.prototype, "date", {
  104. get: function (){
  105. // return `${this.y}-${this.m.dbl()}-${this.d.dbl()}`;
  106. const that = this;
  107. return function (sep = "-"){
  108. return `${that.y}${sep}${that.m.dbl()}${sep}${that.d.dbl()}`;
  109. }
  110. }
  111. });
  112.  
  113. Object.defineProperty(Date.prototype, "time", {
  114. get: function (){
  115. return `${this.h.dbl()}:${this.min.dbl()}:${this.s.dbl()}`;
  116. }
  117. });
  118.  
  119. Object.defineProperty(Date.prototype, "datetime", {
  120. get: function (){
  121. // return `${this.date} ${this.time}`;
  122. const that = this;
  123. return function (sep = "-"){
  124. return `${this.date(sep)} ${this.time}`;
  125. }
  126. }
  127. });
  128.  
  129. let d = new Date();
  130. console.log(d.date());
  131. console.log(d.time);
  132. 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. MySQL 报错:Translating SQLException with SQL state '42000', error code '1064', message

    MySQL报错详细日志 2019-09-12 16:42:29 [http-nio-80-exec-25] DEBUG [org.springframework.jdbc.support.SQLErr ...

  2. Django项目:CRM(客户关系管理系统)--32--24PerfectCRM实现King_admin自定义操作数据

    #admin.py # ————————01PerfectCRM基本配置ADMIN———————— from django.contrib import admin # Register your m ...

  3. UVA10905 Children's Game

    题意:给定n个正整数,把它们连接成一个最大的整数.比如,123,124,556,90有24种连接方法,最大的结果为9 056 124 123. 贪心.一开始就想用string水过.注意不能直接用str ...

  4. TZ_16_Vue的v-model和v-on

    1.v-model是双向绑定,视图(View)和模型(Model)之间会互相影响. 既然是双向绑定,一定是在视图中可以修改数据,这样就限定了视图的元素类型.目前v-model的可使用元素有: inpu ...

  5. vim 简明教程(转自飘过的小牛)

    vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...

  6. spring cloud深入学习(六)-----熔断监控Hystrix Dashboard和Turbine

    Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数 ...

  7. 关于 LVM

    [名词解释] 1. PV(Physical Volume):物理卷,处于LVM最底层,可以是物理硬盘或者分区.     2.PP(Physical Extend):物理区域,PV中可以用于分配的最小存 ...

  8. 洛谷 P2356 弹珠游戏

    题目链接:https://www.luogu.org/problemnew/show/P2356 题目 题目描述 MedalPluS 和他的小伙伴 NOIRP 发掘了一个骨灰级别的游戏——超级弹珠. ...

  9. 二维vector基本使用

    变量声明 vector<vector<int> > 变量名: 添加行 vector<vector<int> > v2d; for(int i=0;i&l ...

  10. Hdu 4251 区间中位数(划分树)

    题目链接 The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/3276 ...