JS把字符串时间转为时间格式
function stringToDate(dateStr,separator){
if(!separator){
separator="-";
}
var dateArr = dateStr.split(separator);
var year = parseInt(dateArr[0]);
var month;
if(dateArr[1].indexOf("0") == 0){
month = parseInt(dateArr[1].substring(1));
}else{
month = parseInt(dateArr[1]);
}
var day = parseInt(dateArr[2]);
var date = new Date(year,month -1,day);
return date;
}
JS给日期加一天或减一天的函数
var dayTime=new Date();
dayTime=dateTime.setDate(dayTime.getDate()+1);
dayTime=new Date(dayTime);
dayTime=dateTime.setDate(dayTime.getDate()-1);
dayTime=new Date(dayTime);
JS字符串格式化日期
function dateFormat(fmt, date) {
let ret;
const opt = {
"Y+": date.getFullYear().toString(),
"m+": (date.getMonth() + 1).toString(),
"d+": date.getDate().toString(),
"H+": date.getHours().toString(),
"M+": date.getMinutes().toString(),
"S+": date.getSeconds().toString()
};
for (let k in opt) {
ret = new RegExp("(" + k + ")").exec(fmt);
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
};
};
return fmt;
}
JS日期转字符串
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 = year + "-" + month + "-" + day;
return dateTime;
}