You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
192 lines
6.1 KiB
192 lines
6.1 KiB
/**
|
|
* 格林威治的时间差 补8小时
|
|
*/
|
|
export function formatDateTime(dateTime) {
|
|
dateTime = formatTimeStrToStr(dateTime)
|
|
if (!dateTime) {
|
|
return '-'
|
|
}
|
|
var nowDate = new Date(dateTime).getTime();
|
|
// var timezone = 8; //目标时区时间,东八区
|
|
var offset_GMT = new Date().getTimezoneOffset(); // 本地时间和格林威治的时间差,单位为分钟
|
|
// var nowDate = new Date().getTime(); // 本地时间距 1970 年 1 月 1 日午夜(GMT 时间)之间的毫秒数
|
|
// var targetDate = new Date(nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000);
|
|
var targetDate = new Date(nowDate - offset_GMT * 60 * 1000);
|
|
let datae = new Date(targetDate);
|
|
var YY = datae.getFullYear() + '-';
|
|
var MM =(datae.getMonth() + 1 < 10
|
|
? '0' + (datae.getMonth() + 1)
|
|
: datae.getMonth() + 1) + '-';
|
|
var DD = datae.getDate() < 10 ? '0' + datae.getDate() : datae.getDate();
|
|
var hh =(datae.getHours() < 10 ? '0' + datae.getHours() : datae.getHours())+':';
|
|
var mm =(datae.getMinutes() < 10 ? '0' + datae.getMinutes() : datae.getMinutes()) +':';
|
|
var ss =datae.getSeconds() < 10 ? '0' + datae.getSeconds() : datae.getSeconds();
|
|
let mydatavalue = `${YY}${MM}${DD} ${hh}${mm}${ss}`;
|
|
return mydatavalue
|
|
}
|
|
|
|
// 2022-08-31T09:45:51.9340433 转 2022-08-31 09:45:51
|
|
export function formatTimeStrToStr(timeStr) {
|
|
if (!timeStr || !new Date(timeStr)) {
|
|
return ''
|
|
}
|
|
if (timeStr.lastIndexOf('.') == -1) {
|
|
return timeStr.replace('T',' ').substring(0,timeStr.length)
|
|
}
|
|
return timeStr.replace('T',' ').substring(0,timeStr.lastIndexOf('.'))
|
|
}
|
|
|
|
// 2022-08-31 09:45:51 转 2022-08-31T09:45:51
|
|
export function formatTimeToT(timeStr) {
|
|
if(!timeStr) return ""
|
|
let _str = timeStr.replace(/(^\s*)|(\s*$)/g, "");
|
|
return _str.replace(' ','T').substring(0,_str.length)
|
|
}
|
|
|
|
// 时间转化格式化,使用位置(供应导入,excel中时间转义)
|
|
/**
|
|
* @param {*} data 当前时间
|
|
* @param {*} type date/默认:返回日期 time:返回时间 datetime:日期+时间
|
|
* @param {*} connect 转义链接符,默认“-”,只适合日期连接符
|
|
* 如果不是zh,则链接统一为传入值,zh则为年月日时分秒
|
|
* 如果类型为数组,则直接为数组,也适合时间连接符,有效数组格式为3位或6位
|
|
* @returns String
|
|
*/
|
|
export function formatTimeStampToNorm(data,type,connect){
|
|
if(!data || data.length <= 0){return ""}
|
|
let _date = new Date(data)
|
|
if(!type) type = "date";
|
|
if(!connect) connect = "-";
|
|
let year = __formatLength(_date.getFullYear());
|
|
let month = __formatLength(_date.getMonth() + 1);
|
|
let day = __formatLength(_date.getDate());
|
|
let hour = __formatLength(_date.getHours());
|
|
let minute = __formatLength(_date.getMinutes());
|
|
let second = __formatLength(_date.getSeconds());
|
|
|
|
function __formatLength(value){
|
|
if(Number(value) < 10){
|
|
return "0" + value
|
|
}else{
|
|
return value
|
|
}
|
|
}
|
|
|
|
let arr = (connect == 'zh') ? ["年","月","日","时","分","秒"] : [connect,connect,"",":",":",""]
|
|
if(typeof connect == "Array") arr = connect;
|
|
switch(type){
|
|
case "date":
|
|
return year+arr[0] + month+arr[1] + day+arr[2];
|
|
case "time":
|
|
return hour+arr[3] + minute+arr[4] + second+arr[5];
|
|
default:
|
|
return year+arr[0] + month+arr[1] + day+arr[2] + " " +hour+arr[3] + minute+arr[4] + second+arr[5];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Parse the time to string
|
|
* @param {(Object|string|number)} time
|
|
* @param {string} cFormat
|
|
* @returns {string | null}
|
|
*/
|
|
export function parseTime(time, cFormat) {
|
|
if (arguments.length === 0 || !time) {
|
|
return null
|
|
}
|
|
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
|
|
let date
|
|
if (typeof time === 'object') {
|
|
date = time
|
|
} else {
|
|
if ((typeof time === 'string')) {
|
|
if ((/^[0-9]+$/.test(time))) {
|
|
// support "1548221490638"
|
|
time = parseInt(time)
|
|
} else {
|
|
// support safari
|
|
// https://stackoverflow.com/questions/4310953/invalid-date-in-safari
|
|
time = time.replace(new RegExp(/-/gm), '/')
|
|
}
|
|
}
|
|
|
|
if ((typeof time === 'number') && (time.toString().length === 10)) {
|
|
time = time * 1000
|
|
}
|
|
date = new Date(time)
|
|
}
|
|
const formatObj = {
|
|
y: date.getFullYear(),
|
|
m: date.getMonth() + 1,
|
|
d: date.getDate(),
|
|
h: date.getHours(),
|
|
i: date.getMinutes(),
|
|
s: date.getSeconds(),
|
|
a: date.getDay()
|
|
}
|
|
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
|
|
const value = formatObj[key]
|
|
// Note: getDay() returns 0 on Sunday
|
|
if (key === 'a') {
|
|
return ['日', '一', '二', '三', '四', '五', '六'][value]
|
|
}
|
|
return value.toString().padStart(2, '0')
|
|
})
|
|
return time_str
|
|
}
|
|
|
|
/**
|
|
* @param {number} time
|
|
* @param {string} option
|
|
* @returns {string}
|
|
*/
|
|
export function formatTime(time, option) {
|
|
if (('' + time).length === 10) {
|
|
time = parseInt(time) * 1000
|
|
} else {
|
|
time = +time
|
|
}
|
|
const d = new Date(time)
|
|
const now = Date.now()
|
|
|
|
const diff = (now - d) / 1000
|
|
|
|
if (diff < 30) {
|
|
return '刚刚'
|
|
} else if (diff < 3600) {
|
|
// less 1 hour
|
|
return Math.ceil(diff / 60) + '分钟前'
|
|
} else if (diff < 3600 * 24) {
|
|
return Math.ceil(diff / 3600) + '小时前'
|
|
} else if (diff < 3600 * 24 * 2) {
|
|
return '1天前'
|
|
}
|
|
if (option) {
|
|
return parseTime(time, option)
|
|
} else {
|
|
return (
|
|
d.getMonth() +
|
|
1 +
|
|
'月' +
|
|
d.getDate() +
|
|
'日' +
|
|
d.getHours() +
|
|
'时' +
|
|
d.getMinutes() +
|
|
'分'
|
|
)
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param {string} type
|
|
* @returns {Date}
|
|
*/
|
|
export function getTime(type) {
|
|
if (type === 'start') {
|
|
return new Date().getTime() - 3600 * 1000 * 24 * 90
|
|
} else {
|
|
return new Date(new Date().toDateString())
|
|
}
|
|
}
|