TimeUtils.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import moment from 'moment';
  2. export default class Time {
  3. constructor(time = new Date(), format = '') {
  4. this.time = moment(time, format);
  5. }
  6. getNowTime(format) {
  7. return moment(this.time.format('X') - moment().format('X'), 'X').format(
  8. format
  9. );
  10. }
  11. addTime(time, formatType) {
  12. return this.time.add(time, 'hours').format(formatType);
  13. }
  14. nextList() {
  15. const time2 = this.time.add(1, 'hours');
  16. const hour = Number(time2.format('H'));
  17. const list = [];
  18. for (let i = hour; i < 24; i++) {
  19. if (hour === i) {
  20. const min = Number(time2.format('s'));
  21. const minList = [];
  22. if (min <= 0) {
  23. minList.push(0);
  24. }
  25. if (min <= 15) {
  26. minList.push(15);
  27. }
  28. if (min <= 30) {
  29. minList.push(30);
  30. }
  31. if (min <= 45) {
  32. minList.push(45);
  33. }
  34. minList.forEach((item) => {
  35. list.push(moment(`${i}:${item}`, 'H:m').format('HH:mm'));
  36. });
  37. } else {
  38. [0, 15, 30, 45].forEach((item) => {
  39. list.push(moment(`${i}:${item}`, 'H:m').format('HH:mm'));
  40. });
  41. }
  42. }
  43. return list;
  44. }
  45. getFormat(formatType) {
  46. return this.time.format(formatType);
  47. }
  48. }