Js get date: yesterday today and tomorrow the day after tomorrow
- 2020-03-30 03:18:52
- OfStack
<html>
<head>
<meta http-equiv="Content-Type" content="textml; charset=utf-8">
<title>js Date of acquisition: the day before 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);//Gets the date after AddDayCount
var y = dd.getFullYear();
var m = dd.getMonth()+1;//Gets the date of the current month
var d = dd.getDate();
return y+"-"+m+"-"+d;
}
document.write(" The day before yesterday: "+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 /> The day: "+GetDateStr(3));
</script>
</body>
<ml>
One way to do this is date.parse (dateVal). This function is powerful, but it has the fatal drawback that it does not support our usual "year - month - day" format. Short dates can use "/" or "-" as Date separators, but must be represented in the month/day/year format, such as "7/20/96".
Another way is to use split, for example:
var dtStr = "2006-11-25";
var dtArr = dtStr.split("-");
var dt = new Date(dtArr[0], dtArr[1], dtArr[2]);
However, this method is rigid and requires a fixed date format, which is only used when there is no other way.
If we can disassemble the date, try to disassemble it, such as ASP respectively output date. Then we'll do it with new Date, and we'll return the Date type.
Date formatting
<script language="javascript" type="text/javascript"><!--
Date.prototype.pattern=function(fmt) {
var o = {
"M+" : this.getMonth()+1, // in
"d+" : this.getDate(), // day
"h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, // hours
"H+" : this.getHours(), // hours
"m+" : this.getMinutes(), // points
"s+" : this.getSeconds(), // seconds
"q+" : Math.floor((this.getMonth()+3)/3), // quarter
"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 ? "u661fu671f" : "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>