/**
|
* @Description 时间转换为Long类型
|
* @Author wh
|
* @Date 2023/7/13 9:10
|
*/
|
export function DateToLong(val) {
|
if (val == null || val == undefined || val == '' || val == 'null' || val == 'undefined') {
|
return true;
|
}
|
// .replace(/ 这里填要匹配的内容/g,'这里是要替换为的内容')
|
/*
|
1. 替换-
|
2. 替换:
|
3. 替换空格
|
*/
|
var temp = val.toString().replace(/\s/g, '').replace(/-/g, '').replace(/:/g, '')
|
return temp;
|
}
|
|
function padLeftZero(str) {
|
return ('00' + str).substr(str.length)
|
}
|
|
/**
|
* @Description 20230712183150
|
* 14 位时间 转为 yyyy-MM-dd HH:mm:ss
|
* @Author wh
|
* @Date 2023/7/13 9:21
|
*/
|
export function LongToDateTime(val) {
|
if (val == null || val == undefined || val == '' || val == 'null' || val == 'undefined') {
|
return true;
|
}
|
var temp = val.toString()
|
// 年
|
var y = temp.slice(0, 4);
|
// 月
|
var m = temp.slice(4, 6);
|
// 日
|
var d = temp.slice(6, 8);
|
// 时
|
var hh = temp.slice(8, 10);
|
// 分
|
var mm = temp.slice(10, 12);
|
// 秒
|
var ss = temp.slice(12, 14);
|
var result = y + '-' + m + '-' + d + ' ' + hh + ':' + mm + ':' + ss
|
return result;
|
}
|
|
export function LongToDate(val) {
|
if (val == null || val == undefined || val == '' || val == 'null' || val == 'undefined') {
|
return true;
|
}
|
var temp = val.toString()
|
// 年
|
var y = temp.slice(0, 4);
|
// 月
|
var m = temp.slice(4, 6);
|
// 日
|
var d = temp.slice(6, 8);
|
var result = y + '-' + m + '-' + d
|
return result;
|
}
|
|
export function formatDate(date, fmt) {
|
try {
|
if (!(date instanceof Date)) {
|
date = new Date(date)
|
}
|
if (fmt == undefined || fmt == '') {
|
fmt = 'yyyy-MM-dd hh:mm:ss'
|
}
|
if (/(y+)/.test(fmt)) {
|
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
|
}
|
const o = {
|
'M+': date.getMonth() + 1,
|
'd+': date.getDate(),
|
'h+': date.getHours(),
|
'm+': date.getMinutes(),
|
's+': date.getSeconds()
|
}
|
for (const k in o) {
|
if (new RegExp(`(${k})`).test(fmt)) {
|
const str = o[k] + ''
|
fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
|
}
|
}
|
return fmt
|
} catch (e) {
|
return '-'
|
}
|
}
|
|
/**
|
* 当前时间
|
* yyyy-MM-dd HH:mm:ss
|
* @returns {string}
|
*/
|
export function formattedDate() {
|
const date = new Date();
|
const year = date.getFullYear();
|
const month = (date.getMonth() + 1).toString().padStart(2, '0');
|
const day = date.getDate().toString().padStart(2, '0');
|
const hour = date.getHours().toString().padStart(2, '0');
|
const minute = date.getMinutes().toString().padStart(2, '0');
|
const second = date.getSeconds().toString().padStart(2, '0');
|
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
|
}
|
|
/**
|
* 当前日期
|
* yyyy-MM-dd
|
* @returns {string}
|
*/
|
export function formattedDateDuan() {
|
const date = new Date();
|
const year = date.getFullYear();
|
const month = (date.getMonth() + 1).toString().padStart(2, '0');
|
const day = date.getDate().toString().padStart(2, '0');
|
return `${year}-${month}-${day}`;
|
}
|
|
/**
|
* 传入时间格式的时间,以当前时间为标准,返回距离当前时间之前
|
*/
|
export function timeago(date) { //dateTimeStamp是一个时间毫秒,注意时间戳是秒的形式,在这个毫秒的基础上除以1000,就是十位数的时间戳。13位数的都是时间毫秒。
|
if (!date) {
|
return
|
}
|
if (!(date instanceof Date)) {
|
date = new Date(date.replace(/-/g, '/'))
|
}
|
|
var dateTimeStamp = date.getTime()
|
|
var minute = 1000 * 60; //把分,时,天,周,半个月,一个月用毫秒表示
|
var hour = minute * 60;
|
var day = hour * 24;
|
var week = day * 7;
|
var halfamonth = day * 15;
|
var month = day * 30;
|
var now = new Date().getTime(); //获取当前时间毫秒
|
var diffValue = now - dateTimeStamp; //时间差
|
|
if (diffValue < 0) {
|
return;
|
}
|
var minC = diffValue / minute; //计算时间差的分,时,天,周,月
|
var hourC = diffValue / hour;
|
var dayC = diffValue / day;
|
var weekC = diffValue / week;
|
var monthC = diffValue / month;
|
var result = '';
|
if (monthC >= 1 && monthC <= 3) {
|
result = " " + parseInt(monthC) + "月前"
|
} else if (weekC >= 1 && weekC <= 3) {
|
result = " " + parseInt(weekC) + "周前"
|
} else if (dayC >= 1 && dayC <= 6) {
|
result = " " + parseInt(dayC) + "天前"
|
} else if (hourC >= 1 && hourC <= 23) {
|
result = " " + parseInt(hourC) + "小时前"
|
} else if (minC >= 1 && minC <= 59) {
|
result = " " + parseInt(minC) + "分钟前"
|
} else if (diffValue >= 0 && diffValue <= minute) {
|
result = "刚刚"
|
} else {
|
var datetime = new Date();
|
datetime.setTime(dateTimeStamp);
|
var Nyear = datetime.getFullYear();
|
var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
|
var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
|
var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
|
var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
|
var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
|
result = Nyear + "-" + Nmonth + "-" + Ndate
|
}
|
return result;
|
}
|