how to get the number of days in a month in javascript
// instantiate a date object
var dt = new Date();
// dt.getMonth() will return a month between 0 - 11
// we add one to get to the last day of the month
// so that when getDate() is called it will return the last day of the month
var month = dt.getMonth() + 1;
var year = dt.getFullYear();
// this line does the magic (in collab with the lines above)
var daysInMonth = new Date(year, month, 0).getDate();
3.7
10
var dt = new Date();
var month = dt.getMonth();
var year = dt.getFullYear();
daysInMonth = new Date(year, month, 0).getDate();
Thank you!
10
0
5
1
<script type="text/javascript">
var dt = new Date();
var month = dt.getMonth();
var year = dt.getFullYear();
daysInMonth = new Date(year, month, 0).getDate();
</script>
Thank you!
1
0
Are there any code examples left?
New code examples in category Javascript