index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import convert from '../convert'
  2. export default {
  3. getDateRange(n = 30, endn = 0) {
  4. // 获取当前日期
  5. var myDate = new Date(Date.now() - 1000 * 60 * 60 * 24 * endn)
  6. var nowY = myDate.getFullYear()
  7. var nowM = myDate.getMonth() + 1
  8. var nowD = myDate.getDate()
  9. var nowH = myDate.getHours() < 10 ? '0' + myDate.getHours() : myDate.getHours()
  10. var nowMi = myDate.getMinutes() < 10 ? '0' + myDate.getMinutes() : myDate.getMinutes()
  11. var nowS = myDate.getSeconds() < 10 ? '0' + myDate.getSeconds() : myDate.getSeconds()
  12. var enddate = nowY + '-' + (nowM < 10 ? '0' + nowM : nowM) + '-' + (nowD < 10 ? '0' + nowD : nowD) + ' ' + nowH + ':' + nowMi + ':' + nowS
  13. // 获取三十天前日期
  14. var lw = new Date(myDate - 1000 * 60 * 60 * 24 * n)
  15. var lastY = lw.getFullYear()
  16. var lastM = lw.getMonth() + 1
  17. var lastD = lw.getDate()
  18. var lastH = lw.getHours() < 10 ? '0' + lw.getHours() : lw.getHours()
  19. var lastMi = lw.getMinutes() < 10 ? '0' + lw.getMinutes() : lw.getMinutes()
  20. var lastS = lw.getSeconds() < 10 ? '0' + lw.getSeconds() : lw.getSeconds()
  21. var startdate = lastY + '-' + (lastM < 10 ? '0' + lastM : lastM) + '-' + (lastD < 10 ? '0' + lastD : lastD) + ' ' + lastH + ':' + lastMi + ':' + lastS
  22. return { startdate, enddate }
  23. },
  24. getFullFileUrl(url) {
  25. return url ? process.env.VUE_APP_FILE_URL + url : ''
  26. },
  27. isTypeof(option) {
  28. var value = Object.prototype.toString.call(option)
  29. if (value === '[object Undefined]') return 'undefined'
  30. else if (value === '[object String]') return 'string'
  31. else if (value === '[object Boolean]') return 'boolean'
  32. else if (value === '[object Number]') return 'number'
  33. else if (value === '[object Null]') return 'null'
  34. else if (value === '[object Object]') return 'object'
  35. else if (value === '[object Array]') return 'array'
  36. else if (value === '[object Date]') return 'date'
  37. else if (value === '[object RegExp]') return 'regexp'
  38. else if (value === '[object Function]') return 'function'
  39. },
  40. formateTData(date, fmt) { // 字符串
  41. if (date) {
  42. if (fmt == 'date') {
  43. return this.formatDate(date, 'yyyy-MM-dd')
  44. } else if (fmt == 'all') {
  45. return this.formatDate(date, 'yyyy-MM-dd hh:mm:ss')
  46. } else if (fmt == 'hour') {
  47. return this.formatDate(date, 'yyyy-MM-dd hh')
  48. } else if (fmt == 'time') {
  49. return this.formatDate(date, 'hh:mm')
  50. } else {
  51. return this.formatDate(date, 'yyyy-MM-dd hh:mm')
  52. }
  53. }
  54. return ''
  55. },
  56. formatDate(date, fmt = 'yyyy-MM-dd hh:mm:ss') { // date对象
  57. if (convert.isNull(date)) return date
  58. var padLeftZero = function (str) {
  59. return ('00' + str).substr(str.length)
  60. }
  61. if (this.isTypeof(date) === 'string') {
  62. let tmpDate = date.replace(/-/g, '/')
  63. tmpDate = tmpDate.replace(/T/g, ' ')
  64. date = new Date(tmpDate)
  65. } else if (this.isTypeof(date) == 'number') {
  66. date = new Date(date)
  67. }
  68. if (/(y+)/.test(fmt)) {
  69. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  70. }
  71. const o = {
  72. 'M+': date.getMonth() + 1,
  73. 'd+': date.getDate(),
  74. 'h+': date.getHours(),
  75. 'm+': date.getMinutes(),
  76. 's+': date.getSeconds()
  77. }
  78. for (var k in o) {
  79. if (new RegExp(`(${k})`).test(fmt)) {
  80. var str = o[k] + ''
  81. fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
  82. }
  83. }
  84. return fmt
  85. }
  86. }