Cross-Browser Technique for Determining Last Day of a Month
To reliably calculate the last day of a month across different browsers, you can employ a technique that leverages the Date object method setFullYear() with a day value of 0.
The following example demonstrates this approach:
<code class="js">var d = new Date(); d.setFullYear(2008, 11, 0); console.log(d.toString()); // Sun Nov 30 2008</code>
This method exploits the behavior where setting the day value to 0 in setFullYear() leads to the last day of the previous month being returned.
Alternatively, you can use a similar approach with the setDate() method:
<code class="js">var month = 0; // January var d = new Date(2008, month + 1, 0); console.log(d.toString()); // last day in January</code>
Both methods provide cross-browser reliability for determining the last day of a month, eliminating any inconsistencies between different browsing environments.
The above is the detailed content of How to Find the Last Day of a Month in Any Browser?. For more information, please follow other related articles on the PHP Chinese website!