merchapp/java-mall-app-shop-admin/utils/date.js
2025-05-08 10:16:20 +08:00

232 lines
7.0 KiB
JavaScript

/**
* @description 日期转换工具类
*/
/**
* 2020-07-12T11:39:35.000+00:00 =》 2020-07-12 12:24:18
* @returns {string}
* @param date
*/
export function renderTime(date) {
if (!date) return ''
const dateee = new Date(date).toJSON()
return new Date(+new Date(dateee) + 8 * 3600 * 1000)
.toISOString()
.replace(/T/g, ' ')
.replace(/\.[\d]{3}Z/, '')
}
/**
* // 时间戳转日期(包含年月日时分秒)
* @param timestamp
* @returns {string}
*/
export function timestampToTime(timestamp) {
if (timestamp) {
let date = new Date(parseInt(timestamp))
let Y = date.getFullYear() + '-'
let M =
(date.getMonth() + 1 < 10
? '0' + (date.getMonth() + 1)
: date.getMonth() + 1) + '-'
let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
let h =
(date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
let m =
(date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
':'
let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
return Y + M + D + ' ' + h + m + s
} else {
return ''
}
}
export function renderDate(date) {
if (!date) return ''
const fmt = "yyyy-MM-dd";
return renderDefault(date, fmt)
}
export function renderDefault(data, fmt) {
if (data == null) {
return null
}
const date = new Date(data);
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;
}
/**
* 日期字符串转年月日
* @param time
* @returns {string}
*/
export function timeToYMD(time) {
if (time) {
let date = new Date(time)
let Y = date.getFullYear() + '-'
let M =
(date.getMonth() + 1 < 10
? '0' + (date.getMonth() + 1)
: date.getMonth() + 1) + '-'
let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
return Y + M + D
} else {
return ''
}
}
// let datetransform = {
// // 将日期转换成含T的格式不含秒
// toTformat(times) {
// var time = times
// if (time) {
// var date = new Date(time)
// let Y = date.getFullYear() + '-'
// let M =
// (date.getMonth() + 1 < 10
// ? '0' + (date.getMonth() + 1)
// : date.getMonth() + 1) + '-'
// let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
// let h =
// (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
// let m =
// date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
// let s =
// date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
// return Y + M + D + 'T' + h + m
// } else {
// return ''
// }
// },
// // 将日期转换成标准格式
// tonormalformat(times) {
// var time = times
// if (time) {
// var date = new Date(time)
// let Y = date.getFullYear() + '-'
// let M =
// (date.getMonth() + 1 < 10
// ? '0' + (date.getMonth() + 1)
// : date.getMonth() + 1) + '-'
// let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
// let h =
// (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
// let m =
// (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
// ':'
// let s =
// date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
// return Y + M + D + ' ' + h + m + s
// } else {
// return ''
// }
// },
// // 日期转时间戳
// tostamp(times) {
// if (times) {
// let time = new Date(times)
// return time.getTime()
// } else {
// return ''
// }
// },
// // 日期转时间戳(零点)
// tozerostamp(times) {
// if (times) {
// let time = new Date(times + ' 00:00:00')
// return time.getTime()
// } else {
// return ''
// }
// },
//
// // 时间戳转日期(包含年月日)
// getday(timestamp) {
// if (timestamp) {
// let date = new Date(parseInt(timestamp))
// let Y = date.getFullYear() + '-'
// let M =
// (date.getMonth() + 1 < 10
// ? '0' + (date.getMonth() + 1)
// : date.getMonth() + 1) + '-'
// let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
// let h =
// (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
// let m =
// (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
// ':'
// let s =
// date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
// return Y + M + D
// } else {
// return ''
// }
// },
// // 时间戳转日期(包含时分秒)
// gethour(timestamp) {
// if (timestamp) {
// let date = new Date(parseInt(timestamp))
// let Y = date.getFullYear() + '-'
// let M =
// (date.getMonth() + 1 < 10
// ? '0' + (date.getMonth() + 1)
// : date.getMonth() + 1) + '-'
// let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
// let h =
// (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
// let m =
// (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
// ':'
// let s =
// date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
// return h + m + s
// } else {
// return ''
// }
// },
// // 日期/时分秒,参数为空时返回当前日期时间 type为"day"时返回当天日期 type为"hour"时返回当天时间 type空时返回当天日期加时间
// getnow(type) {
// let date = new Date()
// let Y = date.getFullYear() + '-'
// let M =
// (date.getMonth() + 1 < 10
// ? '0' + (date.getMonth() + 1)
// : date.getMonth() + 1) + '-'
// let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
// let h =
// (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
// let m =
// (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
// ':'
// let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
// if (type == 'day') {
// return Y + M + D
// } else if (type == 'hour') {
// return h + m + s
// } else {
// return Y + M + D + ' ' + h + m + s
// }
// },
// }