效果:

代码:

 //用于获取日期本周、本月、本季度的js
//Author : guanghe
//文件引用方法:<script src="${staticPath}/common/js/dateUtil.js"></script>
/**
* 获取本周、本季度、本月、上月的开始日期、结束日期
*/
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; //格式化日期: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(theYear, theMonth) {
var monthStartDate = new Date(theYear, theMonth, 1);
var monthEndDate = new Date(theYear, theMonth + 1, 1);
var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
return days;
} //获得本季度的开始月份
function getQuarterStartMonth(theMonth) {
if(!theMonth) {
theMonth = nowMonth;
}
var quarterStartMonth = 0;
if (theMonth < 3) {
quarterStartMonth = 0;
}
if (2 < theMonth && theMonth < 6) {
quarterStartMonth = 3;
}
if (5 < theMonth && theMonth < 9) {
quarterStartMonth = 6;
}
if (theMonth > 8) {
quarterStartMonth = 9;
}
return quarterStartMonth;
} //获得本周的开始日期
function ghGetWeekStartDate() {
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
return formatDate(weekStartDate);
}
//获得本周的结束日期
function ghGetWeekEndDate() {
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek) + 1);
return formatDate(weekEndDate);
} //获得下周的开始日期
function ghGetNextWeekStartDate() {
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 7 + 1);
return formatDate(weekStartDate);
} //获得本月的开始日期
function ghGetMonthStartDate() {
var monthStartDate = new Date(nowYear, nowMonth, 1);
return formatDate(monthStartDate);
} //获得本月的结束日期
function ghGetMonthEndDate() {
var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowYear, nowMonth));
return formatDate(monthEndDate);
} //获得下月开始时间
function ghGetNextMonthStartDate() {
var theYear = nowYear;
var theMonth = nowMonth + 1;
if(theMonth == 12) {
theYear += 1;
theMonth = 0;
}
var nextMonthStartDate = new Date(theYear, theMonth, 1);
return formatDate(nextMonthStartDate);
} //获得本季度的开始日期
function ghGetQuarterStartDate() {
var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
return formatDate(quarterStartDate);
} //或的本季度的结束日期
function ghGetQuarterEndDate() {
var quarterEndMonth = getQuarterStartMonth() + 2;
var quarterStartDate = new Date(nowYear, quarterEndMonth,
getMonthDays(nowYear, quarterEndMonth));
return formatDate(quarterStartDate);
} function ghGetNextQuarterStartDate() {
var theYear = nowYear;
var theQuarterMonth = nowMonth + 3;
if(theQuarterMonth > 11) {
theYear += 1;
theQuarterMonth -= 12;
}
var quarterStartDate = new Date(theYear, getQuarterStartMonth(theQuarterMonth), 1);
return formatDate(quarterStartDate);
} //设置查询时间
function ghSetDate(areaName, inputName, id) {
var date = $(areaName).children(inputName);
var dateStart = date.eq(0);
var dateEnd = date.eq(1);
switch(id) {
case "cur_week" :
dateStart.val(ghGetWeekStartDate());
dateEnd.val(ghGetNextWeekStartDate());
break;
case "cur_month" :
dateStart.val(ghGetMonthStartDate());
dateEnd.val(ghGetNextMonthStartDate());
break;
case "cur_quarter" :
dateStart.val(ghGetQuarterStartDate());
dateEnd.val(ghGetNextQuarterStartDate());
break;
}
} //初始化时间区域选择控件
// name:时间组件选择器name
function ghInitWidget(name) {
var areaName = "#" + name + "_date";
var inputName = "input[name='" + name + "']";
var str = $("<label id='theLabel' class='control-label'><a id='cur_week'>本周</a>&emsp;<a id='cur_month'>本月</a>&emsp;<a id='cur_quarter'>本季度</a>&emsp;自定义&emsp;</label>");
$(areaName).before(str);
//点击区域按钮,高亮并搜索
$("#theLabel a").click(function() {
$("#theLabel a").css("color", "#337ab7");
$(this).css("color", "red");
ghSetDate(areaName, inputName, this.id);
$(":button.btn-search1").click();
});
//点击日期框,区域选择按钮取消高亮
$(areaName).children(inputName).click(function() {
$("#theLabel a").css("color", "#337ab7");
});
}

JS获取周、月、季度日期的更多相关文章

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

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

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

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

  3. JS获取当前时间和日期

    当前时间和日期 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1 ...

  4. Js获取当前时间、日期

    var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();    //获取完整的年份(4位,1 ...

  5. JS 获取当前日期的前一天日期(年月日格式)

    var time = (new Date).getTime() - 24 * 60 * 60 * 1000;var yesday = new Date(time); // 获取的是前一天日期yesda ...

  6. JS获取当年当月最后一天日期

    <html xmlns="http://www.w3.org/1999/xhtml" > <meta charset="UTF-8"> ...

  7. js 获取某月第一天和最后一天

    1.获取某月第一天和最后一天日期 function getDateByMonth (timeStamp) { let inDate = new Date(timeStamp) let year = i ...

  8. java获取当前时间的年周月季度等的开始结束时间

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created b ...

  9. js获取当前时间戳与日期比较

    如何用javascript获取当前时间戳: 复制代码 代码示例: 方法1: var timestamp = date.parse(new date()); 结果:1280977330000 方法2: ...

随机推荐

  1. div的最小宽高和最大宽高

    div的最小宽高和最大宽高很少使用但是很实用,今天敲代码,就遇到了,要在div里设置滚动条,众所周知,一般是设overflow-y:auto,但需要一个高度,只有div里的内容超过这个高度时,才会有滚 ...

  2. 洛谷P2414 阿狸的打字机【AC自动机】【fail树】【dfs序】【树状数组】

    居然真的遇上了这种蔡队题.瑟瑟发抖. 题目背景 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机. 题目描述 打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿 ...

  3. opengl导入obj模型

    在经过查阅各种资料以及各种bug之后,终于成功的实现了导入基本的obj模型. 首相介绍一下什么是obj模型 一.什么是OBJ模型 obj文件实际上是一个文本文档,主要有以下数据,一般可以通过blend ...

  4. PDB、PD、PMP、RTB哪个更好?为品牌主解锁程序化购买的选择技巧

    PDB.PD.PMP.RTB哪个更好?为品牌主解锁程序化购买的选择技巧 https://baijiahao.baidu.com/s?id=1620277114893208111&wfr=spi ...

  5. 算术平均数 print('arithmeticAverageSingleCompressionRatio:', sum(singleCompressionRatio)/len(singleCompressionRatio))

    print('arithmeticAverageSingleCompressionRatio:', sum(singleCompressionRatio)/len(singleCompressionR ...

  6. Redis、Mongo - 目录

    redis redis字典取数据.列表取数据(数据量大) redis 实现栈 - python mongodb - 可视化工具 / pymongo - 使用方法

  7. 【PyQt5-Qt Designer】QComboBox(下拉列表框) 使用模板

    import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * ###### ...

  8. 【虫师】【selenium】参数化

    # 1 #coding=utf-8 from selenium import webdriver import os,time source = open("F:\\test\\info.t ...

  9. android指纹

    Android 6.0+指纹识别心得 Android 6.0+Fingerprint心得 数据来源:https://blog.csdn.net/lhj1076880929/article/detail ...

  10. Jedis 对 Redis 的操作详解

    1. JedisUtil2. 键操作3. 字符串操作4. 字节串4. 整数和浮点数5. 列表6. 集合(Set)7. 散列8. 排序sort 本篇主要阐述Jedis对redis的五大类型的操作:字符串 ...