JavaScript的日期处理
很久前写的代码了,偶尔看到贴出来做个备忘,写得有点乱,懒得整理了。
// 根据初始日期和滚动周期及滚动次数来计算终止日期,日期滚动周期,
// 可以是每天(DAY)、每周(WEEK)、每月(MONTH)、每年(YEAR)
// 返回一个日期字符串如'2009-5-9'
function getFinalDate(startDate,cycleModel,cycleTime,currDate){
var finalDate="";
if(validateDate(startDate) && validateDate(currDate)){
var str_a = startDate+" "+"00:00";
var tempDate = new Date(str_a.replace(/-/g,"/"));
var currDateStr = currDate+" "+"00:00";
var currentDate = new Date(currDateStr.replace(/-/g,"/"));
if(currentDate.getTime() != tempDate.getTime()){
cycleTime--;
}
var paramSdate = startDate.split("-");
var iyear = parseInt(paramSdate[0],10);
var imonth = parseInt(paramSdate[1],10);
var idate = parseInt(paramSdate[2],10);
if("DAY" == cycleModel){
var endDate = new Date();
endDate.setTime(tempDate.getTime()+cycleTime*24*60*60*1000);
var month = parseInt(endDate.getMonth(),10)+1;
month = month<10 ? "0"+month : month;
var day = parseInt(endDate.getDate(),10);
day = day<10 ? "0"+day : day;
finalDate = endDate.getYear()+"-"+month+"-"+day;
}else if("WEEK" == cycleModel){
var endDate = new Date();
endDate.setTime(tempDate.getTime()+cycleTime*7*24*60*60*1000);
var month = parseInt(endDate.getMonth(),10)+1;
month = month<10 ? "0"+month : month;
var day = parseInt(endDate.getDate(),10);
day = day<10 ? "0"+day : day;
finalDate = endDate.getYear()+"-"+month+"-"+day;
}else if("MONTH" == cycleModel){
var newDate;
for(var i=1; i<=cycleTime; i++){
if(imonth == 13){
iyear+=1;
imonth = 1;
}
newDate = new Date(iyear,imonth+1,0);
if(newDate.getDate() < idate){
i--;
}
imonth++;
if(imonth == 13){
iyear+=1;
imonth = 1;
}
}
imonth = imonth<10 ? "0"+imonth : imonth;
idate = idate<10 ? "0"+idate : idate;
finalDate = iyear + "-" + imonth + "-" + idate;
}else if("YEAR" == cycleModel){
var newDate2;
for(var j=1; j<=cycleTime; j++){
iyear++;
newDate2 = new Date(iyear,imonth,0);
if(newDate2.getDate() < idate){
j--;
}
}
imonth = imonth<10 ? "0"+imonth : imonth;
idate = idate<10 ? "0"+idate : idate;
finalDate = iyear + "-" + imonth + "-" + idate;
}
}
//alert(finalDate);
return finalDate;
}
// 根据周期模式及开始日期获取两个时间段内的周期次数
// 返回一个次数
function countCycleTimesFromPeriodDate(startDate,cycleModel,endDate,currDate){
var str_a = startDate+" "+"00:00";
var str_b = endDate+" "+"00:00";
var bDate = new Date(str_a.replace(/-/g,"/"));
var eDate = new Date(str_b.replace(/-/g,"/"));
var syear = parseInt(bDate.getYear(),10);
var smonth = parseInt(bDate.getMonth(),10)+1;
var sday = parseInt(bDate.getDate(),10);
var shour = parseInt(bDate.getHours(),10);
var eyear = parseInt(eDate.getYear(),10);
var emonth = parseInt(eDate.getMonth(),10)+1;
var eday = parseInt(eDate.getDate(),10);
var cycleTimes=0;
if(validateDate(startDate) && validateDate(endDate) && validateDate(currDate) && (eDate.getTime()-bDate.getTime())>=0){
var currDateStr = currDate+" "+"00:00";
var currentDate = new Date(currDateStr.replace(/-/g,"/"));
var tempparam = bDate.getTime() != currentDate.getTime() ? 1 : 0;
if("DAY" == cycleModel){
cycleTimes = Math.ceil((eDate.getTime()-bDate.getTime())/(24*60*60*1000));
cycleTimes = cycleTimes+tempparam;
}else if("WEEK" == cycleModel){
cycleTimes = Math.floor((eDate.getTime()-bDate.getTime())/(7*24*60*60*1000));
cycleTimes = cycleTimes+tempparam;
}else if("MONTH" == cycleModel){
if(eyear==syear){
var newDate;
smonth++;
for(; smonth<emonth; smonth++){
newDate = new Date(syear,smonth,0);
if(sday<=newDate.getDate()){
cycleTimes++;
}
}
var mt = ((smonth == emonth) && (eday >= sday)) ? 1 : 0;
cycleTimes += mt;
}else if(eyear>syear){
var newDate2;
smonth++;
for(; smonth<=12; smonth++){
newDate2 = new Date(syear,smonth,0);
if(sday<=newDate2.getDate()){
cycleTimes++;
}
}
//alert("first:"+cycleTimes);
if(eyear-syear-1 > 0){
var temp = eyear-syear-1;
syear++;
for(var n=1; n<=temp*12; n++){
newDate2 = new Date(syear,n,0);
if(sday<=newDate2.getDate()){
cycleTimes++;
}
if(n%12==0 && eyear-syear-1>0){
syear++;
}
}
//alert("middle:"+cycleTimes);
}
if(eyear-syear-1 == 0){
for(var k=1; k<=emonth; k++){
newDate2 = new Date(eyear,k,0);
if(k==emonth && sday<=newDate2.getDate() && sday<=eday){
cycleTimes++;
}else if(k<emonth && sday<=newDate2.getDate()){
cycleTimes++;
}
}
//alert("end:"+cycleTimes);
}
}
cycleTimes = cycleTimes+tempparam;
}else if("YEAR" == cycleModel){
var newDate3;
if(eyear>syear){
if(eyear-syear >= 1){
for(; syear<eyear; syear++){
newDate3 = new Date(syear,smonth,0);
if(newDate3.getDate() >= sday){
cycleTimes++;
}
}
if(0==tempparam){
cycleTimes--;
}
//alert("middle:"+cycleTimes);
}
if(syear == eyear && emonth>=smonth){
var tm = (emonth<smonth) ? 0: emonth>smonth ? 1 : (eday>=sday) ? 1 : 0;
newDate3 = new Date(eyear,smonth,0);
if(newDate3.getDate() >= sday){
cycleTimes += tm;
}
//alert("end:"+cycleTimes);
}
}else{
cycleTimes = cycleTimes+tempparam;
}
}
}else if(syear==eyear && smonth==emonth && sday==eday && eDate.getTime()-bDate.getTime()<0){
cycleTimes = 0;
}
return cycleTimes;
}
var dateRex = /^\d{4}[-]\d{1,2}[-]\d{1,2}[(](\u661f\u671f)[\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u65e5][)]$/;
var txtRex = /^([\u4e00-\u9fff]|[0-9]|[a-zA-Z]|[,。、“”!?:;]){1,60}$/;
var numRex = /^\d{1,10}$/;
// 比较用户历史提醒次数与现输入提醒次数迭加后是否超过十次,如果超过则以“yyyy年MM月、yyyy年MM月”的格式返回超过的月份
function compareMonthForReminderNum(beginDate,cycleModel,endDate,currDate,historyMap){
var resultStr = "";
if(validateDate(beginDate) && validateDate(endDate) && validateDate(currDate) && historyArr.length>0 && "" != cycleModel){
var dateMap = new Map();
if(compareDate(beginDate,endDate)){
var bDate = beginDate.split("-");
var eDate = endDate.split("-");
var sYear = parseInt(bDate[0],10);
var sMonth = parseInt(bDate[1],10);
var sDay = parseInt(bDate[2],10);
var eYear = parseInt(eDate[0],10);
var eMonth = parseInt(eDate[1],10);
var eDay = parseInt(eDate[2],10);
if((sYear==eYear) && (sMonth==eMonth)){
sMonth = sMonth < 10 ? "0"+sMonth : sMonth;
var mkey=sYear+"-"+sMonth;
var mvalue=countCycleTimesFromPeriodDate(beginDate,cycleModel,endDate,currDate);
dateMap.put(mkey,mvalue);
alert(dateMap.toString());
}else if((sYear==eYear) && (eMonth>sMonth)){
if("DAY"==cycleModel || "MONTH"==cycleModel || "YEAR"==cycleModel){
dateMap = new Map();
var counter = 0;
var newDate = new Date(sYear,sMonth,0);
var ed = sYear+"-"+sMonth+"-"+newDate.getDate();
var mkey = sYear+"-"+sMonth;
counter = countCycleTimesFromPeriodDate(beginDate,cycleModel,ed,currDate);
var mvalue = counter;
dateMap.put(mkey,mvalue);
var tempDate;
for(var i=sMonth+1; i<eMonth; i++){
tempDate = new Date(sYear,i,0);
var edDate = sYear+"-"+i+"-"+tempDate.getDate();
i = i<10 ? "0"+i : i;
var mkey=sYear+"-"+i;
counter = countCycleTimesFromPeriodDate(beginDate,cycleModel,edDate,currDate)-counter;
var mvalue = counter;
dateMap.put(mkey,mvalue);
}
var mkey=sYear+"-"+eMonth;
counter = countCycleTimesFromPeriodDate(beginDate,cycleModel,endDate,currDate)-counter;
var mvalue = counter;
dateMap.put(mkey,mvalue);
alert(dateMap.toString());
}else if("WEEK"==cycleModel){
dateMap = new Map();
var newDate = new Date(sYear,sMonth,0);
var ed = sYear+"-"+sMonth+"-"+newDate.getDate();
var mkey=sYear+"-"+sMonth;
var mvalue=countCycleTimesFromPeriodDate(beginDate,cycleModel,ed,currDate);
dateMap.put(mkey,mvalue);
var wDate = new Date(sYear,sMonth,sDay);
var weekIndex = wDate.getDay();
var tempDate;
for(var i=sMonth+1; i<eMonth; i++){
var count = 0;
tempDate = new Date(sYear,i,0);
var days = tempDate.getDate();
var dayDate;
for(var j=1; j<=days; j++){
dayDate = new Date(sYear,i,j);
if(weekIndex==dayDate.getDay()){
count++;
}
}
i = i<10 ? "0"+i : i;
var mkey=sYear+"-"+i;
var mvalue=count;
dateMap.put(mkey,mvalue);
}
var ct = 0;
for(var k=1; k<=eday; k++){
tempDate = new Date(sYear,emonth,k);
if(weekIndex==tempDate.getDay()){
ct++;
}
}
var mkey=sYear+"-"+eMonth;
var mvalue=ct;
dateMap.put(mkey,mvalue);
alert(dateMap.toString());
}
}else if(sYear<eYear){
if("DAY"==cycleModel || "MONTH"==cycleModel || "YEAR"==cycleModel){
dateMap = new Map();
var counter = 0;
var newDate = new Date(sYear,sMonth,0);
var ed = sYear+"-"+sMonth+"-"+newDate.getDate();
var mkey=sYear+"-"+sMonth;
counter = countCycleTimesFromPeriodDate(beginDate,cycleModel,ed,currDate);
var mvalue = counter;
dateMap.put(mkey,mvalue);
var tempDate;
for(var i=sMonth+1; i<=12; i++){
tempDate = new Date(sYear,i,0);
var edDate = sYear+"-"+i+"-"+tempDate.getDate();
i = i<10 ? "0"+i : i;
var mkey = sYear+"-"+i;
counter = countCycleTimesFromPeriodDate(beginDate,cycleModel,edDate,currDate)-counter;
var mvalue = counter;
dateMap.put(mkey,mvalue);
}
for(var j=sYear+1; j<eYear; j++){
for(var k=1; k<=12; k++){
tempDate = new Date(j,k,0);
var edDate = j+"-"+k+"-"+tempDate.getDate();
k = k<10 ? "0"+k : k;
var mkey = j+"-"+k;
counter = countCycleTimesFromPeriodDate(beginDate,cycleModel,edDate,currDate)-counter;
var mvalue = counter;
dateMap.put(mkey,mvalue);
}
}
for(var m=1; m<eMonth; m++){
tempDate = new Date(eYear,m,0);
var edDate = eYear+"-"+m+"-"+tempDate.getDate();
m = m<10 ? "0"+m : m;
var mkey = eYear+"-"+m;
counter = countCycleTimesFromPeriodDate(beginDate,cycleModel,edDate,currDate)-counter;
var mvalue = counter;
dateMap.put(mkey,mvalue);
}
var mkey = eYear+"-"+eMonth;
counter = countCycleTimesFromPeriodDate(beginDate,cycleModel,endDate,currDate)-counter;
var mvalue = counter;
dateMap.put(mkey,mvalue);
alert(dateMap.toString());
}else if("WEEK"==cycleModel){
dateMap = new Map();
var newDate = new Date(sYear,sMonth,0);
var ed = sYear+"-"+sMonth+"-"+newDate.getDate();
var mkey = sYear+"-"+sMonth;
var mvalue = countCycleTimesFromPeriodDate(beginDate,cycleModel,ed,currDate);
dateMap.put(mkey,mvalue);
var wDate = new Date(sYear,sMonth,sDay);
var weekIndex = wDate.getDay();
var tempDate;
for(var i=sMonth+1; i<=12; i++){
var count = 0;
tempDate = new Date(sYear,i,0);
var days = tempDate.getDate();
var dayDate;
for(var j=1; j<=days; j++){
dayDate = new Date(sYear,i,j);
if(weekIndex==dayDate.getDay()){
count++;
}
}
i = i<10 ? "0"+i : i;
var mkey = sYear+"-"+i;
var mvalue = count;
dateMap.put(mkey,mvalue);
}
for(var j=sYear+1; j<eYear; j++){
for(var k=1; k<=12; k++){
var count = 0;
tempDate = new Date(j,k,0);
var days = tempDate.getDate();
var dayDate;
for(var f=1; f<=days; f++){
dayDate = new Date(j,k,f);
if(weekIndex==dayDate.getDay()){
count++;
}
}
k = k<10 ? "0"+k : k;
var mkey = j+"-"+k;
var mvalue = count;
dateMap.put(mkey,mvalue);
}
}
for(var m=1; m<eMonth; m++){
var count = 0;
tempDate = new Date(eYear,m,0);
var days = tempDate.getDate();
var dayDate;
for(var f=1; f<=days; f++){
dayDate = new Date(eYear,m,f);
if(weekIndex==dayDate.getDay()){
count++;
}
}
m = m<10 ? "0"+m : m;
var mkey = eYear+"-"+m;
var mvalue = count;
dateMap.put(mkey,mvalue);
}
var ct = 0;
for(var n=1; n<=eday; n++){
tempDate = new Date(sYear,emonth,n);
if(weekIndex==tempDate.getDay()){
ct++;
}
}
var mkey = sYear+"-"+eMonth;
var mvalue = ct;
dateMap.put(mkey,mvalue);
alert(dateMap.toString());
}
}
var s = "";
dateMap.each(
function(key,value,index){
s += index+":"+ key+"="+value+"\n";
}
);
alert(s);
for(var dateStr in historyMap){
s += eval(dateStr)+"------"+historyMap[dateStr]+"\n";
}
alert(s);
for(var dateStr in historyMap){
var key = eval(dateStr);
var temp = parseInt(parseInt(historyMap[dateStr],10)+dateMap.get(key),10);
if(10<temp){
resultStr = "、"+key.toString().replace(/-/g,"年")+"月";
}
}
resultStr = resultStr;
}
}
return resultStr;
}
// 根据输入的日期字符串(yyyy-MM-dd)校验该日期是否正确
function validateDate(objDate){
var setDate = objDate;
var dateRex2 = /^\d{4}[-]\d{1,2}[-]\d{1,2}$/;
var paramDate = setDate.split("-");
var tempDate = setDate+" "+"00:00";
var sDate = new Date(tempDate.replace(/-/g,"/"));
var sYear = parseInt(paramDate[0],10);
var sMonth = parseInt(paramDate[1],10);
var sDay = parseInt(paramDate[2],10);
var eYear = parseInt(sDate.getYear(),10);
var eMonth = parseInt(sDate.getMonth(),10)+1;
var eDay = parseInt(sDate.getDate(),10);
if(dateRex2.test(setDate) && sYear==eYear && sMonth==eMonth && sDay==eDay){
return true;
}
return false;
}
// 根据输入的日期字符串(yyyy-MM-dd)转换成yyyy-MM-dd(E)的格式
function formatterDate(dateStr){
var newDate = dateStr;
if(validateDate(dateStr)){
var currDateStr = dateStr+" "+"00:00";
var paramSdate = newDate.split("-");
var iyear = parseInt(paramSdate[0],10);
var imonth = parseInt(paramSdate[1],10);
imonth = imonth < 10 ? "0"+imonth : imonth;
var idate = parseInt(paramSdate[2],10);
idate = idate < 10 ? "0"+idate : idate;
var currentDate = new Date(currDateStr.replace(/-/g,"/"));
var x = new Array("(星期日)", "(星期一)", "(星期二)");
var x = x.concat("(星期三)","(星期四)", "(星期五)");
var x = x.concat("(星期六)");
var day = currentDate.getDay();
return (iyear+"-"+imonth+"-"+idate+x[day]);
}else{
return newDate;
}
}
//判断页面元素是否为空
function handleParameterStr(obj){
var paramete=obj;
if('undefined'==paramete || null==paramete || 0==paramete.value.length || " "==paramete.value){
paramete="";
}else{
paramete=paramete.value;
}
return paramete;
}
//js实现类似JAVA的Map功能
function Map() {
/** 存放键的数组(遍历用到) */
this.keys = new Array();
/** 存放数据 */
this.data = new Object();
/**
* 放入一个键值对
* @param {String} key
* @param {Object} value
*/
this.put = function(key, value) {
if(this.data[key] == null){
this.keys.push(key);
}
this.data[key] = value;
};
/**
* 获取某键对应的值
* @param {String} key
* @return {Object} value
*/
this.get = function(key) {
return this.data[key];
};
/**
* 删除一个键值对
* @param {String} key
*/
this.remove = function(key) {
this.keys.remove(key);
this.data[key] = null;
};
/**
* 遍历Map,执行处理函数
*
* @param {Function} 回调函数 function(key,value,index){..}
*/
this.each = function(fn){
if(typeof fn != 'function'){
return;
}
var len = this.keys.length;
for(var i=0;i<len;i++){
var k = this.keys[i];
fn(k,this.data[k],i);
}
};
/**
* 获取键值数组(类似Java的entrySet())
* @return 键值对象{key,value}的数组
*/
this.entrys = function() {
var len = this.keys.length;
var entrys = new Array(len);
for (var i = 0; i < len; i++) {
entrys[i] = {
key : this.keys[i],
value : this.data[i]
};
}
return entrys;
};
/**
* 判断Map是否为空
*/
this.isEmpty = function() {
return this.keys.length == 0;
};
/**
* 获取键值对数量
*/
this.size = function(){
return this.keys.length;
};
/**
* 重写toString
*/
this.toString = function(){
var s = "{";
for(var i=0;i<this.keys.length;i++,s+=','){
var k = this.keys[i];
s += k+"="+this.data[k];
}
s+="}";
return s;
};
}
/**
* Map 使用示例
* var m = new Map();
* m.put('key1','Google');
* m.put('key2','baidu');
* m.put('key3','sohu');
* alert("init:"+m);
*
* m.put('key1','microsoft');
* alert("set key1:"+m);
*
* m.remove("key2");
* alert("remove key2: "+m);
*
* var s ="";
* m.each(function(key,value,index){
* s += index+":"+ key+"="+value+"\n";
* });
* alert(s);
*/
JavaScript的日期处理的更多相关文章
- 通过javascript的日期对象来得到当前的日期,并输出--内置对象---JS
//通过javascript的日期对象来得到当前的日期,并输出. var mydate = new Date(); var week = ["星期日","星期一" ...
- javascript 时间日期处理相加,减操作方法js
javascript 时间日期处理相加,减操作方法js function dateAddDays(dataStr,dayCount){ var strdate = dataStr; // 2017年0 ...
- 两行代码搞定 JavaScript 的日期验证
我们通常在 JavaScript 中验证日期,基本的思路大概是,先判断年月日是否有效,再判断当月是否有当日,比如一些月份没有 31 日,平年二月没有 29.30 日,闰年二月没有 30 日等等. 偶然 ...
- [JavaScript]配置日期选择控件
我选择的日期控件是:bootstrap-datepicker(下载路径:https://github.com/Aymkdn/Datepicker-for-Bootstrap) 比较方便,实用.原来是英 ...
- javascript类型系统——日期Date对象
× 目录 [1]静态方法 [2]构造函数 [3]实例方法 前面的话 Date对象是javascript语言中内置的数据类型,用于提供日期和时间的操作接口.Date对象是在早期java中的java.ut ...
- 9月23日JavaScript作业----日期时间选择
作业二:日期时间选择 <div style="width:600px; height:100px;"> <select id="year"&g ...
- JavaScript:日期选择器组件的使用
前言: 在实际项目开发中,日期选择是一个十分常见而且重要的问题,在表单中设计到日期的验证时,如果让用户自己输入时间的话,那么使用正则进行验证其正确性是不可取的,因为他一般只能验证日期的格式,无法准确的 ...
- javascript自定义日期函数
1.格式化日期(YYYY-MM-DD) 代码: var DateFormat = function (date) { if (!(date instanceof Date)) { date = dat ...
- javascript之日期对象
学习要点: 日期对象 将日期对象转换为字符串 将日期对象中的日期和时间转换为字符串 日期对象中的日期 日期对象中的时间 设置日期对象中的日期 设置日期对象中的时间 与毫秒相关的方法 一.日期对象 在j ...
- JavaScript Date日期对象以及日期格式化方法
前言 Date对象是javascript语言中内置的数据类型,用于提供日期和时间的操作接口.Date对象是在早期java中的java.util.Date类基础上创建的,为此,Date类型使用自UTC1 ...
随机推荐
- 网易云课堂_C++程序设计入门(上)_第3单元:更上一层楼 – 超越C的语法_第3单元作业【3】-在线编程(难度:中;10分)
1 本题要求实现两个重载的swap函数,每个swap函数都可交换3个整数a,b,c的值.将a的值存入b,b的值存入c,c的值存入a, 并且返回三个整数中最大的数的值.例如a,b,c的值为1,2,3,则 ...
- 浅析jquery中attr属性和prop属性的区别
最近在做项目的时候,发现到了prop这个属性,然后之前一直使用的是attr属性,觉得感觉上都差不多,jQuery也不可能专门做了两个相同的属性撒.所以就结合这两个属性研究了一下,也谈谈我对他们最简单最 ...
- Minimum Sum(思维)
Problem 1603 - Minimum Sum Time Limit: 2000MS Memory Limit: 65536KB Total Submit: 563 Accepted ...
- 《Effective C++ 》学习笔记——条款03
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- Sequence one(hdu2610dfs+去重)
题目:有一个数列N,和一个数字k,输出该数列的前k个子序列,如果k大于N的所有子序列,输出所有符合要求的序列,序列要求不能是递减序列 比如: 3 5 1 3 2 的前五个序列为 1 3 2 1 3 1 ...
- Flash Recovery Area
1. 设置闪回恢复区 闪回恢复区主要通过3个初始化参数来设置和管理 db_recovery_file_dest:指定闪回恢复区的位置db_recovery_file_dest_size:指定闪回恢复区 ...
- Web--->>>Cookie与Session
1.cookie 1.cookie是存在客户端(浏览器)的进程内存中和客户端所在的机器硬盘上 2.cookie只能能够存储少量文本,大概4K大小 3.cookie是不能在不同浏览器之间共享 3.创建c ...
- 初学.NET小技巧(不断更新)
1.快速打出Console.WriteLine : 输入cw,然后按两下tab键. 2.创建一个函数快捷键:bool b = IsPrimeNumber(); 把光标放到函数名上,Shift+Al ...
- eclipse自动提示类型的作用
eclipse的自动提示功能确实十分好用,但是只是笼统的都勾上了,所有会有好多重复项,看着很眼疼. 今天就稍微研究了下,略微知道了几个类型的作用: 序号 类型 大体的中文意思 作用 1 Java Ty ...
- String.Empty、string=”” 和null的区别
String.Empty是string类的一个静态常量: String.Empty和string=””区别不大,因为String.Empty的内部实现是: 1 2 3 4 5 6 7 8 9 10 1 ...