util.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // 设置时间文案
  15. const setDuration = (s) => {
  16. let t = '';
  17. s = Math.floor(s);
  18. if (s > -1) {
  19. let min = Math.floor(s / 60) % 60;
  20. let sec = s % 60;
  21. if (min < 10) {
  22. t += "0";
  23. }
  24. t += min + ":";
  25. if (sec < 10) {
  26. t += "0";
  27. }
  28. t += sec;
  29. }
  30. return t
  31. }
  32. /**
  33. * 将时间戳(1570550400)格式转为 yyyy-MM-dd格式
  34. */
  35. //时间戳转时间
  36. function formatDate(time, flag) {
  37. if (!time) {
  38. return
  39. }
  40. var time = time * 1
  41. var t = new Date(time);
  42. var tf = function (i) {
  43. return (i < 10 ? '0' : '') + i
  44. };
  45. var year = t.getFullYear();
  46. var month = tf(t.getMonth() + 1);
  47. var day = tf(t.getDate());
  48. var hour = tf(t.getHours());
  49. var minute = tf(t.getMinutes());
  50. if (flag == 1) {
  51. return year + '-' + month + '-' + day + '' + ' ' + hour + ':' + minute;
  52. } else if (flag == 2) {
  53. return year + '.' + month + '.' + day
  54. } else if (flag == 3) {
  55. return month + '-' + day + ' ' + hour + ':' + minute;
  56. } else if (flag == 4) {
  57. return year + '年' + month + '月' + day + '日';
  58. } else if (flag == 5) {
  59. return year + '-' + month + '-' + day;
  60. } else if (flag == 5) {
  61. return year + '-' + month + '-' + day;
  62. } else if (flag == 6) {
  63. return year + '-' + month;
  64. }
  65. }
  66. module.exports = {
  67. formatDate,
  68. formatTime,
  69. setDuration
  70. }