123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- var WEEK = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
- function datePolyfill(val) {
- return val < 10 ? '0' + val : val
- }
- /**
- * 时间戳解析
- * @param ts 单位:ms
- * @param type
- * @returns {string}
- */
- function formatTs(ts, type) {
- if (isNaN(ts) || !ts) {
- return ''
- }
- var date = getDate(ts)
- var obj = {
- YYYY: date.getFullYear(),
- MM: datePolyfill(date.getMonth() + 1),
- DD: datePolyfill(date.getDate()),
- HH: datePolyfill(date.getHours()),
- mm: datePolyfill(date.getMinutes()),
- ss: datePolyfill(date.getSeconds()),
- week: WEEK[date.getDay()]
- }
- if (type === 'hh:mm:ss') {
- return obj.HH + ':' + obj.mm + ':' + obj.ss
- }
- if (type === 'MM.DD hh:mm') {
- return obj.MM + '.' + obj.DD + ' ' + obj.HH + ':' + obj.mm
- }
- if (type === 'YYYY.MM.DD hh:mm week') {
- return obj.YYYY + '.' + obj.MM + '.' + obj.DD + ' ' + obj.HH + ':' + obj.mm + ' ' + obj.week
- }
- if (type === 'YYYY-MM-DD hh:mm') {
- return obj.YYYY + '-' + obj.MM + '-' + obj.DD + ' ' + obj.HH + ':' + obj.mm
- }
- if (type === 'YYYY-MM-DD') {
- return obj.YYYY + '-' + obj.MM + '-' + obj.DD
- }
- if (type === 'hh:mm zh') {
- return obj.HH + '小时' + obj.mm + '分'
- }
- }
- function formatNumber(n) {
- n = n.toString()
- return n[1] ? n : '0' + n
- }
- function cutDownTime(ts, type) {
- if (ts < 0) {
- return ['0', '00', '00', '00']
- }
- ts = parseInt(ts)
- var D = Math.floor(ts / 24 / 60 / 60)
- // 小时位
- var h = Math.floor((ts - D * 24 * 60 * 60) / 3600)
- // 分钟位
- var m = Math.floor((ts - D * 24 * 3600 - h * 3600) / 60)
- // 秒位
- var s = ts - D * 24 * 3600 - h * 3600 - m * 60
- if (type === 'hh:mm:ss') {
- return formatNumber(D * 24 + h) + ':' + formatNumber(m) + ':' + formatNumber(s)
- }
- if (type === 'hh:mm zh') {
- return (D * 24 + h) + '小时' + m + '分'
- }
- return [D, formatNumber(h), formatNumber(m), formatNumber(s)]
- }
- function computedFormOrderCarCount(list) {
- var result = 0
- for (var i = 0; i < list.length; i++) {
- result += list[i].car_num
- }
- return result
- }
- function fen2Yuan(num) {
- return isNaN(num) ? '' : (num * 0.01).toFixed(2) * 1
- }
- function yuan2Fen(num) {
- if (isNaN(num)) {
- return ''
- }
- var amount = num.toString()
- var index = amount.indexOf('.')
- var arr = amount.split('.')
- var result = arr[0] * 100
- if (index > -1) {
- var temp = arr[1].split('')
- for (var i = 0; i < temp.length; i++) {
- if (i === 0) {
- result += temp[i] * 10
- } else {
- result += temp[i] * 1
- }
- }
- }
- return result
- }
- function fen2YuanAndJiao(num) {
- if (isNaN(num)) {
- return ''
- }
- var temp = (num * 0.01).toFixed(2) * 1
- var val = temp.toString().split('.')
- return {
- yuan: val[0],
- jiao: val[1] ? '.' + val[1] : ''
- }
- }
- function mToKm(distance) {
- if (isNaN(distance)) {
- return ''
- }
- return Math.ceil(distance / 1000) + 'KM'
- }
- function imgFilter(src) {
- return src.indexOf('http') > -1
- ? src
- : 'https://bashi-1311374120.cos.ap-shanghai.myqcloud.com/wxMini/image' + src
- }
- function formatProductLable(str) {
- if (!str) {
- return []
- }
- return str.split(',')
- }
- module.exports = {
- formatTs: formatTs,
- computedFormOrderCarCount: computedFormOrderCarCount,
- fen2Yuan: fen2Yuan,
- yuan2Fen: yuan2Fen,
- fen2YuanAndJiao: fen2YuanAndJiao,
- mToKm: mToKm,
- cutDownTime: cutDownTime,
- imgFilter: imgFilter,
- formatProductLable: formatProductLable
- }
|