Js acquisition date: the day before yesterday, yesterday, today, tomorrow, and the day after tomorrow

Source: Internet
Author: User

Js Code for obtaining the date: the day before yesterday, yesterday, today, tomorrow, the day after tomorrow, and the day after tomorrow:

Method 1:

Copy to ClipboardReference: [www.bkjia.com] <Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> js acquisition date: the day before yesterday, yesterday, today, tomorrow, and 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); // obtain the date after AddDayCount
Var y = dd. getYear ();
Var m = dd. getMonth () + 1; // obtain the date of the current month
Var d = dd. getDate ();
Return y + "-" + m + "-" + d;
}
Document. write ("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/> day after tomorrow:" + GetDateStr (2 ));
Document. write ("<br/> day after tomorrow:" + GetDateStr (3 ));
</Script>

</Body>
</Html>

Method 2:

Copy to ClipboardReference: [www.bkjia.com] <Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> js acquisition date: the day before yesterday, yesterday, today, tomorrow, and the day after tomorrow-fire college </title>
</Head>

<Body>
<Script language = "JavaScript" type = "text/javascript">
Date. prototype. DateAdd = function (strInterval, Number)
{
Var dtTmp = this;
Switch (strInterval ){
Case's ': return new Date (Date. parse (dtTmp) + (1000 * Number ));
Case 'N': return new Date (Date. parse (dtTmp) + (60000 * Number ));
Case 'H': return new Date (Date. parse (dtTmp) + (3600000 * Number ));
Case 'D': return new Date (Date. parse (dtTmp) + (86400000 * Number ));
Case 'W': return new Date (Date. parse (dtTmp) + (86400000*7) * Number ));
Case 'q': return new Date (dtTmp. getFullYear (), (dtTmp. getMonth () + Number * 3, dtTmp. getDate (), dtTmp. getHours (), dtTmp. getMinutes (), dtTmp. getSeconds ());
Case 'M': return new Date (dtTmp. getFullYear (), (dtTmp. getMonth () + Number, dtTmp. getDate (), dtTmp. getHours (), dtTmp. getMinutes (), dtTmp. getSeconds ());
Case 'y': return new Date (dtTmp. getFullYear () + Number), dtTmp. getMonth (), dtTmp. getDate (), dtTmp. getHours (), dtTmp. getMinutes (), dtTmp. getSeconds ());
}
}

Function GetDateStr2 (AddDayCount)
{
Var dd = new Date ();
Ddd = dd. DateAdd ('D', AddDayCount); // three days later
Var y = ddd. getYear ();
Var m = ddd. getMonth () + 1; // get the current month
Var d = ddd. getDate ();
Return y + "-" + m + "-" + d;
}


Document. write ("day before yesterday:" + GetDateStr2 (-2 ));
Document. write ("<br/> yesterday:" + GetDateStr2 (-1 ));
Document. write ("<br/> today:" + GetDateStr2 (0 ));
Document. write ("<br/> tomorrow:" + GetDateStr2 (1 ));
Document. write ("<br/> day after tomorrow:" + GetDateStr2 (2 ));
Document. write ("<br/> day after tomorrow:" + GetDateStr2 (3 ));

</Script>

</Body>
</Html>

Related Article

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.