Home > Web Front-end > JS Tutorial > body text

How to Find the Last Day of a Month in Any Browser?

Mary-Kate Olsen
Release: 2024-11-05 17:50:02
Original
172 people have browsed it

How to Find the Last Day of a Month in Any Browser?

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!