TimeUtils.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. list.push(
  35. minList.map((item) => {
  36. return moment(`${i}:${item}`, 'H:m').format('HH:mm');
  37. })
  38. );
  39. } else {
  40. list.push(
  41. [0, 15, 30, 45].map((item) => {
  42. return moment(`${i}:${item}`, 'H:m').format('HH:mm');
  43. })
  44. );
  45. }
  46. }
  47. return list.flat();
  48. }
  49. getFormat(formatType){
  50. return this.time.format(formatType);
  51. }
  52. }