export const throttle = ( func, delay) => {
let prev = Date. now ( ) ;
return function ( ) {
const context = this ;
const args = arguments;
const now = Date. now ( ) ;
if ( now - prev >= delay) {
func . apply ( context, args) ;
prev = Date. now ( ) ;
}
} ;
} ;
funcName: throttle ( function ( ) {
} , time)
export const debounce = ( fn, delay, scope) => {
let timer = null ;
return function ( ) {
let context = scope || this ,
args = arguments;
clearTimeout ( timer) ;
timer = setTimeout ( function ( ) {
fn . apply ( context, args) ;
} , delay) ;
} ;
} ;
funcName: debounce ( function ( ) {
} , time)
export const fmoney = ( num) => {
if ( num) {
let amount = num. split ( '.' ) [ 0 ] ;
if ( amount && amount. length >= 4 ) {
amount = amount. split ( '' ) ;
amount. splice ( - 3 , 0 , ',' ) ;
amount = amount. join ( '' ) ;
let ss = num. split ( '.' ) [ 1 ] || '' ;
ss = ss != '' ? '.' + ss : '' ;
return amount + ss;
} else {
return num;
}
}
return '' ;
} ;
fmoney ( amount)
export const trim = ( str = '' ) => {
try {
str = ` ${ str} ` ;
let type = typeof str;
let result = str. replace ( / (^\s*)|(\s*$) / g , "" ) ;
return type === 'string' ? result : type === 'number' ? Number ( str) : str;
} catch ( e) {
return str;
}
} ;
trim ( str)
export const getLast1Month = ( time= new Date ( ) ) => {
var now = new Date ( time) ;
var year = now. getFullYear ( ) ;
var month = now. getMonth ( ) + 1 ;
var day = now. getDate ( ) ;
var dateObj = { } ;
dateObj. now = year + '-' + ( month< 10 ? '0' + month: month) + '-' + ( day< 10 ? '0' + day: day) ;
var nowMonthDay = new Date ( year, month, 0 ) . getDate ( ) ;
if ( month - 1 <= 0 ) {
dateObj. last = ( year - 1 ) + '-' + 12 + '-' + day;
} else {
var lastMonthDay = new Date ( year, ( parseInt ( month) - 1 ) , 0 ) . getDate ( ) ;
let newDay = ( lastMonthDay < day) ? ( day < nowMonthDay) ? ( lastMonthDay - ( nowMonthDay - day) ) : lastMonthDay: day
dateObj. last = year + '-' + ( ( month - 1 ) < 10 ? '0' + ( month - 1 ) : ( month - 1 ) ) + '-' + ( newDay< 10 ? '0' + newDay: newDay) ;
}
return dateObj
}
export const getLast3Month = ( time= new Date ( ) ) => {
var now = new Date ( time) ;
var year = now. getFullYear ( ) ;
var month = now. getMonth ( ) + 1 ;
var day = now. getDate ( ) ;
var dateObj = { } ;
dateObj. now = year + '-' + ( month< 10 ? '0' + month: month) + '-' + ( day< 10 ? '0' + day: day) ;
if ( parseInt ( month) === 1 ) {
dateObj. last = ( parseInt ( year) - 1 ) + '-10-' + ( day< 10 ? '0' + day: day) ;
return dateObj;
}
if ( parseInt ( month) === 2 ) {
dateObj. last = ( parseInt ( year) - 1 ) + '-11-' + ( day< 10 ? '0' + day: day) ;
return dateObj;
}
if ( parseInt ( month) === 3 ) {
dateObj. last = ( parseInt ( year) - 1 ) + '-12-' + ( day< 10 ? '0' + day: day) ;
return dateObj;
}
var preSize = new Date ( year, parseInt ( month) - 3 , 0 ) . getDate ( ) ;
if ( preSize < parseInt ( day) ) {
let resultMonth = parseInt ( month) - 2 < 10 ? ( '0' + parseInt ( month) - 2 ) : ( parseInt ( month) - 2 ) ;
dateObj. last = year + '-' + resultMonth + '-01' ;
return dateObj;
}
if ( parseInt ( month) <= 10 ) {
dateObj. last = year + '-0' + ( parseInt ( month) - 3 ) + '-' + ( day< 10 ? '0' + day: day) ;
return dateObj;
} else {
dateObj. last = year + '-0' + ( parseInt ( month) - 3 ) + '-' + ( day< 10 ? '0' + day: day) ;
return dateObj;
}
}
export const getLast6Month = ( time= new Date ( ) ) => {
var now = new Date ( time) ;
var year = now. getFullYear ( ) ;
var month = now. getMonth ( ) + 1 ;
var day = now. getDate ( ) ;
var dateObj = { } ;
dateObj. now = year + '-' + ( month< 10 ? '0' + month: month) + '-' + ( day< 10 ? '0' + day: day) ;
var nowMonthDay = new Date ( year, month, 0 ) . getDate ( ) ;
var last3MonthDay = ''
if ( month - 6 <= 0 ) {
last3MonthDay = new Date ( ( year - 1 ) , ( 12 - ( 6 - parseInt ( month) ) ) , 0 ) . getDate ( ) ;
let month_6 = ( 12 - ( 6 - month) ) < 10 ? '0' + ( 12 - ( 6 - month) ) : ( 12 - ( 6 - month) )
if ( last3MonthDay > day) {
dateObj. last = ( year - 1 ) + '-' + month_6 + '-' + ( day< 10 ? '0' + last3MonthDay: last3MonthDay) ;
} else {
dateObj. last = ( year - 1 ) + '-' + month_6 + '-' + ( day< 10 ? '0' + day: day) ;
}
} else {
last3MonthDay = new Date ( year, ( parseInt ( month) - 6 ) , 0 ) . getDate ( ) ;
const month_6 = ( month - 6 ) < 10 ? '0' + ( month - 6 ) : ( month - 6 )
if ( last3MonthDay < day) {
if ( day < nowMonthDay) {
dateObj. last = year + '-' + month_6 + '-' + ( ( last3MonthDay - ( nowMonthDay - day) ) < 10 ? '0' + ( last3MonthDay - ( nowMonthDay - day) ) : ( last3MonthDay - ( nowMonthDay - day) ) ) ;
} else {
dateObj. last = year + '-' + month_6 + '-' + ( last3MonthDay< 10 ? '0' + last3MonthDay: last3MonthDay) ;
}
} else {
dateObj. last = year + '-' + month_6 + '-' + ( day< 10 ? '0' + day: day) ;
}
}
return dateObj
}
export const timeDifference = ( date, startDate) => {
if ( ! ( date instanceof Date ) )
return undefined ;
let nowDate = startDate || new Date ( )
date. setHours ( 0 ) ;
date. setMinutes ( 0 ) ;
date. setSeconds ( 0 ) ;
nowDate. setHours ( 0 ) ;
nowDate. setMinutes ( 0 ) ;
nowDate. setSeconds ( 0 ) ;
let timeDis = date - nowDate * 1
let millisDay = 1000 * 60 * 60 * 24
return Math. ceil ( timeDis / millisDay) ;
}
export const getDateBetween = ( start, end) => {
let result = [ ] ;
const startTime = new Date ( start) ;
const endTime = new Date ( end) ;
while ( endTime - startTime >= 0 ) {
let year = startTime. getFullYear ( ) ;
let month = startTime. getMonth ( ) ;
month = month< 9 ? '0' + ( month+ 1 ) : month+ 1 ;
let day = startTime. getDate ( ) . toString ( ) . length == 1 ? "0" + startTime. getDate ( ) : startTime. getDate ( ) ;
result. push ( year + "-" + month + "-" + day) ;
startTime. setDate ( startTime. getDate ( ) + 1 ) ;
}
return result;
}