util.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const formatTime = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  9. }
  10. const formatNumber = n => {
  11. n = n.toString()
  12. return n[1] ? n : '0' + n
  13. }
  14. const formatChDate = date => {
  15. const year = date.getFullYear()
  16. const month = date.getMonth() + 1
  17. const day = date.getDate()
  18. const hour = date.getHours()
  19. const minute = date.getMinutes()
  20. return year + '年' + month + '月' + day + '日' + [hour, minute].map(formatNumber).join(':')
  21. }
  22. const formatChDate1 = date => {
  23. var year = date.getFullYear()
  24. var month = date.getMonth() + 1
  25. var day = date.getDate()
  26. var hour = date.getHours()
  27. var minute = date.getMinutes()
  28. if (month < 10) {
  29. month = '0' + month
  30. }
  31. if (day < 10) {
  32. day = '0' + day
  33. }
  34. return year + '-' + month + '-' + day
  35. }
  36. // 是否为空对象
  37. const isEmptyObject = obj => {
  38. var name
  39. for (name in obj) {
  40. return false
  41. }
  42. return true
  43. }
  44. module.exports = {
  45. formatTime: formatTime,
  46. formatChDate: formatChDate,
  47. isEmptyObject: isEmptyObject,
  48. formatChDate1: formatChDate1
  49. }