123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- /**
- * 日期时间格式化
- * @param {date} time 需要转换的时间
- * @param {String} fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss
- */
- export function formatTime(time, fmt) {
- if (!time) return ''
- else {
- // time=time.replace(/\-/g,'\/')
- const date = typeof (time) === 'object' ? ('format' in time ? time.toDate() : time) : new Date(time)
- if (date.getFullYear() == 1753 && date.getMonth() + 1 == 1 && date.getDate() == 1) {
- return ''
- }
- const o = {
- 'M+': date.getMonth() + 1,
- 'd+': date.getDate(),
- 'H+': date.getHours(),
- 'm+': date.getMinutes(),
- 's+': date.getSeconds(),
- 'q+': Math.floor((date.getMonth() + 3) / 3),
- S: date.getMilliseconds(),
- }
- if (/(y+)/.test(fmt))
- fmt = fmt.replace(
- RegExp.$1,
- (date.getFullYear() + '').substr(4 - RegExp.$1.length)
- )
- for (const k in o) {
- if (new RegExp('(' + k + ')').test(fmt)) {
- fmt = fmt.replace(
- RegExp.$1,
- RegExp.$1.length === 1 ?
- o[k] :
- ('00' + o[k]).substr(('' + o[k]).length)
- )
- }
- }
- return fmt
- }
- }
- // 日期筛选器 05-28
- export function formatDayDate(time) {
- return formatTime(time, 'yy-MM-dd')
- }
- // 日期筛选器 2020-05-28
- export function formatDate(time) {
- return formatTime(time, 'yyyy-MM-dd')
- }
- // 日期时间筛选器 2020-05-28 20:30
- export function formatDateTime(time) {
- return formatTime(time, 'yyyy-MM-dd HH:mm:ss')
- }
- // 日期时间筛选器 2020-05-28 20:30
- export function formatDateMinute(time) {
- return formatTime(time, 'yyyy-MM-dd HH:mm')
- }
- /**
- * 格式化毫秒时间
- */
- export function formatMilliSeconds(time) {
- let span = time / 1000.0
- let seconds = parseInt(span % 60.0)
- span = parseInt(span / 60.0)
- let minutes = parseInt(span % 60.0)
- span = parseInt(span / 60.0)
- let hours = parseInt(span % 24.0)
- let days = parseInt(span / 24.0)
- let res = ''
- if (days > 0)
- res += days + '天'
- if (hours > 0)
- res += hours + '时'
- if (minutes > 0)
- res += minutes + '分'
- if (seconds > 0)
- res += seconds + '秒'
- return res
- }
- export function getWeekDataList(data) {
- //根据日期获取本周周一~周日的年-月-日
- var weekList = []
- var date = new Date(data)
- //判断本日期是否为周日,获取本周一日期
- if(date.getDay()=='0'){
- date.setDate(date.getDate() -6)
- }else {
- date.setDate(date.getDate() - date.getDay() + 1)
- }
- var myDate=date.getDate()
- var myMonth=date.getMonth() + 1
- if(date.getDate()<10){
- myDate= '0'+ myDate
- }
- if(date.getMonth() + 1<10){
- myMonth='0'+myMonth
- }
- weekList.push(date.getFullYear() + '-' + myMonth+ '-' + myDate)
- // 获取周二以后日期
- for(var i=0;i<6;i++) {
- date.setDate(date.getDate() + 1)
- myDate=date.getDate()
- myMonth=date.getMonth() + 1
- if(date.getDate()<10){
- myDate= '0'+ myDate
- }
- if(date.getMonth() + 1<10){
- myMonth='0'+myMonth
- }
- weekList.push(date.getFullYear() + '-' + myMonth+ '-' +myDate)
- }
- console.log(weekList)
- return weekList
- }
|