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

Source: Internet
Author: User

<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

    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>

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

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.