字符串转换成日期,日期转换成字符中,废话不说,上代码!
//日/月/年
export function stringToDate (dateStr,separator) {
if(!separator){
separator="/";
}
var dateArr = dateStr.split(separator);
var day = parseInt(dateArr[0]);
//处理月份为04这样的情况
if(dateArr[0].indexOf("0") == 0){
day = parseInt(dateArr[0].substring(1));
}else{
day = parseInt(dateArr[0]);
}
var month;
//处理月份为04这样的情况
if(dateArr[1].indexOf("0") == 0){
month = parseInt(dateArr[1].substring(1));
}else{
month = parseInt(dateArr[1]);
}
var year = parseInt(dateArr[2]);
if(dateArr[2].indexOf("0") == 0){
year = parseInt(dateArr[2].substring(1));
}else{
year = parseInt(dateArr[2]);
}
var date = new Date(year,month -1,day);
return date;
}
//日/月/年
export function dateToString (date) {
var year = date.getFullYear();
var month =(date.getMonth() + 1).toString();
var day = (date.getDate()).toString();
if (month.length == 1) {
month = "0" + month;
}
if (day.length == 1) {
day = "0" + day;
}
var dateTime = day + "/" + month + "/" + year;
return dateTime;
}
//年-月-日
export function dateToString2 (date) {
var year = date.getFullYear();
var month =(date.getMonth() + 1).toString();
var day = (date.getDate()).toString();
if (month.length == 1) {
month = "0" + month;
}
if (day.length == 1) {
day = "0" + day;
}
var dateTime = year+ "-" + month +"-"+day +" 00:00:00" ;
return dateTime;
}
在Vue中引用