js 获取是否为闰年,以及各月的天数

calendar utils

isLeapYear



const isLeapYear = (year) => {
return (year % 4 === 0) && (year % 100 !== 0 || year % 400 === 0);
} const year = new Date().getFullYear() isLeapYear(year);

getMonthDays

const getMonthDays = (timestamp) => {
const date = new Date(timestamp);
const year = date.getFullYear();
const month = date.getMonth() + 1;
let days = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 2:
if(isLeapYear(year)) {
days = 29;
} else {
days = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
default:
break;
}
return days;
}

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 // 将时间设置为这个月的下一月的第一天,然后回拨一秒 function monthDay(year, month) {
var date = new Date(year, month, 1, 0, 0, 0)
var yesterDay = new Date(date - 1000)
return yesterDay.getDate()
}
monthDay(2017, 2) // 28
monthDay(2017, 12) // 31

isToday


const isToday = (timestamp = ``) => {
return new Date(timestamp).toLocaleDateString() === new Date().toLocaleDateString();
}

format today

const autoGetToday = (time = ``, debug = false) => {
let log = console.log;
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
if (debug) {
log(year);
log(month);
log(day);
}
let today = `${year}/${month}/${day} ${time}`;
if (debug) {
log(`today =`, today);
}
return today;
};

refs

https://www.cnblogs.com/xgqfrms/p/12661145.html



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


js 获取是否为闰年,以及各月的天数 & isLeapYear的更多相关文章

  1. js获取给定月份的N个月后的日期

    1.在讲js获取给定月份的N个月后的日期之前,小颖先给大家讲下getFullYear().getYear()的区别. ①getYear() var d = new Date() console.log ...

  2. js获取上一个月、下一个月格式为yyyy-mm-dd的日期

    /** * 获取上一个月 * * @date 格式为yyyy-mm-dd的日期,如:2014-01-25 */ function getPreMonth(date) { var arr = date. ...

  3. js如何获取一个月的天数 data javascript

    js如何获取一个月的天数 function days(year,month){ var dayCount; now = new Date(year,month, 0); dayCount = now. ...

  4. 用js获取周、月第一天和最后一天(转载)

    var getCurrentWeek = function (day) { var days = ["周日", "周一", "周二", &q ...

  5. 关于JS获取某月最后一天

    发现网上用js获取某月最后一个的方式大多比较复杂,上个简单的: new Date(2013,4).toJSON().substring(0,10) new Date(2013,4,0).toLocal ...

  6. js获取n分钟(或n小时或n个月)后(或前)的时间(日期)

    标题有点绕,其实意思就是根据系统当前时间,获取n分钟或n小时或n个月后的时间. 例如:当前时间下,获取10分钟后的时间. var date=new Date(); //1. js获取当前时间 var ...

  7. JS获取周、月、季度日期

    效果: 代码: //用于获取日期本周.本月.本季度的js //Author : guanghe //文件引用方法:<script src="${staticPath}/common/j ...

  8. js获取某周、某月、下月、某季度的开始日期、结束日期及判断日期第几周

    //格式化日期:yyyy-MM-dd function formatDate(date) {   var myyear = date.getFullYear();   var mymonth = da ...

  9. js获取上一个月、下一个月

    /** * 获取上一个月 * * @date 格式为yyyy-mm-dd的日期,如:2014-01-25 */ function getPreMonth(date) { var arr = date. ...

随机推荐

  1. redis 代码结构与阅读顺序

    https://www.cnblogs.com/aixiaomei/p/6311633.html

  2. HTTPS学习(二):原理与实践

    div.example { background-color: rgba(229, 236, 243, 1); color: rgba(0, 0, 0, 1); padding: 0.5em; mar ...

  3. IdentityServer4之Implicit和纯前端好像很配哦

    前言 上一篇Resource Owner Password Credentials模式虽然有用户参与,但对于非信任的第三方的来说,使用这种模式是有风险的,所以相对用的不多:这里接着说说implicit ...

  4. Python学习【第5篇】:数据类型和变量总结

    字符串,数字,列表,元组,字典 可变不可变 1.可变:列表 如: p.p1 { margin: 0; font: 11px Menlo; color: rgba(0, 0, 0, 1); backgr ...

  5. shell(shell函数、shell正则表达式)

    本章内容 shell函数 shell正则表达式 1.shell函数 linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. 格式: funname () { CMD #函数体 ...

  6. MiniProfiler性能分析工具— .Net Core中用法

    前言: 在日常开发中,应用程序的性能是我们需要关注的一个重点问题.当然我们有很多工具来分析程序性能:如:Zipkin等:但这些过于复杂,需要单独搭建. MiniProfiler就是一款简单,但功能强大 ...

  7. hdu1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  8. Codeforces Round #649 (Div. 2)

    Codeforces Round #649 (Div. 2) -- WKL \(\mathcal{A}\)题: \(\mathrm{XXXXX}\) Greedy implementation *12 ...

  9. 在WLS2下开发和部署NET Core3.1目录

    前言 在youtube推送上看到WSL2的消息(https://www.youtube.com/watch?v=MrZolfGm8Zk&t=1s),觉得很棒.恰好最近在学习PowerShell ...

  10. MySQL 企业案例:误删核心业务表

    问题描述: 1.正在运行的网站系统,MySQL 数据库,数据量 25G,日业务增量 10 - 15M 2.备份策略:每天 23:00,计划任务调用 mysqldump 执行全备脚本 3.故障时间点:上 ...