Javascript - Lấy số ngày trong tháng
Lấy số ngày trong tháng
Viết hàm JavaScript để lấy số ngày trong tháng.
Mã nguồn:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JavaScript program to convert the letters of a given string in alphabetical order.</title>
</head>
<script>
var getDaysInMonth = function(month,year) {
// Here January is 1 based
//Day 0 is the last day in the previous month
return new Date(year, month, 0).getDate();
// Here January is 0 based
// return new Date(year, month+1, 0).getDate();
};
document.write(getDaysInMonth(1, 2012)+"<br/>");
document.write(getDaysInMonth(2, 2012)+"<br/>");
document.write(getDaysInMonth(9, 2012)+"<br/>");
document.write(getDaysInMonth(12, 2012));
</script>
<body>
</body>
</html>
Lưu đồ thuật toán: