我如何在 javascript 中获得每月的日期?

我需要从 Date 对象获取月份的日期,但似乎 getDay()方法返回星期的日期。是否有一个函数返回月中的某一天?

87114 次浏览

Use date_object.getDate() to get the month day.

From the MDN docs link:

"Returns the day of the month for the specified date according to local time."

Try getDate() instead. Confusing naming, but that's life...

var date = new Date().getDate();

From Mozilla Developer Network:

Date.prototype.getDate() The getDate() method returns the day of the month for the specified date according to local time.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate

const date = new Date();


const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();