mock平台

util.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * 获取所需要的日期区间点
  3. * @param time {Number} Number是ele日期区间选择组件返回的结果
  4. * Number是之前时刻距离今天的间隔天数,默认是90天
  5. * @param start {String} 日期对象,日期区间的开始点 '2017-01-17 00:00:00'
  6. * @param withToday {Boolean} 是否包含今天
  7. * @return {Array} ['2017-01-17 00:00:00', '2017-01-20 23:59:59']
  8. */
  9. exports.getDateRange = (time = 90, start = false, withToday = true) => {
  10. const gapTime = time * 24 * 3600 * 1000;
  11. if (!start) {
  12. // 没有规定start时间
  13. let endTime = getNowMidnightDate().getTime();
  14. if (!withToday) {
  15. endTime -= 86400000;
  16. }
  17. return [this.formatYMD(endTime - gapTime), this.formatYMD(endTime - 1000)];
  18. }
  19. const startTime = dateSpacialWithSafari(start);
  20. const endTime = startTime + (gapTime - 1000);
  21. return [start, this.formatYMD(endTime)];
  22. }
  23. // 时间
  24. const convert2Decimal = num => (num > 9 ? num : `0${num}`)
  25. /**
  26. * 获取距今天之前多少天的所有时间
  27. * @param time {Number} Number是ele日期区间选择组件返回的结果
  28. * Number是之前时刻距离今天的间隔天数,默认是30天
  29. * @return {Array} ['2017-01-17', '2017-01-28', '2017-10-29',...]
  30. */
  31. exports.getDateInterval = (time = 30) => {
  32. // const gapTime = time * 24 * 3600 * 1000;
  33. // 今天
  34. let endTime = new Date().getTime();
  35. let timeList = []
  36. for (let i = 0; i < time; i++) {
  37. const gapTime = i * 24 * 3600 * 1000;
  38. const time = this.formatYMD(endTime - gapTime);
  39. timeList.push(time);
  40. }
  41. return timeList;
  42. }
  43. /**获取2017-10-27 00:00:00 和 2017-10-27 23:59:59的时间戳
  44. * @param date {String} "2017-10-27"
  45. * @return {Array} [ 1509033600000, 1509119999000 ]
  46. */
  47. exports.getTimeInterval = (date) => {
  48. const startTime = (getNowMidnightDate(date).getTime()-86400000)/1000;
  49. const endTime =(getNowMidnightDate(date).getTime()-1000)/1000;
  50. return [startTime, endTime];
  51. }
  52. /**
  53. * 获取当前时间午夜0点的日期对象
  54. */
  55. const getNowMidnightDate = (time) => {
  56. let date;
  57. if (time) {
  58. date = new Date(time);
  59. } else {
  60. date = new Date();
  61. }
  62. return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);
  63. }
  64. /**
  65. * 格式化 年、月、日、时、分、秒
  66. * @param val {Object or String or Number} 日期对象 或是可new Date的对象或时间戳
  67. * @return {String} 2017-01-20 20:00:00
  68. */
  69. const formatDate = val => {
  70. let date = val;
  71. if (typeof val !== 'object') {
  72. date = new Date(val);
  73. }
  74. return `${[
  75. date.getFullYear(),
  76. convert2Decimal(date.getMonth() + 1),
  77. convert2Decimal(date.getDate())
  78. ].join('-')} ${[
  79. convert2Decimal(date.getHours()),
  80. convert2Decimal(date.getMinutes()),
  81. convert2Decimal(date.getSeconds())
  82. ].join(':')}`;
  83. }
  84. /**
  85. * 格式化年、月、日
  86. * @param val {Object or String or Number} 日期对象 或是可new Date的对象或时间戳
  87. * @return {String} 2017-01-20
  88. */
  89. exports.formatYMD = (val, joinStr = '-') => {
  90. let date = val;
  91. if (typeof val !== 'object') {
  92. date = new Date(val);
  93. }
  94. return `${[
  95. date.getFullYear(),
  96. convert2Decimal(date.getMonth() + 1),
  97. convert2Decimal(date.getDate())
  98. ].join(joinStr)}`;
  99. }
  100. /**
  101. * 获取所需的时间差值,
  102. * tip:new Date('2017-01-17 00:00:00')在safari下不可用,需进行替换
  103. * @param Array ['2017-01-17 00:00:00', '2017-01-20 23:59:59']
  104. * @return {Number} 3
  105. */
  106. exports.getDayGapFromRange = dateRange => {
  107. const startTime = dateSpacialWithSafari(dateRange[0]);
  108. const endTime = dateSpacialWithSafari(dateRange[1]);
  109. return Math.ceil((endTime - startTime) / 86400000);
  110. }
  111. /**
  112. * dateSpacialWithSafari 格式话safari下通用的格式
  113. * @param str {String} 2017-04-19T11:01:19.074+0800 or 2017-10-10 10:10:10
  114. * @return {number} date.getTime()
  115. */
  116. const dateSpacialWithSafari = str => {
  117. if (str.indexOf('T') > -1) {
  118. let date;
  119. str.replace(/(\d{4})-(\d{2})-(\d{2})\w(\d{2}):(\d{2}):(\d{2})/, (match, p1, p2, p3, p4, p5, p6) => {
  120. date = new Date(p1, +p2 - 1, p3, p4, p5, p6);
  121. return;
  122. })
  123. return date.getTime();
  124. }
  125. return new Date(str.replace(/-/g, '/')).getTime();
  126. }
  127. /**
  128. * 将内存单位从字节(b)变成GB
  129. */
  130. exports.transformBytesToGB = bytes => {
  131. return (bytes/1024/1024/1024).toFixed(2)
  132. }
  133. exports.transformSecondsToDay = seconds => {
  134. return (seconds/3600/24).toFixed(2)
  135. }