export const dateToString = (timeStr
,fmt
) => {
if(!timeStr
){
return;
}
let time
;
if(new Date(timeStr
).toJSON()){
time
= new Date(timeStr
);
}else{
let ieTimeOld
= new Date(timeStr
.replace(/-/g,'/').replace('T',' ').replace('.000+0000',''));
let ieTime
= new Date( ieTimeOld
.setHours(ieTimeOld
.getHours() + 8))
time
= new Date(ieTime
);
}
var o
= {
"M+" : time
.getMonth()+1,
"d+" : time
.getDate(),
"h+" : time
.getHours(),
"m+" : time
.getMinutes(),
"s+" : time
.getSeconds(),
"q+" : Math
.floor((time
.getMonth()+3)/3),
"S" : time
.getMilliseconds()
};
if(/(y+)/.test(fmt
)) {
fmt
=fmt
.replace(RegExp
.$
1, (time
.getFullYear()+"").substr(4 - RegExp
.$
1.length
));
}
for(var 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
;
}
export const stringToDate = (dateString
)=> {
if (dateString
) {
var arr1
= dateString
.split(" ");
var sdate
= arr1
[0].split('-');
var date
= "";
if(arr1
.length
== 2){
var sTime
= arr1
[1].split(':')
date
= new Date(sdate
[0], sdate
[1]-1, sdate
[2],sTime
[0],sTime
[1],sTime
[2]);
}else{
date
= new Date(sdate
[0], sdate
[1]-1, sdate
[2]);
}
return date
;
}
}
export const preMonth = (time
)=>{
let year
= time
.getFullYear();
let month
= time
.getMonth();
if(month
== 0){
year
= year
-1;
month
= 12;
}
if(month
<10){
month
= "0"+month
;
}
return year
+"-"+month
;
}
转载请注明原文地址: https://lol.8miu.com/read-20604.html