123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- 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] + '<br />' + 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 getRawMonthOffset() {
- const myDate = new Date()
- let year = myDate.getFullYear()
- let month = myDate.getMonth()
- let end = year + '-' + ('0' + (month + 1)).slice(-2)
- if (month === 0) {
- year -= 1
- month = 12
- }
- let start = year + '-' + ('0' + month).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 {
- 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 = getFormatDatetime(date[0].$d)
- endTime = getFormatDatetime(date[1].$d)
- } else {
- startTime = getFormatDatetime(date[0])
- endTime = getFormatDatetime(date[1])
- }
- return {startTime, endTime}
- }
- export function formatYear(date) {
- if (typeof date === 'undefined') return null
- if (date === '' || date === null) return null
- if (typeof date === 'string') return date
- return date.getFullYear()
- }
- export function formatMonth(date) {
- if (typeof date === 'string') return date
- if (typeof date === 'undefined') return null
- if (date === '' || date === null) return null
- 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 = 'YYYY-MM-DD HH:mm:ss') {
- if (stringIsBlank(date)) {
- return ''
- }
- return moment(date).format(pattern)
- }
- export function getLastMonth() {
- const myDate = new Date()
- let year = myDate.getFullYear()
- let month = myDate.getMonth()
- if (month === 0) {
- year -= 1
- month = 12
- }
- return year + '-' + ('0' + month).slice(-2)
- }
- export function judgeToday(date1, date2) {
- return moment(date2).isSame(moment(), "day")
- }
- // 时间相减
- export function subtractTime(date1, date2) {
- date1 = moment(date1)
- date2 = moment(date2)
- return date1.diff(date2, 'day', false)
- }
- export function formatMonth1(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)
- }
- export function getDateTiffDays(value) {
- // value---最近天数
- return moment(new Date().getTime() - value * 1000 * 24 * 60 * 60).format('YYYY-MM-DD hh:mm:ss')
- }
- // 获取前day的日期(arr是日期拼接符,默认'-')
- export function getDayAgo(day, arr, type) {
- var today = new Date();
- // 日期拼接符
- if (!arr) {
- arr = '-'
- }
- var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
- //注意,这行是关键代码
- today.setTime(targetday_milliseconds);
- var tYear = today.getFullYear();
- var tMonth = today.getMonth();
- var tDate = today.getDate();
- tMonth = doHandleMonth(tMonth + 1);
- tDate = doHandleMonth(tDate);
- // 显示年月
- if (1 === type) {
- return tYear + arr + tMonth
- } else if (2 === type) {
- // 显示月日
- return tMonth + arr + tDate;
- } else {
- // 显示年月日
- return tYear + arr + tMonth + arr + tDate;
- }
- }
- function doHandleMonth(month) {
- var m = month;
- if (month.toString().length == 1) {
- m = "0" + month;
- }
- return m;
- }
|