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 ...
随机推荐
- Quartz框架的使用
专用词汇:scheduler:任务调度器trigger:触发器,用于定义任务调度时间规则job:任务,即被调度的任务misfire:错过的,指本来应该被执行但实际没有被执行的任务调度 Quartz核心 ...
- Android项目 手机安全卫士(代码最全,注释最详细)之十二 设置中心的界面
------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 按惯例,写在前面的:可能在学习Android的过程中,大家会和我一样,学习过大量的基础知识,很多的知识点 ...
- POJ 3311 Hie with the Pie(状压DP + Floyd)
题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...
- 蓝桥杯 BASIC 29 高精度加法(大数)
[思路]:大数处理都一样. [AC代码]:代码细节能够美化一下. #include <iostream> #include <algorithm> #include <c ...
- 【递归】【3月周赛1】【Problem B】
Problem B Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other) Total Sub ...
- Jquery实现鼠标hover图片遮罩弹出提示层特效
1.引入JS文件和所需要的样式文件 2.简单的写上以下18行代码就可以实现了 $(document).ready(function() { $("div.tip_trigger a.img& ...
- 关闭编译器FPO优化
// The release libs don't include FPO debug information, so FPO// optimization will interfere with s ...
- WireShark抓包软件的使用
在大学的时候学习的计算机网络的具体的知识,也使用过wireshark去抓取一些网络的封包用来分析,但是都没能系统的写一篇博客,今天总结一下吧. wireshark介绍 wireshark的官方下载网站 ...
- 使用md5判断网站内容是否被篡改
该脚本比较简单,判断网站根目录是否被篡改,如果被篡改把篡改的文件发送到管理员邮箱 #!/bin/bash #author:luodi date:// #use md5 to check web sit ...
- PYthon成长之路第一篇(1)__字符串初识
今天一起走进python的代码世界,一起领悟python的魅力 在很多学习python的书中都会以 print “Hello,world!” 这样的而一个程序为开始,那么其中的 hello,worl ...