Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 第736天 写一个方法根据日期获取当月有多少天 #3846

Open
haizhilin2013 opened this issue Apr 20, 2021 · 4 comments
Open
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第736天 写一个方法根据日期获取当月有多少天

3+1官网

我也要出题

@haizhilin2013 haizhilin2013 added the js JavaScript label Apr 20, 2021
@githubzml
Copy link

function getDateNum(params) {
  let temp = null;
  let getYear = params.getFullYear();
  let getMonth = params.getMonth() + 1;
  // new Date(year, month, 0).getDate()
  temp = new Date(getYear, getMonth, 0).getDate();
  return temp;
}
console.log('结果:', getDateNum(new Date()));

@ghost
Copy link

ghost commented Apr 21, 2021

/**
 * 根据日期获取当月天数
 * @param {string|number} month - 月份
 * @param {string|number} [year] - 年份
 * @returns {number} 天数
 */
function daysInMonth(month, year) {
    year = year || new Date().getFullYear();
    return new Date(year, month, 0).getDate();
}

daysInMonth(8, 2019); // => 31
daysInMonth(2, 2020); // => 29
daysInMonth(2); // => 28

@yxj1028530975
Copy link

function getDateDays(year,month){ return new Date(year,month,0).getDate()//获得是标准时间,需要getDate()获得天数 } getDateDays(2021,5) // =>31

@alanhg
Copy link

alanhg commented Apr 22, 2021

const daysInMonth = (date) => new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests

4 participants