常用的js代码
//把 yyyy-MM-dd格式的字符串转换成日期
Date.toDate=function(date,time){
//20101020122012换成日期应该是2010-10-20 12:20:12
if(date.indexOf("-")<0){
var yearnum=date.substring(0,4);
var monthnum=date.substring(4,6);
var daynum=date.substring(6,8);
var hournum=date.substring(8,10);
var minutenum=date.substring(10,12);
var secondnum=date.substring(12,date.length-1);
return new Date(yearnum,monthnum-1,daynum,hournum,minutenum,secondnum);
}
var yearIndex=date.indexOf("-");
var year=date.substring(0,yearIndex);
date=date.substring(yearIndex+1);
var monthIndex=date.indexOf("-");
var month=date.substring(0,monthIndex);
date=date.substring(monthIndex+1);
var dayIndex=date.indexOf("-");
var day=date.substring(0,monthIndex);
if(typeof(time)=="undefined"||time==null)
return new Date(year,month-1,day);
else{
var hourIndex=time.indexOf(":");
var hour=time.substring(0,hourIndex);
time=time.substring(hourIndex+1);
var minuteIndex=time.indexOf(":");
var minute=time.substring(0,minuteIndex);
time=time.substring(minuteIndex+1);
var secondIndex=time.indexOf(":");
var second=time.substring(0,minuteIndex);
time=time.substring(secondIndex+1);
return new Date(year,month-1,day,hour,minute,second);
}
}
//比较两个日期是否为同一个日期(不比较具体的小时和分钟)
Date.prototype.isSameDay=function(data){
return this.format("yyyy-MM-dd")==data.format("yyyy-MM-dd")?true:false;
}
//计算和目标日期之间相差几天(无符号)
Date.prototype.DateDiffUnsigned=function(date){
return parseInt(Math.abs(date.getTime()-this.getTime())/86400000);
}
//计算和目标日期之间相差几天(有符号)
Date.prototype.DateDiff=function(date){
return parseInt((this.getTime()-date.getTime())/86400000);
}
//添加日期
Date.addDays=function(oldDate,days){
var newDate=new Date(oldDate.getFullYear(),oldDate.getMonth(),oldDate.getDate());
newDate.setDate(newDate.getDate()+days);
return newDate;
};
//日期回滚
Date.rollDays=function(oldDate,days){
var newDate=new Date(oldDate.getFullYear(),oldDate.getMonth(),oldDate.getDate());
newDate.setDate(newDate.getDate()-days);
return newDate;
}
//增加日期的天数
Date.prototype.addDay=function(days){
this.setDate(this.getDate()+days);
}
//日期回滚
Date.prototype.rollDay=function(days){
this.setDate(this.getDate()-days);
}
//获得当前日期是星期几
Date.prototype.getDayOfWeek=function(){
var array=new Array();
array[0]="星期天";
array[1]="星期一";
array[2]="星期二";
array[3]="星期三";
array[4]="星期四";
array[5]="星期五";
array[6]="星期六";
return array[this.getDay()];
}
String.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, "");
}
//给日期类型添加的一个格式化方法
Date.prototype.format = function(format) {
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
(this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length==1 ? o[k] :
("00"+ o[k]).substr((""+ o[k]).length));
return format;
};
//移除数组指定位置的元素
Array.prototype.removeAt = function(dx){
if(isNaN(dx)||dx>this.length){return false;}
this.splice(dx,1);
};