JS get the day before, yesterday, today, tomorrow, the day after tomorrow

Source: Internet
Author: User

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

  1. <script language="javascript" type="Text/javascript" ><!--
  2. /**
  3. * Extension to date, converts date to a string of the specified format
  4. * 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
  5. * Year (Y) can use 1-4 placeholders, milliseconds (S) only with 1 placeholders (1-3 digits)
  6. * Eg:
  7. * (New Date ()). Pattern ("Yyyy-mm-dd hh:mm:ss. S ") ==> 2006-07-02 08:09:04.423
  8. * (New Date ()). Pattern ("Yyyy-mm-dd E HH:mm:ss") ==> 2009-03-10 II 20:09:04
  9. * (New Date ()). Pattern ("Yyyy-mm-dd EE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04
  10. * (New Date ()). Pattern ("Yyyy-mm-dd EEE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04
  11. * (New Date ()). Pattern ("yyyy-m-d h:m:s.s") ==> 2006-7-2 8:9:4.18
  12. */
  13. date.prototype.pattern=Function (FMT) {
  14. var o = {
  15. "m+": this.getmonth () +1, //month
  16. "d+": this.getdate (), //day
  17. "H +": this.gethours ()%12 = = 0?: this.gethours ()%12, //hour
  18. "H +": this.gethours (), //hour
  19. "m+": this.getminutes (), //min
  20. "s+": this.getseconds (), //sec
  21. "q+": Math.floor ((this.getmonth () +3)/3), //quarterly
  22. "S": this.getmilliseconds () //MS
  23. };
  24. var week = {
  25. " 0": "\u65e5",
  26. " 1": "\u4e00",
  27. " 2": "\u4e8c",
  28. " 3": "\u4e09",
  29. " 4": "\u56db",
  30. " 5": "\u4e94",
  31. "6": " \u516d"
  32. };
  33. if (/(y+)/.test (FMT)) {
  34. Fmt=fmt.replace (regexp.$1, (this.getfullyear () +""). substr (4-regexp.$1.length));
  35. }
  36. if (/(e+)/.test (FMT)) {
  37. Fmt=fmt.replace (regexp.$1, (regexp.$1.length>1)? (regexp.$1.length>2?)     " \u661f\u671f": " \u5468"): " ") +week[this.getday () +""]);
  38. }
  39. For (var k in o) {
  40. if (new RegExp ("(" + K +")"). Test (FMT)) {
  41. FMT = Fmt.replace (regexp.$1, (regexp.$1.length==1)?     (O[k]): (("xx" + o[k]). substr (("" + o[k]).));
  42. }
  43. }
  44. return FMT;
  45. }
  46. var date = new Date ();
  47. Window.alert (Date.pattern ("Yyyy-mm-dd hh:mm:ss"));
  48. --></script>

JS get the day before, yesterday, today, tomorrow, the day after tomorrow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.