Нет описания

util.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 formatDate = 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. const second = date.getSeconds()
  21. return year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second
  22. }
  23. const formatDateDay = date => {
  24. const year = date.getFullYear()
  25. let month = date.getMonth() + 1
  26. let day = date.getDate();
  27. if(month <= 9){
  28. month = '0' + month
  29. }
  30. if(day <= 9){
  31. day = '0' + day
  32. }
  33. return year+'-'+month+'-'+day
  34. }
  35. const getTimeLastWeek = (last,days) => {
  36. const year = last.getFullYear()
  37. const day = last.getDate()
  38. const ti = day - days
  39. // const month6 = last.getMonth() + 1
  40. // const dayOfWeek = last.getDay() //今天本周的第几天
  41. // 判断是否月初
  42. if (ti <= 0) {
  43. const month = last.getMonth() + 1 - 1
  44. const d = new Date(year, month, 0)
  45. const dayBig = d.getDate() //获取当月的所有天数
  46. const ti1 = dayBig + ti
  47. return [year, month, ti1].map(formatNumber).join('-')
  48. } else {
  49. const month = last.getMonth() + 1
  50. return [year, month, ti].map(formatNumber).join('-')
  51. }
  52. // return [year, month, day].map(formatNumber).join('-')
  53. }
  54. module.exports = {
  55. formatTime: formatTime,
  56. formatDate,
  57. formatDateDay,
  58. getTimeLastWeek
  59. }