util.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. function formatTime(time) {
  2. if (typeof time !== 'number' || time < 0) {
  3. return time
  4. }
  5. var hour = parseInt(time / 3600)
  6. time = time % 3600
  7. var minute = parseInt(time / 60)
  8. time = time % 60
  9. var second = time
  10. return ([hour, minute, second]).map(function(n) {
  11. n = n.toString()
  12. return n[1] ? n : '0' + n
  13. }).join(':')
  14. }
  15. function formatDateTime(date, fmt = 'yyyy-MM-dd hh:mm:ss') {
  16. if(!date) {
  17. return ''
  18. }
  19. if (typeof (date) === 'number') {
  20. date = new Date(date * 1000)
  21. }
  22. var o = {
  23. "M+": date.getMonth() + 1, //月份
  24. "d+": date.getDate(), //日
  25. "h+": date.getHours(), //小时
  26. "m+": date.getMinutes(), //分
  27. "s+": date.getSeconds(), //秒
  28. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  29. "S": date.getMilliseconds() //毫秒
  30. }
  31. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length))
  32. for (var k in o)
  33. if (new RegExp("(" + k + ")").test(fmt))
  34. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)))
  35. return fmt
  36. }
  37. function formatLocation(longitude, latitude) {
  38. if (typeof longitude === 'string' && typeof latitude === 'string') {
  39. longitude = parseFloat(longitude)
  40. latitude = parseFloat(latitude)
  41. }
  42. longitude = longitude.toFixed(2)
  43. latitude = latitude.toFixed(2)
  44. return {
  45. longitude: longitude.toString().split('.'),
  46. latitude: latitude.toString().split('.')
  47. }
  48. }
  49. var dateUtils = {
  50. UNITS: {
  51. '年': 31557600000,
  52. '月': 2629800000,
  53. '天': 86400000,
  54. '小时': 3600000,
  55. '分钟': 60000,
  56. '秒': 1000
  57. },
  58. humanize: function(milliseconds) {
  59. var humanize = '';
  60. for (var key in this.UNITS) {
  61. if (milliseconds >= this.UNITS[key]) {
  62. humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
  63. break;
  64. }
  65. }
  66. return humanize || '刚刚';
  67. },
  68. format: function(dateStr) {
  69. var date = this.parse(dateStr)
  70. var diff = Date.now() - date.getTime();
  71. if (diff < this.UNITS['天']) {
  72. return this.humanize(diff);
  73. }
  74. var _format = function(number) {
  75. return (number < 10 ? ('0' + number) : number);
  76. };
  77. return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
  78. _format(date.getHours()) + ':' + _format(date.getMinutes());
  79. },
  80. parse: function(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  81. var a = str.split(/[^0-9]/);
  82. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  83. }
  84. };
  85. const hexToRgba = (hex, opacity) => { //16进制颜色转rgba
  86. return "rgba(" + parseInt("0x" + hex.slice(1, 3)) + "," + parseInt("0x" + hex.slice(3, 5)) + "," + parseInt("0x" + hex.slice(5, 7)) + "," + opacity + ")"
  87. }
  88. module.exports = {
  89. formatTime,
  90. formatDateTime,
  91. formatLocation,
  92. dateUtils,
  93. hexToRgba
  94. }