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())