> For the complete documentation index, see [llms.txt](https://sugar-at.gitbook.io/blog-article/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sugar-at.gitbook.io/blog-article/js/p1.md).

# 1. js获取某年某月的天数

## 实现代码

```javascript
/**
 * 获取某年某月的天数
 * @param {Number} year  年
 * @param {Number} month 月份
 */
function getDays(year, month) {
    return (new Date(year, month, 0)).getDate();
}
console.log(getDays(2019, 9));//30
```

## 代码解释

```javascript
new Date(year, month, day);
```

Date 对象提供的构造函数之一,初始化对象为指定的年月日

**注:** 月是从0开始计算(即:0代表1月),日是(1-31)

```javascript
new Date(2019,9,0)
```

这里day传入的0是因为如果日的范围是(1-31),传入0 就像前寻一天即等价于

```javascript
new Date(2019,8,30)//2019-09-30
```

```javascript
(new Date()).getDate()
```

getDate()：返回实例对象对应每个月的几号

所以

```javascript
(new Date(2019, 9, 0)).getDate();//30
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://sugar-at.gitbook.io/blog-article/js/p1.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
