date.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * 日期时间格式化
  3. * @param {date} time 需要转换的时间
  4. * @param {String} fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss
  5. */
  6. export function formatTime(time, fmt) {
  7. if (!time) return ''
  8. else {
  9. // time=time.replace(/\-/g,'\/')
  10. const date = typeof (time) === 'object' ? ('format' in time ? time.toDate() : time) : new Date(time)
  11. if (date.getFullYear() == 1753 && date.getMonth() + 1 == 1 && date.getDate() == 1) {
  12. return ''
  13. }
  14. const o = {
  15. 'M+': date.getMonth() + 1,
  16. 'd+': date.getDate(),
  17. 'H+': date.getHours(),
  18. 'm+': date.getMinutes(),
  19. 's+': date.getSeconds(),
  20. 'q+': Math.floor((date.getMonth() + 3) / 3),
  21. S: date.getMilliseconds(),
  22. }
  23. if (/(y+)/.test(fmt))
  24. fmt = fmt.replace(
  25. RegExp.$1,
  26. (date.getFullYear() + '').substr(4 - RegExp.$1.length)
  27. )
  28. for (const k in o) {
  29. if (new RegExp('(' + k + ')').test(fmt)) {
  30. fmt = fmt.replace(
  31. RegExp.$1,
  32. RegExp.$1.length === 1 ?
  33. o[k] :
  34. ('00' + o[k]).substr(('' + o[k]).length)
  35. )
  36. }
  37. }
  38. return fmt
  39. }
  40. }
  41. // 日期筛选器 05-28
  42. export function formatDayDate(time) {
  43. return formatTime(time, 'yy-MM-dd')
  44. }
  45. // 日期筛选器 2020-05-28
  46. export function formatDate(time) {
  47. return formatTime(time, 'yyyy-MM-dd')
  48. }
  49. // 日期时间筛选器 2020-05-28 20:30
  50. export function formatDateTime(time) {
  51. return formatTime(time, 'yyyy-MM-dd HH:mm:ss')
  52. }
  53. // 日期时间筛选器 2020-05-28 20:30
  54. export function formatDateMinute(time) {
  55. return formatTime(time, 'yyyy-MM-dd HH:mm')
  56. }
  57. /**
  58. * 格式化毫秒时间
  59. */
  60. export function formatMilliSeconds(time) {
  61. let span = time / 1000.0
  62. let seconds = parseInt(span % 60.0)
  63. span = parseInt(span / 60.0)
  64. let minutes = parseInt(span % 60.0)
  65. span = parseInt(span / 60.0)
  66. let hours = parseInt(span % 24.0)
  67. let days = parseInt(span / 24.0)
  68. let res = ''
  69. if (days > 0)
  70. res += days + '天'
  71. if (hours > 0)
  72. res += hours + '时'
  73. if (minutes > 0)
  74. res += minutes + '分'
  75. if (seconds > 0)
  76. res += seconds + '秒'
  77. return res
  78. }
  79. export function getWeekDataList(data) {
  80. //根据日期获取本周周一~周日的年-月-日
  81. var weekList = []
  82. var date = new Date(data)
  83. //判断本日期是否为周日,获取本周一日期
  84. if(date.getDay()=='0'){
  85. date.setDate(date.getDate() -6)
  86. }else {
  87. date.setDate(date.getDate() - date.getDay() + 1)
  88. }
  89. var myDate=date.getDate()
  90. var myMonth=date.getMonth() + 1
  91. if(date.getDate()<10){
  92. myDate= '0'+ myDate
  93. }
  94. if(date.getMonth() + 1<10){
  95. myMonth='0'+myMonth
  96. }
  97. weekList.push(date.getFullYear() + '-' + myMonth+ '-' + myDate)
  98. // 获取周二以后日期
  99. for(var i=0;i<6;i++) {
  100. date.setDate(date.getDate() + 1)
  101. myDate=date.getDate()
  102. myMonth=date.getMonth() + 1
  103. if(date.getDate()<10){
  104. myDate= '0'+ myDate
  105. }
  106. if(date.getMonth() + 1<10){
  107. myMonth='0'+myMonth
  108. }
  109. weekList.push(date.getFullYear() + '-' + myMonth+ '-' +myDate)
  110. }
  111. console.log(weekList)
  112. return weekList
  113. }