date-util.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. const oneMinute = 1000 * 60; // MilliseConds in a Minute
  2. const oneHour = oneMinute * 60; // The milliseconds of one hour
  3. const oneDay = oneHour * 24; // The Number of MilliseConds A Day
  4. const oneWeek = oneDay * 7; // The milliseconds per week
  5. const oneMonth = oneDay * 30; // The milliseconds of a month
  6. /**
  7. * Decrease according to the number of days
  8. *
  9. * @param days to reduce the number of days to be reduced
  10. */
  11. Date.prototype.minusDays = function (days) {
  12. return this.minusMillis(oneDay * days);
  13. };
  14. /**
  15. * Increase according to the number of days
  16. *
  17. * @param days The number of days to be increased
  18. */
  19. Date.prototype.plusDays = function (days) {
  20. return this.plusMillis(oneDay * days);
  21. };
  22. /**
  23. * A few
  24. *
  25. * @param hours to be reduced
  26. */
  27. Date.prototype.minusHours = function (hours) {
  28. return this.minusMillis(oneHour * hours);
  29. };
  30. /**
  31. * Increase hourly
  32. *
  33. * @param hours to increase the number of hours
  34. */
  35. Date.prototype.plusHours = function (hours) {
  36. return this.plusMillis(oneHour * hours);
  37. };
  38. /**
  39. * Make reduction in minutes
  40. *
  41. * @param minutes to reduce the number of minutes
  42. */
  43. Date.prototype.minusMinutes = function (minutes) {
  44. return this.minusMillis(oneMinute * minutes);
  45. };
  46. /**
  47. * Add in minutes
  48. *
  49. * @param minutes to increase the number of minutes
  50. */
  51. Date.prototype.plusMinutes = function (minutes) {
  52. return this.plusMillis(oneMinute * minutes);
  53. };
  54. /**
  55. * Decrease in milliseconds
  56. *
  57. * @param millis to reduce the milliseconds
  58. */
  59. Date.prototype.minusMillis = function(millis) {
  60. let time = this.getTime() - millis;
  61. let newDate = new Date();
  62. newDate.setTime(time);
  63. return newDate;
  64. };
  65. /**
  66. * Add in milliseconds to increase
  67. *
  68. * @param millis to increase the milliseconds to increase
  69. */
  70. Date.prototype.plusMillis = function(millis) {
  71. let time = this.getTime() + millis;
  72. let newDate = new Date();
  73. newDate.setTime(time);
  74. return newDate;
  75. };
  76. /**
  77. * Setting time is 00: 00: 00.000 on the day
  78. */
  79. Date.prototype.setMinTime = function () {
  80. this.setHours(0);
  81. this.setMinutes(0);
  82. this.setSeconds(0);
  83. this.setMilliseconds(0);
  84. return this;
  85. };
  86. /**
  87. * Setting time is 23: 59: 59.999 on the same day
  88. */
  89. Date.prototype.setMaxTime = function () {
  90. this.setHours(23);
  91. this.setMinutes(59);
  92. this.setSeconds(59);
  93. this.setMilliseconds(999);
  94. return this;
  95. };
  96. /**
  97. * Formatting date
  98. */
  99. Date.prototype.formatDate = function () {
  100. return this.getFullYear() + "-" + addZero(this.getMonth() + 1) + "-" + addZero(this.getDate());
  101. };
  102. /**
  103. * Format time
  104. */
  105. Date.prototype.formatTime = function () {
  106. return addZero(this.getHours()) + ":" + addZero(this.getMinutes()) + ":" + addZero(this.getSeconds());
  107. };
  108. /**
  109. * Formatting date plus time
  110. *
  111. * @param split Date and time separation symbols, default is a space
  112. */
  113. Date.prototype.formatDateTime = function (split = ' ') {
  114. return this.formatDate() + split + this.formatTime();
  115. };
  116. class DateUtil {
  117. // String string to date object
  118. static parseDate(str) {
  119. return new Date(str.replace(/-/g, '/'));
  120. }
  121. static formatMillis(millis) {
  122. return moment(millis).format('YYYY-M-D H:m:s');
  123. }
  124. static firstDayOfMonth() {
  125. const date = new Date();
  126. date.setDate(1);
  127. date.setMinTime();
  128. return date;
  129. }
  130. }