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> 

Xem ví dụ

Lưu đồ thuật toán:

Flowchart: JavaScript- Get the number of days in a month


Chatbot Tư vấn Lộ trình CNTT 🤖
Chào bạn! Tôi có thể tư vấn về các lộ trình học CNTT dựa trên roadmap.w3typing.com. Hãy chọn một từ khóa dưới đây hoặc gõ câu hỏi của bạn (ví dụ: "Frontend", "Backend", "Python", "DevOps").