util.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //获取年月日
  2. const formatTime = (date) => {
  3. const year = date.getFullYear()
  4. const month = date.getMonth() + 1
  5. const day = date.getDate()
  6. const hour = date.getHours()
  7. const minute = date.getMinutes()
  8. const second = date.getSeconds()
  9. return {
  10. year:year,
  11. month:[year, month].map(formatNumber).join('-'),
  12. day:[year, month, day].map(formatNumber).join('-'),
  13. month_tow:month,
  14. allDate:[year, month, day].map(formatNumber).join('-')+" "+hour+":"+minute
  15. }
  16. }
  17. const formatNumber = n => {
  18. n = n.toString()
  19. return n[1] ? n : '0' + n
  20. }
  21. //删除数组的某一项
  22. const arrRemoveObj = (array, obj) => {
  23. let length = array.length;
  24. for (let i = 0; i < length; i++) {
  25. if (array[i] === obj) {
  26. if (i === 0) {
  27. array.shift();
  28. return array;
  29. } else if (i === length - 1) {
  30. array.pop();
  31. return array;
  32. } else {
  33. array.splice(i, 1);
  34. return array;
  35. }
  36. }
  37. }
  38. }
  39. //获取对应积分类型
  40. const getTypeItem=(arr,id)=>{
  41. var item=arr.filter(element => {
  42. return typeof(id)=='string'?element.code==id:element.id==id
  43. });
  44. return item[0]
  45. }
  46. module.exports = {
  47. formatTime: formatTime,
  48. arrRemoveObj:arrRemoveObj,
  49. getTypeItem:getTypeItem
  50. }