import {stringIsBlank} from "@/utils/blank-utils"; import moment from "moment"; export function getDate() { const date = new Date() const year = date.getFullYear() let month = date.getMonth() + 1 let day = date.getDate() return year + '-' + ('0' + month).slice(-2) + '-' + ('0' + day).slice(-2) } export function getDatetime() { const now = new Date() const year = now.getFullYear() const month = now.getMonth() + 1 const day = now.getDate() const hh = now.getHours() const mm = now.getMinutes() const ss = now.getSeconds() let clock = year + '-' if (month < 10) clock += '0' clock += month + '-' if (day < 10) clock += '0' clock += day + ' ' if (hh < 10) clock += '0' clock += hh + ':' if (mm < 10) clock += '0' clock += mm + ':' if (ss < 10) clock += '0' clock += ss return clock } export function compareDate(date1, date2) { const oDate1 = new Date(date1); const oDate2 = new Date(date2); return oDate1.getTime() > oDate2.getTime(); } export function huanHangXianShi(date) { let clock = date.split(" ") return clock[0] + '
' + clock[1] } export function formatDatetime(date) { if (typeof date === 'undefined') return null if (date === '' || date === null) return null if (typeof date === 'string') return date const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hh = date.getHours() const mm = date.getMinutes() const ss = date.getSeconds() let clock = year + '-' if (month < 10) clock += '0' clock += month + '-' if (day < 10) clock += '0' clock += day + ' ' if (hh < 10) clock += '0' clock += hh + ':' if (mm < 10) clock += '0' clock += mm + ':' if (ss < 10) clock += '0' clock += ss return clock } export function formatDate(date) { if (typeof date === 'undefined') return null if (date === '' || date === null) return null if (typeof date === 'string') return date const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() return year + '-' + ('0' + month).slice(-2) + '-' + ('0' + day).slice(-2) } export function getOneMonthOffset() { const myDate = new Date() let year = myDate.getFullYear() let month = myDate.getMonth() let date = myDate.getDate() let end = year + '-' + ('0' + (month + 1)).slice(-2) + '-' + ('0' + date).slice(-2) if (month === 0) { year -= 1 month = 12 } else if (month === 2) { date = date > 28 ? 28 : date } else { date = date > 30 ? 30 : date } let start = year + '-' + ('0' + month).slice(-2) + '-' + ('0' + date).slice(-2) return {start, end} } export function getDateRangeFormatDate(data) { let startTime = '' let endTime = '' //用户手动输入了日期 并且没有按下大键盘上面的回车 if (data === null || data.length === 0) return {startTime, endTime} if (typeof data[0].$d !== 'undefined') { startTime = formatDate(data[0].$d) + ' 00:00:00' endTime = formatDate(data[1].$d) + ' 23:59:59' } else if (data !== null) { startTime = formatDate(data[0]) + ' 00:00:00' endTime = formatDate(data[1]) + ' 23:59:59' } return {startTime, endTime} } export function getDateRangeFormatDateTime(date) { let startTime = '' let endTime = '' if (!date) return {startTime, endTime} if (!date[0] || !date[1]) return {startTime, endTime} //用户手动输入了日期 并且没有按下大键盘上面的回车 if (!date[0].$d) { startTime = formatDatetime(date[0].$d) endTime = formatDatetime(date[1].$d) } else { startTime = formatDatetime(date[0]) endTime = formatDatetime(date[1]) } return {startTime, endTime} } export function formatMonth(date) { if (typeof date === 'undefined') return null if (date === '' || date === null) return null if (typeof date === 'string') return date const year = date.getFullYear() const month = date.getMonth() + 1 return year + '-' + ('0' + month).slice(-2) } /** * 格式化日期 * @param date 日期 * @param pattern 格式 * @returns {string} 返回格式好的日期 */ export function getFormatDatetime(date, pattern) { if (stringIsBlank(pattern)) { pattern = "YYYY-MM-DD HH:mm:ss" } if (stringIsBlank(date)) { return "" } return moment(date).format(pattern) }