/**
* 获取本周、本季度、本月、上月的开端日期、停止日期
*/
var now = new Date(); //当前日期
var nowDayOfWeek = now.getDay(); //今天本周的第几天
var nowDay = now.getDate(); //当前日
var nowMonth = now.getMonth(); //当前月
var nowYear = now.getYear(); //当前年
nowYear += (nowYear < 2000) ? 1900 : 0; //

var lastMonthDate = new Date(); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
var lastYear = lastMonthDate.getYear();
var lastMonth = lastMonthDate.getMonth();

//格局化日期: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(myMonth){
var monthStartDate = new Date(nowYear, myMonth, 1);
var monthEndDate = new Date(nowYear, myMonth + 1, 1);
var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
return days;
}

//获得本季度的开端月份
function getQuarterStartMonth(){
var quarterStartMonth = 0;
if(nowMonth<3){
quarterStartMonth = 0;
}
if(2<nowMonth && nowMonth<6){
quarterStartMonth = 3;
}
if(5<nowMonth && nowMonth<9){
quarterStartMonth = 6;
}
if(nowMonth>8){
quarterStartMonth = 9;
}
return quarterStartMonth;
}

//获得本周的开端日期
function getWeekStartDate() {
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
return formatDate(weekStartDate);
}

//获得本周的停止日期
function getWeekEndDate() {
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
return formatDate(weekEndDate);
}

//获得本月的开端日期
function getMonthStartDate(){
var monthStartDate = new Date(nowYear, nowMonth, 1);
return formatDate(monthStartDate);
}

//获得本月的停止日期
function getMonthEndDate(){
var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
return formatDate(monthEndDate);
}

//获得上月开端时候
function getLastMonthStartDate(){
var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
return formatDate(lastMonthStartDate);
}

//获得上月停止时候
function getLastMonthEndDate(){
var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
return formatDate(lastMonthEndDate);
}

//获得本季度的开端日期
function getQuarterStartDate(){

var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
return formatDate(quarterStartDate);
}

//获得本季度的停止日期
function getQuarterEndDate(){
var quarterEndMonth = getQuarterStartMonth() + 2;
var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
return formatDate(quarterStartDate);
}

JavaScript中,本周、上周、本月、上月日期判断的更多相关文章

  1. 用php获取本周,上周,本月,上月,本季度日期的代码

    echo date("Ymd",strtotime("now")), "\n"; echo date("Ymd",str ...

  2. JS 时间 获取 当天,昨日,本周,上周,本月,上月

    调用 setTimeRange (2); function  setTimeRange (v) { var fmt = 'YYYY-MM-DD HH:mm'; var now = new Date() ...

  3. Asp.net C# 获取本周上周本月上月本年上年第一天最后一天时间大全

    DateTime dt = DateTime.Now; int weeknow = Convert.ToInt32(DateTime.Now.DayOfWeek); ) * weeknow + ; D ...

  4. [moka同学笔记]php 获取时间(今天,昨天,三天内,本周,上周,本月,三年内,半年内,一年内,三年内)

    <?php /** * php 获取时间(今天,昨天,三天内,本周,上周,本月,三年内,半年内,一年内,三年内) * * author:ihelloworld2010@gmail.com * d ...

  5. Javascript中的基本数据类型,如何判断数据类型,作用域链的理解

    第一部分:Javascript中的数据类型 javascript中 基本数据类型有 五种, 数字 number 字符串 string  布尔 boolean  未定义 undefined 空值 nul ...

  6. MySQL查询今天/本周/上周/本月/上个月份的数据

    MySQL查询的方式很多,下面为您介绍的MySQL查询实现的是查询本周.上周.本月.上个月份的数据,如果您对MySQL查询方面感兴趣的话,不妨一看. 查询当前今天的数据 SELECT name,sub ...

  7. java获取本周 上周的所有日期

    1 根据当前日期获得所在周的日期区间(周一和周日日期) public String getTimeInterval(Date date) { Calendar cal = Calendar.getIn ...

  8. PHP 返回近7天 本月 上月日期

    <?php /** * 返回近7天,本月,上月数据 * 不计当天(展示后台数据专用) */ function weekMonthLastMonth($search_date = 'week') ...

  9. SQL SERVER 取本月上月日期

    select   dateadd(dd,-day(dateadd(month,-1,getdate()))+1,dateadd(month,-1,getdate()))     /*上个月一号*/ s ...

随机推荐

  1. jQuery小节

    jQuery 语法 jQuery 选择器 在前面的章节中,我们展示了一些有关如何选取 HTML 元素的实例. 关键点是学习 jQuery 选择器是如何准确地选取您希望应用效果的元素. jQuery 元 ...

  2. CSS 图片倾斜的制作

    <style> #zhong{ height:600px; width:1350px; position:relative; z-index:2} .znei{ height:60px; ...

  3. 把HBITMAP 保存图片文件.

    //VC下把HBITMAP保存为bmp图片 BOOL CtestBitMapDlg::SaveBmp(HBITMAP hBitmap, CString FileName) { HDC hDC; //当 ...

  4. DBCP数据源连接池实现原理分析

    前些天在调试公司系统的时候发现这样的一个问题:mysql数据库服务停止一段时间后再次重启后吗,tomcat服务无法请求数据库服务,调试了半天对这个问题进行定位解决,期间也搞了很多有关mysql数据库的 ...

  5. Quartz.net 定时调度CronTrigger时间配置格式说明

    1.   CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 ...

  6. mysql存储过程 --游标的使用 取每行记录 (多字段)

    delimiter $ create PROCEDURE phoneDeal() BEGIN DECLARE id varchar(64); -- id DECLARE phone1 varchar( ...

  7. iOS.Animation.Math-behind-CATransform3D

    iOS CoreAnimation: Math behind CATransform3D 1. What's CATransform? Matrix Transform: "User spa ...

  8. 终端 git log 修改样式

    git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d ...

  9. MySQL——保证数据的完整性

    为了防止垃圾的产生,从而影响数据库的执行效率. 1实体完整性——行数据的有效性   唯一约束(unique).主键约束(primary key) 2域完整性——列数据的有效性 非空约束(not nul ...

  10. python课程第四周重点记录

    1.迭代器 names = iter(["alex","jack","rain"]) #声明列表的一个迭代器 names.__next__( ...