工具函数分享
// 时间戳转日期格式 yyyy-mm-dd hh:mm:ss
export const timestampToDate = (timestamp) => {
if (timestamp == null) {
return ''
}
let date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
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;
}
// 时间戳转日期格式 yyyy-mm-dd hh:mm
export const timestampToDate2 = (timestamp) => {
if (timestamp == null) {
return ''
}
let date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
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()) ;
return Y + M + D + h + m ;
}
// 时间戳转日期格式 yyyy-mm-dd hh:mm:ss
export const timestampToMonth = (timestamp) => {
if (timestamp == null) {
return ''
}
let date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
let Y = date.getFullYear() + '-';
let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
return Y + M;
}
export const formatDates = (data, column) => {
if (data == null) {
return ''
}
let date = new Date(parseInt(data))
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
}
// 时间戳转日期格式 yyyy-mm-dd
export const timestampToDate1 = (timestamp) => {
let date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
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;
}
export const formatterType = (type) => {
switch (type) {
case 1:
return '一';
case 2:
return '二';
case 3:
return '三';
case 4:
return '四';
default:
return '未知';
}
}
// 日期格式yyyy-mm-dd hh:mm:ss 转 时间戳
export const dateToTimestamp = (time) => {
return new Date(time).getTime()
}
export const timeDate = (time) => {
let data = new Date(time).toJSON();
return new Date(+new Date(data) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
}
export const timeJson = (time = +new Date()) => {
var date = new Date(time + 8 * 3600 * 1000);
return date.toJSON().substr(0, 19).replace('T', ' ').replace(/-/g, '-');
}
最新回复