JS时间操作
2024-08-25 05:45:49
/**
* 判断年份是否为润年
*
* @param {Number} year
*/
function isLeapYear(year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
/**
* 获取某一年份的某一月份的天数
*
* @param {Number} year
* @param {Number} month
*/
function getMonthDays(year, month) {
return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28);
} /**
* 获取某年的某天是第几周
* @param {Number} y
* @param {Number} m
* @param {Number} d
* @returns {Number}
*/
function getWeekNumber(now) {
year = now.getFullYear(),
month = now.getMonth(),
days = now.getDate();
//那一天是那一年中的第多少天
for (var i = 0; i < month; i++) {
days += getMonthDays(year, i);
} //那一年第一天是星期几
var yearFirstDay = new Date(year, 0, 1).getDay() || 7; var week = null;
if (yearFirstDay == 1) {
week = Math.ceil(days / yearFirstDay);
} else {
days -= (7 - yearFirstDay + 1);
week = Math.ceil(days / 7) + 1;
} return week;
} //一个月有多少天
function getCountDays(curDate) { /* 获取当前月份 */
var curMonth = curDate.getMonth();
/* 生成实际的月份: 由于curMonth会比实际月份小1, 故需加1 */
curDate.setMonth(curMonth + 1);
/* 将日期设置为0, 这里为什么要这样设置, 我不知道原因, 这是从网上学来的 */
curDate.setDate(0);
/* 返回当月的天数 */
return curDate.getDate();
} function getWeekStart(curDate) { var curMonth = curDate.getMonth();
curDate.setDate(curDate.getDate() + 1 - curDate.getDay());//
return curDate;
}
function getWeekEnd(curDate) { curDate.setDate(curDate.getDate() + 7 - curDate.getDay());
return curDate;
}
最新文章
- 图解GitHub基本操作
- 快速入门系列--CLR--01基本概念
- QA:Failed to deploy artifacts from/to snapshots XX Failed to transfer file Return code is: 405, ReasonPhrase:Method Not Allowed.
- 深入理解OpenGL拾取模式(OpenGL Picking)
- mount/umount系统调用
- Struts2运行流程分析
- Android使用Google推荐的联网框架Volley,让连接网络更加简单
- IBATIS处理typeHandler类容易范的SQLException总结
- extjs+amcharts生成3D柱状图和数据表格使用总结
- (从零开始java开发) IDEA+MAVEN构建一个webapp骨架项目(解决一直downloading问题)
- OvS: data structure analysis
- python 中__setattr__, __getattr__,__getattribute__, __call__使用方法
- React点击操作自动定位到另外一个元素
- 八大排序算法——基数排序(动图演示 思路分析 实例代码java 复杂度分析)
- 经纬度编码方法推荐-plus code简介
- Vxlan学习笔记——原理(转)
- C# 定时器和队列结合,卖包子啦,Timer、 AutoResetEvent、 ManualResetEvent
- DDD领域模型数据访问权限之用户权限(十)
- 设置Eclipse的类文件和xml文件代码自动补全
- Oracle EBS 银行账户API
热门文章
- 数据库系统原理及其应用总结---ShinePans
- 使用断言assert
- RPM包查询
- 使用 Intel HAXM 为 Android 模拟器加速,媲美真机(转)
- 查看Linux相关信息
- asp.net 发送邮件代码 System.Net.Mail
- sublime text3 安装package control
- flask开发restful api系列(3)--利用alembic进行数据库更改
- 关于lambda表达式树
- ROW_NUMBER()/RANK()/DENSE_RANK()/ntile() over()