//转自http://blog.csdn.net/xc_young/article/details/16878849,其上季度有错误,已修改
var MrYangUtil = function () { this.getCurrentDate = function () {
return new Date();
}; this.getCurrentWeek = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//返回date是一周中的某一天
var week = currentDate.getDay();
//返回date是一个月中的某一天
var month = currentDate.getDate();
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
//减去的天数
var minusDay = week != 0 ? week - 1 : 6;
//alert(minusDay);
//本周 周一
var monday = new Date(currentDate.getTime() - (minusDay * millisecond));
//本周 周日
var sunday = new Date(monday.getTime() + (6 * millisecond));
//添加本周时间
startStop.push(monday);//本周起始时间
//添加本周最后一天时间
startStop.push(sunday);//本周终止时间
//返回
return startStop;
}; this.getCurrentMonth = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//获得当前月份0-11
var currentMonth = currentDate.getMonth();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
//求出本月第一天
var firstDay = new Date(currentYear, currentMonth, 1);
//当为12月的时候年份需要加1
//月份需要更新为0 也就是下一年的第一个月
if (currentMonth == 11) {
currentYear++;
currentMonth = 0;//就为
} else {
//否则只是月份增加,以便求的下一月的第一天
currentMonth++;
}
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
//下月的第一天
var nextMonthDayOne = new Date(currentYear, currentMonth, 1);
//求出上月的最后一天
var lastDay = new Date(nextMonthDayOne.getTime() - millisecond);
//添加至数组中返回
startStop.push(firstDay);
startStop.push(lastDay);
//返回
return startStop;
}; this.getQuarterSeasonStartMonth = function (month) {
var quarterMonthStart = 0;
var spring = 0; //春
var summer = 3; //夏
var fall = 6; //秋
var winter = 9;//冬
//月份从0-11
if (month < 3) {
return spring;
}
if (month < 6) {
return summer;
}
if (month < 9) {
return fall;
}
return winter;
}; this.getMonthDays = function (year, month) {
//本月第一天 1-31
var relativeDate = new Date(year, month, 1);
//获得当前月份0-11
var relativeMonth = relativeDate.getMonth();
//获得当前年份4位年
var relativeYear = relativeDate.getFullYear();
//当为12月的时候年份需要加1
//月份需要更新为0 也就是下一年的第一个月
if (relativeMonth == 11) {
relativeYear++;
relativeMonth = 0;
} else {
//否则只是月份增加,以便求的下一月的第一天
relativeMonth++;
}
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
//下月的第一天
var nextMonthDayOne = new Date(relativeYear, relativeMonth, 1);
//返回得到上月的最后一天,也就是本月总天数
return new Date(nextMonthDayOne.getTime() - millisecond).getDate();
}; this.getCurrentSeason = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//获得当前月份0-11
var currentMonth = currentDate.getMonth();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
//获得本季度开始月份
var quarterSeasonStartMonth = this.getQuarterSeasonStartMonth(currentMonth);
//获得本季度结束月份
var quarterSeasonEndMonth = quarterSeasonStartMonth + 2;
//获得本季度开始的日期
var quarterSeasonStartDate = new Date(currentYear, quarterSeasonStartMonth, 1);
//获得本季度结束的日期
var quarterSeasonEndDate = new Date(currentYear, quarterSeasonEndMonth, this.getMonthDays(currentYear, quarterSeasonEndMonth));
//加入数组返回
startStop.push(quarterSeasonStartDate);
startStop.push(quarterSeasonEndDate);
//返回
return startStop;
}; this.getCurrentYear = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
//本年第一天
var currentYearFirstDate = new Date(currentYear, 0, 1);
//本年最后一天
var currentYearLastDate = new Date(currentYear, 11, 31);
//添加至数组
startStop.push(currentYearFirstDate);
startStop.push(currentYearLastDate);
//返回
return startStop;
}; this.getPriorMonthFirstDay = function (year, month) {
//年份为0代表,是本年的第一月,所以不能减
if (month == 0) {
month = 11;//月份为上年的最后月份
year--;//年份减1
return new Date(year, month, 1);
}
//否则,只减去月份
month--;
return new Date(year, month, 1);;
}; this.getPreviousMonth = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//获得当前月份0-11
var currentMonth = currentDate.getMonth();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
//获得上一个月的第一天
var priorMonthFirstDay = this.getPriorMonthFirstDay(currentYear, currentMonth);
//获得上一月的最后一天
var priorMonthLastDay = new Date(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth(), this.getMonthDays(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth()));
//添加至数组
startStop.push(priorMonthFirstDay);
startStop.push(priorMonthLastDay);
//返回
return startStop;
}; this.getPreviousWeek = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//返回date是一周中的某一天
var week = currentDate.getDay();
//返回date是一个月中的某一天
var month = currentDate.getDate();
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
//减去的天数
var minusDay = week != 0 ? week - 1 : 6;
//获得当前周的第一天
var currentWeekDayOne = new Date(currentDate.getTime() - (millisecond * minusDay));
//上周最后一天即本周开始的前一天
var priorWeekLastDay = new Date(currentWeekDayOne.getTime() - millisecond);
//上周的第一天
var priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (millisecond * 6));
//添加至数组
startStop.push(priorWeekFirstDay);
startStop.push(priorWeekLastDay);
return startStop;
}; this.getPriorSeasonFirstDay = function (year, month) {
var quarterMonthStart = 0;
var spring = 0; //春
var summer = 3; //夏
var fall = 6; //秋
var winter = 9;//冬 //月份从0-11
switch (Math.floor(month/3)) {//季度的其实月份
case 0:
//如果是第一季度则应该到去年的冬季
year--;
month = winter;
break;
case 1:
month = spring;
break;
case 2:
month = summer;
break;
case 3:
month = fall;
break;
};
return new Date(year, month, 1);
}; this.getPreviousSeason = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//获得当前月份0-11
var currentMonth = currentDate.getMonth();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
//上季度的第一天
var priorSeasonFirstDay = this.getPriorSeasonFirstDay(currentYear, currentMonth);
//上季度的最后一天
var priorSeasonLastDay = new Date(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2, this.getMonthDays(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2));
//添加至数组
startStop.push(priorSeasonFirstDay);
startStop.push(priorSeasonLastDay);
return startStop;
}; this.getPreviousYear = function () {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = this.getCurrentDate();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
currentYear--;
var priorYearFirstDay = new Date(currentYear, 0, 1);
var priorYearLastDay = new Date(currentYear, 11, 31);
//添加至数组
startStop.push(priorYearFirstDay);
startStop.push(priorYearLastDay);
return startStop;
};
};
//csdn 的 Meizz
/**
* 对Date的扩展,将 Date 转化为指定格式的String
* 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符
* 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
* eg:
* (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
* (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04
* (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04
* (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04
* (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
*/
Date.prototype.pattern = function (fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时
"H+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
var week = {
"0": "/u65e5",
"1": "/u4e00",
"2": "/u4e8c",
"3": "/u4e09",
"4": "/u56db",
"5": "/u4e94",
"6": "/u516d"
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
if (/(E+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/u661f/u671f" : "/u5468") : "") + week[this.getDay() + ""]);
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return fmt;
}
var my = new MrYangUtil();
window.onload = function () {
var sIpt = document.getElementById('sDate');
var eIpt = document.getElementById('eDate'); document.getElementById('sel').onchange = function () {
switch (this.value) {
case "cw":
sIpt.value = my.getCurrentWeek()[0].pattern("yyyy-MM-dd");
eIpt.value = my.getCurrentWeek()[1].pattern("yyyy-MM-dd");
break;
case "pw":
sIpt.value = my.getPreviousWeek()[0].pattern("yyyy-MM-dd");
eIpt.value = my.getPreviousWeek()[1].pattern("yyyy-MM-dd");
break;
case "cm":
sIpt.value = my.getCurrentMonth()[0].pattern("yyyy-MM-dd");
eIpt.value = my.getCurrentMonth()[1].pattern("yyyy-MM-dd");
break;
case "pm":
sIpt.value = my.getPreviousMonth()[0].pattern("yyyy-MM-dd");
eIpt.value = my.getPreviousMonth()[1].pattern("yyyy-MM-dd");
break;
case "cq":
sIpt.value = my.getCurrentSeason()[0].pattern("yyyy-MM-dd");
eIpt.value = my.getCurrentSeason()[1].pattern("yyyy-MM-dd");
break;
case "pq":
sIpt.value = my.getPreviousSeason()[0].pattern("yyyy-MM-dd");
eIpt.value = my.getPreviousSeason()[1].pattern("yyyy-MM-dd");
break;
case "cy":
sIpt.value = my.getCurrentYear()[0].pattern("yyyy-MM-dd");
eIpt.value = my.getCurrentYear()[1].pattern("yyyy-MM-dd");
break;
case "py":
sIpt.value = my.getPreviousYear()[0].pattern("yyyy-MM-dd");
eIpt.value = my.getPreviousYear()[1].pattern("yyyy-MM-dd");
break;
default:
break;
}
};
};
 <select id="sel">
<option value="cw">本周</option>
<option value="pw">上周</option>
<option value="cm">本月</option>
<option value="pm">上月</option>
<option value="cq">本季</option>
<option value="pq">上季</option>
<option value="cy">本年</option>
<option value="py">上年</option>
</select><br /><br/>
<input type="text" name="name" value=" " id="sDate" />至
<input type="text" name="name" value=" " id="eDate" />

JavaScript实现Select选择本周、上周、本季、上季、本年的时间选取的更多相关文章

  1. Javascript JQuery select选择之Safari与Firefox

    发现在苹果IOS手机及Safari浏览其中,如下代码不工作. $("#users option[value='hello']").attr("selected" ...

  2. [javascript][转载]jQuery获取Select选择的Text和 Value

    原文地址: http://www.cnblogs.com/yaoshiyou/archive/2010/08/24/1806939.html http://www.cnblogs.com/SAL292 ...

  3. javascript文件夹选择框的两种解决方案

    javascript文件夹选择框的两种解决方案 解决方案1:调用windows 的shell,但会有安全问题. * browseFolder.js * 该文件定义了BrowseFolder()函数,它 ...

  4. jQuery获取Select选择的Text和 Value(转)用时比较方便寻找

    ---恢复内容开始--- jQuery获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code. ...

  5. jQuery获取Select选择的Text和 Value(转)

    radio: radio: var item = $('input[name=items][checked]').val(); var item = $('input[name=items]:chec ...

  6. jQuery获取Select选择的Text和 Value

    jQuery获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code...});   //为Se ...

  7. Android较低版本(<5.2) 页面默认Select选择框效果的BUG解决

    Bug描述: 使用低版本安卓(<5.2),在微信上打开网页,点击下拉框,会出现如下图所示的用来展示select选项的弹出框: 在选项较少的时候,可以向下滑动,将选项滑到底部 滑动前: 滑动后: ...

  8. jQuery获取Select选择的Text和Value(详细汇总)

    语法解释: 1. $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 2. var ch ...

  9. jQuery获取Select选择的Text(非表单元素)和 Value(表单元素)(转)

    jQuery获取Select选择的Text和Value: 语法解释: . $("#select_id").change(function(){//code...}); //为Sel ...

随机推荐

  1. 函数指针使用演示样例(參考Linux-内核代码)

    本文有xhz1234(徐洪志)编写,转载请注明出处. http://blog.csdn.net/xhz1234/article/details/36635083 作者:徐洪志 近期阅读Linux-内核 ...

  2. C++零基础到入门

    (1)C语言概述 (2)编写.运行一个简单的C语言程序 (3)数据类型 (4)运算符和表达式 如果你对C语言一窍不通,那你就好好看这篇文章,我会力争让你真正的做到从零基础到入门,同时这篇文章会让你基本 ...

  3. live555直播

    http://www.cppblog.com/tx7do/archive/2014/05/31/207155.aspx http://blog.csdn.net/sunkwei/article/det ...

  4. ANALYSIS AND EXPLOITATION OF A LINUX KERNEL VULNERABILITY (CVE-2016-0728)

    ANALYSIS AND EXPLOITATION OF A LINUX KERNEL VULNERABILITY (CVE-2016-0728) By Perception Point Resear ...

  5. repeter中应用三元运算符

    应用情景一:根据ID显示名称例如:0代表启动,1:代表关闭例子如下 <td><%#Eval("ID").ToString() == "0" ? ...

  6. gradle找不到java目录里的mybatis的xml文件

    因为idea只编译java,gradle也默认只编译java,所以xml被忽略了. idea目前不知道如何修改,gradle修改时,需要把xml文件加上,不过gradle修改了只对gradle起作用, ...

  7. 九度OJ 1106:数字之和 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2505 解决:1706 题目描述: 对于给定的正整数 n,计算其十进制形式下所有位置数字之和,并计算其平方的各位数字之和. 输入: 每行输入 ...

  8. spawn类参数command详解

    我们主要来看spawn类它的构造方法参数主要有command,从字面上就是指spawn类的子程序用来执行的子程序,也就是系统所能够执行的相应的命令,对于command这个参数,我们是以字符串的方式给出 ...

  9. 记一次FastJSON和Jackson解析json时遇到的中括号问题

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/jadyer/article/details/24395015 完整版见https://jadyer. ...

  10. AndroidPageObjectTest_TimeOutManagement.java

    以下代码使用ApiDemos-debug.apk进行测试 //这个脚本用于演示PageFactory的功能:设置timeout时间. package com.saucelabs.appium; imp ...