How does JavaScript obtain the date such as yesterday and tomorrow?

Source: Internet
Author: User

Sometimes we need to know the date of yesterday, the day before yesterday, tomorrow or the day after tomorrow. The following program can achieve this requirement. It is not complicated at all, mainly because you are familiar with the use of the JavaScript setDate () method and getDate () method. Let's take a look at the demo and code implementation below.

Effect demonstration

JavaScript code
<Script type = "text/javascript"> function GetDateStr (AddDayCount) {var dd = new Date (); dd. setDate (dd. getDate () + AddDayCount); // obtain the date var y = dd after AddDayCount. getFullYear (); var m = dd. getMonth () + 1; // obtain the date var d = dd of the current month. 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/> The Day After Tomorrow:" + GetDateStr (2); document. write ("<br/> day after tomorrow:" + GetDateStr (3); </script>
JavaScript setDate () method

The setDate () method is used to set a day of a month. The syntax is dateObject. setDate (day ).

The day parameter is required. Represents a value (1 ~ 31 ).

This method is always used in combination with a Date object. We can see from the above example.

JavaScript getDate () method

The getDate () method returns a day of a month. The local time is used for a day in the month specified by dateObject. The returned value is 1 ~ An integer between 31.

For example, output the date of the current month:

var d = new Date()document.write(d.getDate())

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.