format.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /** *
  2. * 全局时间转换
  3. * @parmas time 时间戳默认为11位时间戳
  4. * @parmas all 为true时time为13位时间戳
  5. */
  6. function formatTime(time, all) {
  7. console.log(time)
  8. time = all ? time * 1000 : time
  9. if (typeof time !== 'number' || time < 0) {
  10. return time
  11. }
  12. var hour = parseInt(time / 3600)
  13. time = time % 3600
  14. var minute = parseInt(time / 60)
  15. time = time % 60
  16. var second = time
  17. return ([hour, minute, second]).map(function(n) {
  18. n = n.toString()
  19. return n[1] ? n : '0' + n
  20. }).join(':')
  21. }
  22. function formatLocation(longitude, latitude) {
  23. if (typeof longitude === 'string' && typeof latitude === 'string') {
  24. longitude = parseFloat(longitude)
  25. latitude = parseFloat(latitude)
  26. }
  27. longitude = longitude.toFixed(2)
  28. latitude = latitude.toFixed(2)
  29. return {
  30. longitude: longitude.toString().split('.'),
  31. latitude: latitude.toString().split('.')
  32. }
  33. }
  34. var dateUtils = {
  35. UNITS: {
  36. '年': 31557600000,
  37. '月': 2629800000,
  38. '天': 86400000,
  39. '小时': 3600000,
  40. '分钟': 60000,
  41. '秒': 1000
  42. },
  43. humanize: function(milliseconds) {
  44. var humanize = ''
  45. for (var key in this.UNITS) {
  46. if (milliseconds >= this.UNITS[key]) {
  47. humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前'
  48. break
  49. }
  50. }
  51. return humanize || '刚刚'
  52. },
  53. format: function(dateStr) {
  54. var date = this.parse(dateStr)
  55. var diff = Date.now() - date.getTime()
  56. if (diff < this.UNITS['天']) {
  57. return this.humanize(diff)
  58. }
  59. var _format = function(number) {
  60. return (number < 10 ? ('0' + number) : number)
  61. }
  62. return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDay()) + '-' +
  63. _format(date.getHours()) + ':' + _format(date.getMinutes())
  64. },
  65. parse: function(str) { // 将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  66. var a = str.split(/[^0-9]/)
  67. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5])
  68. }
  69. }
  70. /**
  71. * 格式化价格为两位小数
  72. * @param {number|string} price - 需要格式化的价格
  73. * @param {number} decimals - 小数位数,默认为2
  74. * @returns {string} 格式化后的价格字符串
  75. */
  76. function formatPrice(price, decimals = 2) {
  77. // 输入验证
  78. if (price === null || price === undefined || price === '') {
  79. return '0.00';
  80. }
  81. // 转换为数字
  82. const numPrice = typeof price === 'string' ? parseFloat(price) : price;
  83. // 检查是否为有效数字
  84. if (isNaN(numPrice)) {
  85. console.warn('formatPrice: Invalid price value:', price);
  86. return '0.00';
  87. }
  88. // 格式化为指定小数位数
  89. return numPrice.toFixed(decimals);
  90. }
  91. module.exports = {
  92. formatTime: formatTime,
  93. formatLocation: formatLocation,
  94. dateUtils: dateUtils,
  95. formatPrice: formatPrice
  96. }