util.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 formatTime2 = (time,type) => {
  16. var date = new Date(time)
  17. var format = type || 'YYYY-MM-DD HH:NN:SS';
  18. const year = date.getFullYear();
  19. const month = (date.getMonth() + 1)< 10 ? '0'+(date.getMonth() + 1) : date.getMonth() + 1;
  20. const day = date.getDate()<10 ? '0'+date.getDate() : date.getDate();
  21. const hour = date.getHours()<10 ? '0'+date.getHours() : date.getHours();
  22. const minute = date.getMinutes()<10 ? '0'+date.getMinutes() : date.getMinutes();
  23. const second = date.getSeconds()<10 ? '0'+date.getSeconds() : date.getSeconds();
  24. format.indexOf('YYYY') > -1 ? format = format.replace('YYYY',year) : '';
  25. format.indexOf('MM') > -1 ? format = format.replace('MM',month) : '';
  26. format.indexOf('DD') > -1 ? format = format.replace('DD',day) : '';
  27. format.indexOf('HH') > -1 ? format = format.replace('HH',hour) : '';
  28. format.indexOf('NN') > -1 ? format = format.replace('NN',minute) : '';
  29. format.indexOf('SS') > -1 ? format = format.replace('SS',second) : '';
  30. return format;
  31. }
  32. //获取登录code
  33. const getWxCode = () => {
  34. wx.login({
  35. success: res => {
  36. wx.setStorageSync('wxCode', res.code)
  37. }
  38. })
  39. }
  40. module.exports = {
  41. formatTime,
  42. formatTime2,getWxCode
  43. }