util.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //获取系统的时间
  2. var days = []
  3. const formatNumber = n => {
  4. n = n.toString()
  5. return n[1] ? n : '0' + n
  6. }
  7. //当前时间-数组模式 年月日 时分秒
  8. const nowDateTime = date => {
  9. const year = date.getFullYear()
  10. const month = date.getMonth() + 1
  11. const day = date.getDate()
  12. const hour = date.getHours()
  13. const minute = date.getMinutes()
  14. const second = date.getSeconds()
  15. const nowTime = [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')//整合时间
  16. const time = {
  17. 'year': year, 'month': month, 'day': day, 'hour': hour, 'minute': minute, 'second': second, 'nowTime': nowTime
  18. }
  19. return time
  20. }
  21. //当前时间-数组模式 年月日
  22. const nowDate = date => {
  23. const year = date.getFullYear()
  24. const month = date.getMonth() + 1
  25. const day = date.getDate()
  26. return [year, month, day].map(formatNumber).join('-')
  27. }
  28. // 从 todate 开始计算 days 天的日期数组
  29. function dateArry(todate,days) {
  30. var dateArry = [];
  31. for (var i = 0; i < days; i++) {
  32. var dateObj = dateLater(todate, i);
  33. dateArry.push(dateObj)
  34. }
  35. return dateArry;
  36. }
  37. function dateLater(dates, later) {
  38. let dateObj = {};
  39. let day_week = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
  40. let day_index = new Array(7, 1, 2, 3, 4, 5, 6);
  41. let date = new Date(dates);
  42. date.setDate(date.getDate() + later);
  43. let day = date.getDay();
  44. let yearDate = date.getFullYear();
  45. let month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
  46. let dayFormate = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
  47. days = days.concat(dayFormate)
  48. dateObj.date = yearDate + '-' + month + '-' + dayFormate;
  49. dateObj.week = day_week[day];
  50. dateObj.num = day_index[day];
  51. dateObj.day = days[day];
  52. return dateObj;
  53. }
  54. //往后/前推几天的日期
  55. function delayDate(today, addDayCount) {
  56. var dd;
  57. if (today) {
  58. dd = new Date(today);
  59. } else {
  60. dd = new Date();
  61. }
  62. dd.setDate(dd.getDate() + addDayCount);//获取addDayCount天后的日期
  63. var y = dd.getFullYear();
  64. var m = dd.getMonth() + 1;//获取当前月份的日期
  65. var d = dd.getDate();
  66. if (m < 10) {
  67. m = '0' + m;
  68. };
  69. if (d < 10) {
  70. d = '0' + d;
  71. };
  72. var newDay = y + '-' + m + '-' + d
  73. return newDay;
  74. }
  75. //当前周的数组
  76. function nowWeek(day) {
  77. var nowWeekArry = {}
  78. , nowDate = day //当前日期
  79. , num = dateArry(nowDate,1)[0].num //当前周几
  80. , monday = delayDate(nowDate, -(num - 1)) //本周一的 日期
  81. , sunday = delayDate(nowDate, 7 - num) //本周日的 日期
  82. , nowWeek = dateArry(monday,7) //本周列表
  83. console.log(num)
  84. nowWeekArry = {
  85. 'nowDate': nowDate //当前日期
  86. , 'num': num //当前周几
  87. , 'nowWeek': nowWeek //本周列表
  88. }
  89. return nowWeekArry
  90. }
  91. //一周的几号重组
  92. function weekData(weekData) {
  93. var weekData = weekData
  94. for (let i = 0; i < weekData.length; i++) {
  95. var day = weekData[i].date
  96. weekData[i].day = day.substr(8, 2)
  97. }
  98. return weekData;
  99. }
  100. //日期转整型
  101. function date(date) {
  102. var a = parseInt(date.substr(0, 4) + date.substr(5, 2) + date.substr(8, 2))
  103. return a;
  104. }
  105. function toFix(value) {
  106. return value.toFixed(2) // 此处2为保留两位小数,保留几位小数,这里写几
  107. }
  108. module.exports = {
  109. nowDateTime: nowDateTime //年月日 时分秒
  110. , nowDate: nowDate //年月日
  111. , dateArry: dateArry // 从 todate 开始计算 days 天的日期数组
  112. , delayDate: delayDate //往后/前推几天的日期
  113. , date: date //日期转整型
  114. , weekData: weekData //一周的几号重组
  115. , nowWeek: nowWeek
  116. , toFix: toFix
  117. //,box: box
  118. }