js获取某周、某月、下月、某季度的开始日期、结束日期及判断日期第几周
//格式化日期:yyyy-MM-dd
function formatDate(date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth()+1;
var myweekday = date.getDate();
if(mymonth < 10){
mymonth = "0" + mymonth;
}
if(myweekday < 10){
myweekday = "0" + myweekday;
}
return (myyear+"-"+mymonth + "-" + myweekday);
}
//获得某月的天数
function getMonthDays(paraYear,paraMonth){
var monthStartDate = new Date(paraYear, paraMonth, 1);
var monthEndDate = new Date(paraYear, paraMonth + 1, 1);
var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
return days;
}
//获得某周的开始日期
function getWeekStartDate(paraYear,paraMonth,paraDay,paraDayOfWeek) {
var weekStartDate = new Date(paraYear, paraMonth, paraDay + 1 - paraDayOfWeek);
return formatDate(weekStartDate);
}
//获得某周的结束日期
function getWeekEndDate(paraYear,paraMonth,paraDay,paraDayOfWeek) {
var weekEndDate = new Date(paraYear, paraMonth, paraDay + (7 - paraDayOfWeek));
return formatDate(weekEndDate);
}
//获得某月的开始日期
function getMonthStartDate(paraYear,paraMonth){
var monthStartDate = new Date(paraYear, paraMonth, 1);
return formatDate(monthStartDate);
}
//获得某月的结束日期
function getMonthEndDate(paraYear,paraMonth){
var monthEndDate = new Date(paraYear,paraMonth, getMonthDays(paraYear,paraMonth));
return formatDate(monthEndDate);
}
//获得上月开始时间
function getLastMonthStartDate(paraYear, lastMonth){
var lastMonthStartDate = new Date(paraYear, lastMonth, 1);
return formatDate(lastMonthStartDate);
}
//获得上月结束时间
function getLastMonthEndDate(paraYear, lastMonth, lastMonth){
var lastMonthEndDate = new Date(paraYear, lastMonth, getMonthDays(lastMonth));
return formatDate(lastMonthEndDate);
}
//获得某季度的开始日期
function getQuarterStartDate(paraYear,paraSeason){
switch (paraSeason){
case '1' : return paraYear+"-01-01";
case '2' : return paraYear+"-04-01";
case '3' : return paraYear+"-07-01";
case '4' : return paraYear+"-10-01";
}
}
//获得某季度的结束日期
function getQuarterEndDate(paraYear,paraSeason){
switch (paraSeason){
case '1' : return paraYear+"-03-31";
case '2' : return paraYear+"-06-30";
case '3' : return paraYear+"-09-30";
case '4' : return paraYear+"-12-31";
}
}
//获取某年某周的开始日期
function getBeginDateOfWeek(paraYear, weekIndex){
var firstDay = GetFirstWeekBegDay(paraYear);
//7*24*3600000 是一星期的时间毫秒数,(JS中的日期精确到毫秒)
var time=(weekIndex-1)*7*24*3600000;
var beginDay = firstDay;
//为日期对象 date 重新设置成时间 time
beginDay.setTime(firstDay.valueOf()+time);
return formatDate(beginDay);
}
//获取某年某周的结束日期
function getEndDateOfWeek(paraYear, weekIndex){
var firstDay = GetFirstWeekBegDay(paraYear);
//7*24*3600000 是一星期的时间毫秒数,(JS中的日期精确到毫秒)
var time=(weekIndex-1)*7*24*3600000;
var weekTime = 6*24*3600000;
var endDay = firstDay;
//为日期对象 date 重新设置成时间 time
endDay.setTime(firstDay.valueOf()+weekTime+time);
return formatDate(endDay);
}
//获取日期为某年的第几周
function GetWeekIndex(dateobj) {
var firstDay = GetFirstWeekBegDay(dateobj.getFullYear());
if (dateobj < firstDay) {
firstDay = GetFirstWeekBegDay(dateobj.getFullYear() - 1);
}
d = Math.floor((dateobj.valueOf() - firstDay.valueOf()) / 86400000);
return Math.floor(d / 7) + 1;
}
//获取某年的第一天
function GetFirstWeekBegDay(year) {
var tempdate = new Date(year, 0, 1);
var temp = tempdate.getDay();
if (temp == 1){
return tempdate;
}
temp = temp == 0 7 : temp;
tempdate = tempdate.setDate(tempdate.getDate() + (8 - temp));
return new Date(tempdate);
}
js获取某周、某月、下月、某季度的开始日期、结束日期及判断日期第几周的更多相关文章
- js获取前几个月的具体日期
// 往前数monthNum月份,不能往后数monthNum getPreMonthDay("2018-12-28",20) // 往前数monthNum月份,不能往后数month ...
- js 获取现在时间一个月(N天)后的日期
欢迎加入前端交流群交流知识&&获取视频资料:749539640 let today = new Date().getTime() let lastDay = getTimeByDay( ...
- JS获取当前日期及 js获取当前时间和一星期前的时间
var myDate = new Date(); new Date() 代表当前 年 月 日 时 分 秒: myDate.getYear(); //获取当前年份(2位),getY ...
- js日期计算及快速获取周、月、季度起止日,获取指定日期周数以及星期几的小例子
JS获取日期时遇到如下需求,根据某年某周获取一周的日期.如开始日期规定为星期四到下一周的星期五为一周. 格式化日期: function getNowFormatDate(theDate) { var ...
- js获取几个月前,几周前时间。
/** * DK 命名空间 防止全局变量污染 */ var DK = {} ; /** * 获取前几个月,默认为一个月,当前时间 * @author duke * @date 格式为yy ...
- 用js获取周、月第一天和最后一天(转载)
var getCurrentWeek = function (day) { var days = ["周日", "周一", "周二", &q ...
- js日期计算及快速获取周、月、季度起止日
var now = new Date(); //当前日期 var nowDayOfWeek = (now.getDay() == 0) ? 7 : now.getDay() - 1; //今天是本周的 ...
- js获取上一个月、下一个月格式为yyyy-mm-dd的日期
/** * 获取上一个月 * * @date 格式为yyyy-mm-dd的日期,如:2014-01-25 */ function getPreMonth(date) { var arr = date. ...
- C#根据当前时间获取周,月,季度,年度等时间段的起止时间
最近有个统计分布的需求,需要按统计本周,上周,本月,上月,本季度,上季度,本年度,上年度等时间统计分布趋势,所以这里就涉及到计算周,月,季度,年度等的起止时间了,下面总结一下C#中关于根据当前时间获取 ...
- MySQL中获取天、周、月等数据
MySQL中获取天.周.月等数据 1.今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 2.昨天 SELECT * FROM 表名 ...
随机推荐
- Python2和Python3
1. 字符编码 1.1. Python2默认为ACSII编码 1.2. Python3为Unicode 2. Unicode和UTF8和GBK编码的关系 utf8:中文3字节 ...
- 天津Uber优步司机奖励政策(12月21日到12月27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- day 5 模块发布安装
1.模块的位置 现在当前路径查找,再到系统路径/usr/lib/python3.5/查找,再到其他系统路径查找 2.模块发布 1)模块目录结构 Msg ├── __init__.py ├── recv ...
- apache和IIS共存,服务器对外统一使用80端口
apache和IIS共用80端口为了PHP与ASP各自的执行效率,要在服务器上安装iis与Apache,但是无法同时使用80端口,否则其中必定有一个启动不了.让它们共存的并且访问网站不需要加端口号,解 ...
- android学习九 对话框碎片
1.android的对话框是异步的,对话框创建后马上执行下面的代码.好处: a.通过实现对话框的回调方法反馈用户与对话框的交互. b.能够在代码中清楚对话框. 2.碎片对话框基 ...
- Unity AssetBundle工作流
一.创建AssetBundle 1.在资源的Inspector视图下有一个AssetBundle的UI,第一个选项表示AssetBundle名称,第二个用于设置AssetBundle Variant, ...
- TW实习日记:第27天
今天依旧是磨洋工的一天,说真的,被存在各种问题的后端接口把耐心和动力都给磨没了.于是一天就又在沟通接口问题中度过了,完善了一个新功能,将一个新功能开发到了一半.效率可真是够低的,唉.然后不知道为什么突 ...
- OIDC in Angular 6
参照 草根专栏- ASP.NET Core + Ng6 实战:https://v.qq.com/x/page/i07702h18nz.html 1. OIDC-Client https://githu ...
- 搭建hexo博客并部署到github上
hexo是由Node.js驱动的一款快速.简单且功能强大的博客框架,支持多线程,数百篇文章只需几秒即可生成.支持markdown编写文章,可以方便的生成静态网页托管在github上. 感觉不错. 前端 ...
- vue中如何实现pdf文件预览?
今天产品提出一个优化的需求,就是之前我们做的图片展示就是一个img标签搞定,由于我们做的是海外后台管理系统,那边的人上传的文件时pdf格式,vue本事是不支持这种格式文件展示的,于是就google搜索 ...