JS get the day before yesterday, today, tomorrow, the day after Tomorrow (turn) JS gets the day before, yesterday, today, tomorrow, the day after tomorrow 2011-05-19 21:03
<body> <script language= "JavaScript" type= "Text/javascript" > Function getdatestr (adddaycount) {var DD = n EW Date (); Dd.setdate (Dd.getdate () +adddaycount);//Get Adddaycount days after the date 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 After Tomorrow:" +getdatestr (3)); </script>
</body>
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 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]);
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>
JS get the day before, yesterday, today, tomorrow, the day after tomorrow