timer.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * 时间插件
  3. */
  4. const timer = {
  5. // 小于10的数字前面补0
  6. to2: number => Number(number) < 10 ? `0${number}` : String(number),
  7. // 获取指定时间
  8. time (format = 'y-m-d', time = null){
  9. let date = time ? new Date(time) : new Date();
  10. format = format.replace('y', date.getFullYear());
  11. format = format.replace('m', this.to2(date.getMonth() + 1));
  12. format = format.replace('d', this.to2(date.getDate()));
  13. format = format.replace('h', this.to2(date.getHours()));
  14. format = format.replace('i', this.to2(date.getMinutes()));
  15. format = format.replace('s', this.to2(date.getSeconds()));
  16. format = format.replace('w', date.getDay());
  17. return format;
  18. },
  19. // 获取当前月份有多少天
  20. monthLength (time = null){
  21. let y = this.time('y', time) * 1;
  22. let m = this.to2(this.time('m', time) * 1 + 1);
  23. let unix = new Date(`${y}-${m}-01`).getTime() - 24 * 60 * 60 * 1000;
  24. return Number(this.time('d', unix));
  25. },
  26. // 获取上个月有多少天
  27. preMonthLenggth (time = null){
  28. let y = this.time('y', time) * 1;
  29. let m = this.to2(this.time('m', time) * 1);
  30. let unix = new Date(`${y}-${m}-01`).getTime() - 24 * 60 * 60 * 1000;
  31. return Number(this.time('d', unix));
  32. },
  33. // 获取下个月有多少天
  34. nextMonthLenggth (time = null){
  35. let y = this.time('y', time) * 1;
  36. let m = this.to2(this.time('m', time) * 1 + 2);
  37. let unix = new Date(`${y}-${m}-01`).getTime() - 24 * 60 * 60 * 1000;
  38. return Number(this.time('d', unix));
  39. },
  40. // 获取当月第一天
  41. monthFirst (time = null){
  42. let unix = this.time('y-m', time) + '-01';
  43. return this.time('y-m-d', unix);
  44. },
  45. // 获取当月最后一天
  46. monthLast (time = null){
  47. let y = this.time('y', time) * 1;
  48. let m = this.to2(this.time('m', time) * 1 + 1);
  49. let unix = new Date(`${y}-${m}-01`).getTime() - 24 * 60 * 60 * 1000;
  50. return this.time('y-m-d', unix);
  51. },
  52. // 获取上个月的今天
  53. preMonthToday (time = null){
  54. let y = this.time('y', time);
  55. let m = this.to2(this.time('m', time) * 1 - 1);
  56. let d = this.time('d', time);
  57. return this.time('y-m-d', `${y}-${m}-${d}`);
  58. },
  59. // 获取下个月的今天
  60. nextMonthToday (time = null){
  61. let y = this.time('y', time);
  62. let m = this.to2(this.time('m', time) * 1 + 1);
  63. let d = this.time('d', time);
  64. return this.time('y-m-d', `${y}-${m}-${d}`);
  65. },
  66. // 已经过去了多少时间
  67. timeout (time){
  68. },
  69. // 倒计时
  70. timeRemaining (time, format){
  71. time = time - parseInt(new Date().getTime()/1000);
  72. return this.unixToString(format, time);
  73. },
  74. // 时间戳的差值转化为时间字符串
  75. unixToString (format, time){
  76. let d = parseInt(time / 60 / 60 / 24);
  77. let h = parseInt((time - d * 86400) / 3600);
  78. let i = parseInt((time - d * 86400 - h * 3600) / 60);
  79. let s = time - d * 86400 - h * 3600 - i * 60;
  80. format = format.replace('d', timer.to2(timer.low(d)));
  81. format = format.replace('h', timer.to2(timer.low(h)));
  82. format = format.replace('i', timer.to2(timer.low(i)));
  83. format = format.replace('s', timer.to2(timer.low(s)));
  84. return format;
  85. },
  86. // 小于0的直接输出0
  87. low (num){
  88. return num < 0 ? 0 : num;
  89. },
  90. // 把日期转成对象
  91. parse (string){
  92. return new Date(string);
  93. }
  94. }
  95. export default timer;