The code copy is as follows:
<html>
<head>
<meta http-equiv="Content-Type" content="textml; charset=utf-8">
<title>js get date: day before yesterday, yesterday, today, tomorrow, the day after tomorrow - Liehuo.Net</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
function GetDateStr(AddDayCount) {
var dd = new Date();
dd.setDate(dd.getDate()+AddDayCount);//Get the date of AddDayCount queen
var y = dd.getFullYear();
var m = dd.getMonth()+1;//Get the date of the current month
var d = dd.getDate();
return y+"-"+m+"-"+d;
}
document.write("Day last day:"+GetDateStr(-2));
document.write("<br />Yesterday: "+GetDateStr(-1));
document.write("<br />Today: "+GetDateStr(0));
document.write("<br />Tomorrow: "+GetDateStr(1));
document.write("<br />The day after tomorrow: "+GetDateStr(2));
document.write("<br />Da Zuo Tian: "+GetDateStr(3));
</script>
</body>
<ml>
One of the methods is: Date.parse(dateVal). This function is powerful, but it has a fatal disadvantage, that is, it does not support the commonly used "year-month-day" format. Short dates can use "/" or "-" as date separators, but must be represented in the format of month/day/year, such as "7/20/96".
Another way is to use split, such as:
The code copy is as follows:
var dtStr = "2006-11-25";
var dtArr = dtStr.split("-");
var dt = new Date(dtArr[0], dtArr[1], dtArr[2]);
However, this method is relatively rigid and requires a fixed date format and can only be used if there is no way.
If we can separate the year, month and day, try to separate them, for example, ASP outputs year, month and day respectively. Then use new Date to process, and the returned date type.
Date formatting
The code copy is as follows:
<script language="javascript" type="text/javascript"><!--
/**
* Extension of Date, convert Date into String in the specified format
* Month (M), day (d), 12 hours (h), 24 hours (H), minute (m), seconds (s), week (E), quarter (q) 1-2 placeholders can be used
* Year (y) can be used with 1-4 placeholders, millisecond (S) can be used with 1 placeholder (it is a 1-3 digit number)
* eg:
* (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
* (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 20:09:04
* (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04
* (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04
* (new Date()).pattern("yyyy-Md h:m:sS") ==> 2006-7-2 8:9:4.18
*/
Date.prototype.pattern=function(fmt) {
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //Day
"h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //hours
"H+" : this.getHours(), //hours
"m+" : this.getMinutes(), //min
"s+" : this.getSeconds(), //sec
"q+" : Math.floor((this.getMonth()+3)/3), //Quarterly
"S" : this.getMilliseconds() //ms
};
var week = {
"0" : "/u65e5",
"1" : "/u4e00",
"2" : "/u4e8c",
"3" : "/u4e09",
"4" : "/u56db",
"5" : "/u4e94",
"6" : "/u516d"
};
if(/(y+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
if(/(E+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);
}
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;
}
var date = new Date();
window.alert(date.pattern("yyyy-MM-dd hh:mm:ss"));
// --></script>