util.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. function formatTimeTwo(number, format) {
  11. var formateArr = ['Y', 'M', 'D', 'H', 'M', 'S'];
  12. var returnArr = [];
  13. var date = new Date(number * 1000);
  14. returnArr.push(date.getFullYear());
  15. returnArr.push(formatNumber(date.getMonth() + 1));
  16. returnArr.push(formatNumber(date.getDate()));
  17. returnArr.push(formatNumber(date.getHours()));
  18. returnArr.push(formatNumber(date.getMinutes()));
  19. returnArr.push(formatNumber(date.getSeconds()));
  20. for (var i in returnArr) {
  21. format = format.replace(formateArr[i], returnArr[i]);
  22. }
  23. return format;
  24. }
  25. const formatMonth = date => {
  26. const year = date.getFullYear()
  27. const month = date.getMonth() +1
  28. return `${[year, month].map(formatNumber).join('-')} `
  29. }
  30. const formatDay = date =>{
  31. const year = date.getFullYear()
  32. const month = date.getMonth() +1
  33. const day = date.getDate()
  34. return `${[year, month,day].map(formatNumber).join('-')} `
  35. }
  36. const formatNumber = n => {
  37. n = n.toString()
  38. return n[1] ? n : `0${n}`
  39. }
  40. const timestamp = n =>{
  41. return Date.parse(new Date(n))/1000
  42. }
  43. function formatArr(val) {
  44. var special_confArr = val.toString(2).split('').slice(0, val.toString(2).length - 1).reverse();
  45. return getInital(special_confArr)
  46. }
  47. function getInital(arr) {
  48. var result = []
  49. for (var i = 0; i < arr.length; i++) {
  50. if (arr[i] == 1) {
  51. result.push(i + 1 + '')
  52. }
  53. }
  54. return result
  55. }
  56. module.exports = {
  57. formatTime,
  58. formatMonth,
  59. timestamp,
  60. formatDay,
  61. formatArr,
  62. formatTimeTwo
  63. }