1. 获取几天后的日期
getEndDay ( startDate, days ) {
let nDate = new Date ( startDate) . getTime ( )
let millSeconds = Math. abs ( nDate) + days * 24 * 60 * 60 * 1000
return XEUtils. toDateString ( millSeconds)
} ,
2. 遍历出两个日期之间的月份
export function getMonthIn ( start, end ) {
const startYear = parseInt ( start. split ( '-' ) [ 0 ] )
const startMonth = parseInt ( start. split ( '-' ) [ 1 ] )
const endYear = parseInt ( end. split ( '-' ) [ 0 ] )
const endMonth = parseInt ( end. split ( '-' ) [ 1 ] )
let list = [ ]
const yearNum = endYear - startYear
if ( yearNum === 0 ) {
let num = startMonth
while ( num <= endMonth) {
list. push ( {
month : startYear + '-' + num
} )
num++
}
}
if ( yearNum >= 1 ) {
let num = startMonth
while ( num <= 12 ) {
list. push ( {
month : startYear + '-' + num
} )
num++
}
let num2 = endMonth
while ( num2 > 0 ) {
list. push ( {
month : endYear + '-' + num2
} )
num2--
}
if ( yearNum >= 2 ) {
let i = startYear + 1
while ( i < endYear) {
let j = 1
while ( j <= 12 ) {
list. push ( {
month : i + '-' + j
} )
j++
}
i++
}
}
}
list. sort ( ( a, b ) => new Date ( a. month) . getTime ( ) - new Date ( b. month) . getTime ( ) )
return list
}
3. 解决小数相加、相减的精度问题
export function accountAdd ( arg1, arg2 ) {
let r1, r2, m
try {
r1 = arg1. toString ( ) . split ( '.' ) [ 1 ] . length
} catch ( e) {
r1 = 0
}
try {
r2 = arg2. toString ( ) . split ( '.' ) [ 1 ] . length
} catch ( e) {
r2 = 0
}
m = Math. pow ( 10 , Math. max ( r1, r2) )
return Math. round ( arg1 * m + arg2 * m) / m
}
export const accountSub = ( arg1, arg2 ) => {
let r1, r2, m, n
try {
r1 = arg1. toString ( ) . split ( '.' ) [ 1 ] . length
} catch ( e) {
r1 = 0
}
try {
r2 = arg2. toString ( ) . split ( '.' ) [ 1 ] . length
} catch ( e) {
r2 = 0
}
m = Math. pow ( 10 , Math. max ( r1, r2) )
n = r1 >= r2 ? r1 : r2
return ( ( arg1 * m - arg2 * m) / m) . toFixed ( n)
}
4. 金额转成大写
export function moneyToCaptital ( num ) {
let minus = ''
let m_str = ''
const text = num. toString ( )
const zh = '零壹贰叁肆伍陆柒捌玖'
if ( text. indexOf ( '-' ) === 0 ) {
num = text. replace ( '-' , '' )
minus = '负'
}
let money_num = Number ( num)
let money = Math. round ( money_num * 100 ) . toString ( 10 )
const len = money. length
for ( let i = 0 ; i < len; i++ ) {
m_str = m_str + zh. charAt ( parseInt ( money. charAt ( i) ) ) + sitToUnit ( len - i - 1 )
}
m_str = m_str. replace ( '零分' , '' )
m_str = m_str. replace ( '零角' , '零' )
let yy = 0
while ( true ) {
let len = m_str. length
m_str = m_str. replace ( '零元' , '元' )
m_str = m_str. replace ( '零万' , '万' )
m_str = m_str. replace ( '零亿' , '亿' )
m_str = m_str. replace ( '零仟' , '零' )
m_str = m_str. replace ( '零佰' , '零' )
m_str = m_str. replace ( '零零' , '零' )
m_str = m_str. replace ( '零拾' , '零' )
m_str = m_str. replace ( '亿万' , '亿零' )
m_str = m_str. replace ( '万仟' , '万零' )
m_str = m_str. replace ( '仟佰' , '仟零' )
yy = m_str. length
if ( yy === len) {
break
}
}
yy = m_str. length
if ( m_str. charAt ( yy - 1 ) === '零' ) {
m_str = m_str. substring ( 0 , yy - 1 )
}
yy = m_str. length
if ( m_str. charAt ( yy - 1 ) === '元' ) {
m_str = m_str
}
return minus + m_str
}
export function sitToUnit ( a ) {
if ( a > 10 ) {
a = a - 8
return sitToUnit ( a)
}
const zh = '分角元拾佰仟万拾佰仟亿'
return zh. charAt ( a)
}
5. 英文全角转换为半角
export function ToCDB ( str ) {
let result = ''
for ( var i = 0 ; i < str. length; i++ ) {
let code = str. charCodeAt ( i)
if ( code >= 65281 && code <= 65374 ) {
result += String. fromCharCode ( str. charCodeAt ( i) - 65248 )
} else if ( code == 12288 ) {
result += String. fromCharCode ( str. charCodeAt ( i) - 12288 + 32 )
} else {
result += str. charAt ( i)
}
}
return result
}