Rap 原分销系统代码Web
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

217 строки
5.4KB

  1. /* eslint-disable */
  2. /**
  3. * Date对象的补充函数,包括类似Python中的strftime()
  4. * 阿债 https://gitee.com/azhai/datetime.js
  5. */
  6. Date.prototype.toMidnight = function() {
  7. this.setHours(0)
  8. this.setMinutes(0)
  9. this.setSeconds(0)
  10. this.setMilliseconds(0)
  11. return this
  12. }
  13. Date.prototype.daysAgo = function(days, midnight) {
  14. days = days ? days - 0 : 0
  15. const date = new Date(this.getTime() - days * 8.64E7)
  16. return midnight ? date.toMidnight() : date
  17. }
  18. Date.prototype.monthBegin = function(offset) {
  19. offset = offset ? offset - 0 : 0
  20. const days = this.getDate() - 1 - offset
  21. return this.daysAgo(days, true)
  22. }
  23. Date.prototype.quarterBegin = function() {
  24. const month = this.getMonth() - this.getMonth() % 3
  25. return new Date(this.getFullYear(), month, 1).toMidnight()
  26. }
  27. Date.prototype.yearBegin = function() {
  28. return new Date(this.getFullYear(), 0, 1).toMidnight()
  29. }
  30. Date.prototype.strftime = function(format, local) {
  31. if (!format) {
  32. const str = new Date(this.getTime() + 2.88E7).toISOString()
  33. return str.substr(0, 16).replace('T', ' ')
  34. }
  35. local = local && local.startsWith('zh') ? 'zh' : 'en'
  36. const padZero = function(str, len) {
  37. const pads = len - str.toString().length
  38. return (pads && pads > 0 ? '0'.repeat(pads) : '') + str
  39. }
  40. format = format.replace('%F', '%Y-%m-%d')
  41. format = format.replace(/%D|%x/, '%m/%d/%y')
  42. format = format.replace(/%T|%X/, '%H:%M:%S')
  43. format = format.replace('%R', '%H:%M')
  44. format = format.replace('%r', '%H:%M:%S %p')
  45. format = format.replace('%c', '%a %b %e %H:%M:%S %Y')
  46. const _this = this
  47. return format.replace(/%[A-Za-z%]/g, function(f) {
  48. let ans = f
  49. switch (f) {
  50. case '%%':
  51. ans = '%'
  52. break
  53. case '%Y':
  54. case '%G':
  55. ans = _this.getFullYear()
  56. break
  57. case '%y':
  58. ans = _this.getFullYear() % 100
  59. break
  60. case '%C':
  61. ans = _this.getFullYear() / 100
  62. break
  63. case '%m':
  64. case '%n':
  65. ans = _this.getMonth() + 1
  66. break
  67. case '%B':
  68. local = local.startsWith('en') ? 'english' : local
  69. case '%b':
  70. const m = _this.getMonth()
  71. ans = local_labels.monthes[local][m]
  72. break
  73. case '%d':
  74. case '%e':
  75. ans = _this.getDate()
  76. break
  77. case '%j':
  78. ans = _this.getDaysOfYear()
  79. break
  80. case '%U':
  81. case '%W':
  82. const ws = _this.getWeeksOfYear(f === '%W')
  83. ans = padZero(ws, 2)
  84. break
  85. case '%w':
  86. ans = _this.getDay()
  87. case '%u':
  88. ans = ans === 0 ? 7 : ans
  89. break
  90. case '%A':
  91. local = local.startsWith('en') ? 'english' : local
  92. case '%a':
  93. const d = _this.getDay()
  94. ans = local_labels.weekdays[local][d]
  95. break
  96. case '%H':
  97. case '%k':
  98. ans = _this.getHours()
  99. break
  100. case '%I':
  101. case '%l':
  102. ans = _this.getHours()
  103. ans = ans % 12
  104. break
  105. case '%M':
  106. ans = _this.getMinutes()
  107. break
  108. case '%S':
  109. ans = _this.getSeconds()
  110. break
  111. case '%s':
  112. ans = parseInt(_this.getTime() / 1E3)
  113. break
  114. case '%f':
  115. const ms = _this.getMilliseconds()
  116. ans = padZero(ms * 1E3, 6)
  117. break
  118. case '%P':
  119. local = local.startsWith('en') ? 'english' : local
  120. case '%p':
  121. const h = _this.getHours()
  122. ans = local_labels.meridians[local][h < 12 ? 0 : 1]
  123. break
  124. case '%z':
  125. let tzo = _this.getTimezoneOffset()
  126. const sign = tzo < 0 ? '-' : '+'
  127. tzo = Math.abs(tzo)
  128. const ho = padZero(tzo / 60, 2)
  129. const mo = padZero(tzo % 60, 2)
  130. ans = sign + ho + mo
  131. break
  132. default:
  133. break
  134. }
  135. if (f === '%C' || f === '%y' || f === '%m' || f === '%d' || f === '%H' || f === '%M' || f === '%S') {
  136. ans = padZero(ans, 2)
  137. }
  138. return ans.toString()
  139. })
  140. }
  141. Date.prototype.humanize = function(local) {
  142. local = local && local.startsWith('zh') ? 'zh' : 'en'
  143. const result = this.strftime('', local)
  144. const days = (Date.today() - this.toMidnight().getTime()) / 8.64E7
  145. if (days <= -10 || days >= 10) {
  146. return result
  147. }
  148. const labels = local_labels.dayagos[local]
  149. let lbl = ''
  150. if (days === 0 || days === 1) {
  151. lbl = labels[days]
  152. } else if (days === -1) {
  153. lbl = labels[2]
  154. } else if (days >= 2) {
  155. lbl = days + labels[3]
  156. } else {
  157. lbl = days + labels[4]
  158. }
  159. return lbl + result.substr(10, 6)
  160. }
  161. const local_labels = {
  162. monthes: {
  163. english: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  164. en: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  165. zh: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
  166. },
  167. weekdays: {
  168. english: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  169. en: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
  170. zh: ['日', '一', '二', '三', '四', '五', '六']
  171. },
  172. meridians: {
  173. english: ['a.m.', 'p.m.'],
  174. en: ['AM', 'PM'],
  175. zh: ['上午', '下午']
  176. },
  177. dayagos: {
  178. english: ['Today', 'Yesterday', 'Tomorrow', ' days ago', ' days late'],
  179. en: ['Today', 'Yesterday', 'Tomorrow', ' days ago', ' days late'],
  180. zh: ['今天', '昨天', '明天', '天前', '天后']
  181. }
  182. }
  183. export default Date