118 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			118 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
| //该方法用于给日期、时间补零
 | |
| function addZero(num) {
 | |
|   if (parseInt(num) < 10) {
 | |
|     num = '0' + num
 | |
|   }
 | |
|   return num
 | |
| }
 | |
| //把毫秒数转化成具体日期   2021-06-04
 | |
| //参数 毫秒数
 | |
| function formatMsToDate(ms) {
 | |
|   if (ms) {
 | |
|     var oDate = new Date(ms),
 | |
|       oYear = oDate.getFullYear(),
 | |
|       oMonth = oDate.getMonth() + 1,
 | |
|       oDay = oDate.getDate(),
 | |
|       // oHour = oDate.getHours(),
 | |
|       // oMin = oDate.getMinutes(),
 | |
|       // oSen = oDate.getSeconds(),
 | |
|       oTime = oYear + '-' + addZero(oMonth) + '-' + addZero(oDay)
 | |
|     return oTime
 | |
|   } else {
 | |
|     return ''
 | |
|   }
 | |
| }
 | |
| //判断值是否为空
 | |
| function isEmpty(v) {
 | |
|   switch (typeof v) {
 | |
|     case 'undefined':
 | |
|       return true
 | |
|     case 'string':
 | |
|       if (v.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true
 | |
|       break
 | |
|     case 'boolean':
 | |
|       if (!v) return true
 | |
|       break
 | |
|     case 'number':
 | |
|       if (0 === v || isNaN(v)) return true
 | |
|       break
 | |
|     case 'object':
 | |
|       if (null === v || v.length === 0) return true
 | |
|       for (var i in v) {
 | |
|         return false
 | |
|       }
 | |
|       return true
 | |
|   }
 | |
|   return false
 | |
| }
 | |
| function formatSeconds(second_time) {
 | |
|   var time = parseInt(second_time) + '天'
 | |
|   if (parseInt(second_time) >= 60) {
 | |
|     var second = parseInt(second_time) % 60
 | |
|     var min = parseInt(second_time / 60)
 | |
|     if (second == 0) {
 | |
|       // time = min + "分";
 | |
|     } else {
 | |
|       // time = min + "分" + second + "秒";
 | |
|     }
 | |
|     if (min > 60) {
 | |
|       min = parseInt(second_time / 60) % 60
 | |
|       var hour = parseInt(parseInt(second_time / 60) / 60)
 | |
|       if (second == 0) {
 | |
|         // time = hour + "小时";
 | |
|       } else {
 | |
|         // time = hour + "小时";
 | |
|       }
 | |
|       if (hour > 24) {
 | |
|         hour = parseInt(parseInt(second_time / 60) / 60) % 24
 | |
|         var day = parseInt(parseInt(parseInt(second_time / 60) / 60) / 24)
 | |
|         if (second == 0) {
 | |
|           time = day + '天'
 | |
|         } else {
 | |
|           time = day + '天'
 | |
|         }
 | |
|       }
 | |
|     }
 | |
|   } else {
 | |
|     time = time
 | |
|   }
 | |
|   return time
 | |
| }
 | |
| 
 | |
| function formatCurrency(value) {
 | |
|   // 处理空值或无效值
 | |
|   if (value === null || value === undefined || value === '') {
 | |
|     return value
 | |
|   }
 | |
| 
 | |
|   // 转换为数字类型
 | |
|   const numValue = typeof value === 'string' ? parseFloat(value) : value
 | |
| 
 | |
|   // 验证是否为有效数字
 | |
|   if (isNaN(numValue)) {
 | |
|     return value
 | |
|   }
 | |
| 
 | |
|   // 截断到两位小数(不四舍五入)
 | |
|   const truncated = Math.floor(numValue * 100) / 100
 | |
| 
 | |
|   // 添加千分位分隔符的函数
 | |
|   function addThousandSeparator(num) {
 | |
|     return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
 | |
|   }
 | |
| 
 | |
|   // 判断是否有小数部分
 | |
|   if (truncated % 1 === 0) {
 | |
|     // 整数,不显示小数位,但添加千分位分隔符
 | |
|     return addThousandSeparator(truncated)
 | |
|   } else {
 | |
|     // 有小数,保留两位,并为整数部分添加千分位分隔符
 | |
|     const fixedValue = truncated.toFixed(2)
 | |
|     const parts = fixedValue.split('.')
 | |
|     parts[0] = addThousandSeparator(parts[0])
 | |
|     return parts.join('.')
 | |
|   }
 | |
| }
 | |
| 
 | |
| export { formatCurrency, formatMsToDate, formatSeconds, isEmpty }
 |