1、时间格式化:含有秒
export const formatTime = (date) => { var year = date.getFullYear() var month = date.getMonth() + 1 var day = date.getDate() var hour = date.getHours() var minute = date.getMinutes() var second = date.getSeconds() return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second};
2、日期格式化 例如:2019年04月09日
export const formatTimeText = (date) => { var time = new Date(date.replace(/-/g, "/")) var year = time.getFullYear() var month = time.getMonth() + 1 var day = time.getDate() return year + ' 年 ' + month + ' 月 ' + day + ' 日'};
日期加减法 date参数为计算开始的日期,days为需要减的天数,格式:minusDate('20179-04-09',20) 日期加'0'
export const minusDate =(date,days) => { var d=new Date(date); var tian = d.setDate(d.getDate() - days); var m=d.getMonth()+1; return d.getFullYear()+'-'+m+'-'+d.getDate();}
日期格式化 例如:2019-04-09
export const formatDate = (day) => { var Year = 0; var Month = 0; var Day = 0; var CurrentDate = ''; //初始化时间 Year = day.getFullYear();//ie火狐下都可以 Month = day.getMonth() + 1; Day = day.getDate(); CurrentDate += Year + '-'; if (Month >= 10) { CurrentDate += Month + '-'; } else { CurrentDate += '0' + Month + '-'; } if (Day >= 10) { CurrentDate += Day; } else { CurrentDate += '0' + Day; } return CurrentDate;};
日期计算小时数
export const calcRemainderTime = (time) => { if (time <= 0) { return '0小时' } var day = Math.floor(time / 24) var hour = time % 24 time = (day === 0 ? time + '小时' : day + '天' + (hour === 0 ? '' : hour + '小时')) return time}
默认七天
export const default7Day = () =>{ let now = formatDate(new Date()) let defaultEndDate = minusDate(now,1) let defaultStartDate = minusDate(now,7) let dateObj = { beginDate: defaultStartDate, endDate: defaultEndDate } return dateObj;}
两个时间相差天数
/** * 两个时间相差天数 * sDate1和sDate2是2012-12-12格式 * 开始日期:00:00:00 * 结束日期:23:59:59 */export const dateDifference = (sDate1, sDate2) => { var dateSpan, tempDate, iDays; sDate1 = Date.parse(sDate1); sDate2 = Date.parse(sDate2); dateSpan = sDate2 - sDate1; dateSpan = Math.abs(dateSpan); iDays = Math.floor(dateSpan / (24 * 3600 * 1000)); return iDays + 1};