util.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * 工具类
  3. */
  4. class Util {
  5. static parseDate(date){
  6. var time = date==null?"": Date.parse(date.replace(/-/g, '/'));
  7. return new Date(time);
  8. }
  9. static formatTime(date) {
  10. let year = date.getFullYear();
  11. let month = date.getMonth() + 1;
  12. let day = date.getDate();
  13. let hour = date.getHours();
  14. let minute = date.getMinutes();
  15. let second = date.getSeconds();
  16. return [year, month, day].map(this.formatNumber).join('/') + ' ' + [hour, minute, second].map(this.formatNumber).join(':');
  17. };
  18. static formatNumber(n) {
  19. n = n.toString();
  20. return n[1] ? n : '0' + n;
  21. };
  22. static distance = (la1, lo1, la2, lo2) => {
  23. var La1 = la1 * Math.PI / 180.0;
  24. var La2 = la2 * Math.PI / 180.0;
  25. var La3 = La1 - La2;
  26. var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
  27. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
  28. s = s * 6378.137; //地球半径
  29. s = Math.round(s * 1000) / 1000;
  30. // console.log("计算结果",s)
  31. return s
  32. }
  33. };
  34. module.exports = Util;