<HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>JS Get Date: The day before yesterday, today, tomorrow, the day after Tomorrow-liehuo.net</title></Head><Body><Scriptlanguage= "JavaScript"type= "Text/javascript">functionGetdatestr (adddaycount) {varDD= NewDate (); Dd.setdate (Dd.getdate ()+adddaycount);//get the date adddaycount days vary=dd.getfullyear (); varm=Dd.getmonth ()+1;//Gets the date of the current month varD=dd.getdate (); returny+"-"+m+"-"+D;} document.write ("day before yesterday:"+Getdatestr (-2));d Ocument.write ("<br/> Yesterday:"+Getdatestr (-1));d Ocument.write ("<br/> Today:"+Getdatestr (0));d Ocument.write ("<br/> Tomorrow:"+Getdatestr (1));d Ocument.write ("<br/> Day After Tomorrow:"+Getdatestr (2));d Ocument.write ("<br/> The day after tomorrow:"+Getdatestr (3));</Script></Body></HTML>
One way is: Date.parse (dateval), this function is powerful, but there is a fatal disadvantage, that is not supported by our common "year-month-day" format, the short date can use "/" or "-" as the date separator, but must be in the format of the month/day/year, such as " 7/20/96 ".
Another approach is to use split, such as:
var dtstr = "2006-11-25";
var Dtarr = Dtstr.split ("-");
var dt = new Date (dtarr[0], dtarr[1], dtarr[2]);
But this method is more rigid, requires a fixed date format, only if there is no way to use the case.
If we can split the year and day, we try to take it apart, such as ASP output date. It is then processed with the new date, and the date type is returned.
Date formatting
- <script language="javascript" type="Text/javascript" ><!--
- /**
- * Extension to date, converts date to a string of the specified format
- * Months (m), days (d), 12 hours (h), 24 hours (h), minutes (m), seconds (s), Weeks (E), quarter (q) can be used with 1-2 placeholders
- * Year (Y) can use 1-4 placeholders, milliseconds (S) only with 1 placeholders (1-3 digits)
- * 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 II 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-m-d h:m:s.s") ==> 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?: this.gethours ()%12, //hour
- "H +": this.gethours (), //hour
- "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]): (("xx" + o[k]). substr (("" + o[k]).));
- }
- }
- return FMT;
- }
- var date = new Date ();
- Window.alert (Date.pattern ("Yyyy-mm-dd hh:mm:ss"));
- --></script>
Transfer from http://www.cnblogs.com/gengaixue/archive/2011/07/05/2098299.html
JS get the day before, yesterday, today, tomorrow, the day after tomorrow