util.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * 日期时间格式化
  3. * 2018/01/01 23:59:59
  4. */
  5. const formatTime = date => {
  6. const year = date.getFullYear()
  7. const month = date.getMonth() + 1
  8. const day = date.getDate()
  9. const hour = date.getHours()
  10. const minute = date.getMinutes()
  11. const second = date.getSeconds()
  12. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  13. }
  14. /**
  15. * 日期时间格式化
  16. * 2018-01-01 23:59:59
  17. */
  18. const formatTime2 = date => {
  19. const year = date.getFullYear()
  20. const month = date.getMonth() + 1
  21. const day = date.getDate()
  22. const hour = date.getHours()
  23. const minute = date.getMinutes()
  24. const second = date.getSeconds()
  25. return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  26. }
  27. /**
  28. * 对个位数前补零
  29. */
  30. const formatNumber = n => {
  31. n = n.toString()
  32. return n[1] ? n : '0' + n
  33. }
  34. /**
  35. * 日期格式化
  36. * 2018-01-01
  37. */
  38. const formatDate = date => {
  39. const year = date.getFullYear()
  40. const month = date.getMonth() + 1
  41. const day = date.getDate()
  42. const hour = date.getHours()
  43. const minute = date.getMinutes()
  44. const second = date.getSeconds()
  45. return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  46. }
  47. /**
  48. * 时间格式化
  49. * 23:59:59
  50. */
  51. const formatTimeTwo = date => {
  52. const hour = date.getHours()
  53. const minute = date.getMinutes()
  54. const second = date.getSeconds()
  55. return [hour, minute, second].map(formatNumber).join(':')
  56. }
  57. /**
  58. * 路径格式化
  59. */
  60. const formatUrl = (url, obj) => {
  61. let temp = []
  62. let fn = (obj) => {
  63. let temp = []
  64. for (let i in obj) {
  65. if (obj[i] != null && typeof obj[i] == 'object') {
  66. temp.push(`${i}=${JSON.stringify(obj[i])}`)
  67. } else {
  68. temp.push(`${i}=${obj[i]}`)
  69. }
  70. }
  71. return temp.join('&')
  72. }
  73. return url.indexOf('?') < 0 ? url + '?' + fn(obj) : url + '&' + fn(obj)
  74. }
  75. /**
  76. * 函数节流和函数抖动。
  77. */
  78. function throttle(fn, gapTime) {
  79. if (gapTime == null || gapTime == undefined) {
  80. gapTime = 1500
  81. }
  82. let _lastTime = null
  83. // 返回新的函数
  84. return function () {
  85. let _nowTime = + new Date()
  86. if (_nowTime - _lastTime > gapTime || !_lastTime) {
  87. fn.apply(this, arguments) //将this和参数传给原函数
  88. _lastTime = _nowTime
  89. }
  90. }
  91. }
  92. module.exports = {
  93. formatTime,
  94. formatTime2,
  95. formatUrl,
  96. formatDate,
  97. formatTimeTwo,
  98. formatNumber,
  99. throttle
  100. }