var str = "2010-08-01";
// 转换日期格式
str = str.replace(/-/g, '/'); // "2010/08/01";
// 创立日期对象
var date = new Date(str);
// 加一天
date.setDate(date.getDate() + 1);
扩展材料:
反之,日期格式化为字符串
//完好的格式化 var time2 = new Date().format("yyyy-MM-dd hh:mm:ss");
Date.prototype.format = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
//处置年份
var reYear = /(y+)/;
var resultYear = reYear.exec(fmt);
if (resultYear)
{
var yearformatPart = resultYear[0];//匹配到的格式化字符
var yearVal = (this.getFullYear() + "").substr(4 - yearformatPart.length);
fmt = fmt.replace(yearformatPart, yearVal);
}
for (var k in o) {
var re = new RegExp("(" + k + ")");
var re = re.exec(fmt);
if (re) {
var Val = "" + o[k];//本次需求交换的数据
var formatPart = re[0];//匹配到的格式化字符
var replaceVal = (formatPart.length == 1) ? (Val) : (("00" + Val).substr(Val.length));
fmt = fmt.replace(formatPart, replaceVal);
}
}
return fmt;
}
需求准备的资料分别有:电脑、html编辑器、浏览器。
1、首先,翻开html编辑器,新建html文件,例如:index.html。
2、在index.html中的标签,输入js代码:
var a = '2006-10-01 12:00:05';
var date = new Date(a.replace(/-/g, '/'));
document.body.innerText = date;
3、浏览器运行index.html页面,此时胜利打印出了被转化为日期的数据。
相关文章