liushuangls / memo

用于记录
1 stars 0 forks source link

JS判断闰年与获取月份天数 #15

Closed liushuangls closed 6 years ago

liushuangls commented 7 years ago

判断闰年:

某个年份的2月有29号则为闰年

function isLeapYear(year) {
  // 如果year年2月没有29则自动进一变为3月1日
  var date = new Date(year, 1, 29)
  return date.getDate() === 29
}
isLeapYear(2000) // true
isLeapYear(2001) // false

获取天数:

下个月第0天就是这个月最后一天。

function monthDay(year, month) {
  var date = new Date(year, month, 0)
  return date.getDate()
}
monthDay(2017, 2)  // 28
monthDay(2017, 12)  // 31
headwindz commented 7 years ago

路过~

获取天数还有另外一种思路,下个月的第0天就是这个月的最后一天。

function monthDay(year, month) {
    var date = new Date(year, month, 0)
    return date.getDate()
}
liushuangls commented 7 years ago

@n0ruSh 谢谢指教~