asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0

综合整理之后的PowerDate .方便处理常日公历日期操作.

<script language="JavaScript">
<!--
function PowerDate(){
/* 名称: PowerDate
 * 版本: v Beta 1.0
 * 作者: 梅雪香(meixx)
 * 说明: PowerDate 基本实现了较常见的对于日期的操作,但不包括农历.
 * 注意: 您可以随意复制此程序,请保留该说明.您可以随意更改此程序,如发现有BUG或者有改进请
 *       mail给我一份,我的E-mail:wy_hd@163.com 或者在CSDN给我留言,我的ID是:zhaoxiaoyang
 */
//日期对象
this.date=null;
//格式化时是否加零补位标志,默认为不加零false
this.isFmtZero=false;
this.weekArr=[["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
  ["SUN","MON","TUR","WED","THU","FRI","SAT"]];
this.monthArr=["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
//初始化日期对象
switch(arguments.length){
case 0:
this.date = new Date();
break;
case 1:
//如果为日期类型
if(typeof(arguments[0]) == "object" && (/Date/.test(arguments[0].constructor))){
this.date = arguments[0];
}else{//字符串类型或者毫秒数
var reg = /^(\d{2,4})\D+(\d{1,2})\D+(\d{1,2})$/;
var str = arguments[0].replace(/\s/g,"");
str = str.replace(reg,"$1/$2/$3");
this.date = new Date(str);
}
break;
case 2: //年月----此时初始化为年月的1号
this.date = new Date(arguments[0],arguments[1]-1,1);
break;
case 3: //年月日
this.date = new Date(arguments[0],arguments[1]-1,arguments[2]);
break;
case 6: //年月日时分秒
this.date = new Date(arguments[0],arguments[1]-1,arguments[2],arguments[3],arguments[4],arguments[5]);
break;
case 7: //年月日时分秒毫秒
this.date = new Date(arguments[0],arguments[1]-1,arguments[2],arguments[3],arguments[4],arguments[5],arguments[6]);
break;
default: this.date = new Date("1970/1/1"); break;
}
//初始化失败处理
if(typeof(this.date) != "object" || !(/Date/.test(this.date.constructor)))
throw (new Error(-1, '构造PowerDate方法失败,检查输入参数!'));

this.getDate = function (){
return this.date;
}
this.getFullYear=function(){
return this.date.getFullYear();
};
this.getYear=function(){
return this.date.getYear();
};
this.getMonth=function(){
return this.frmWithZero(this.date.getMonth()+1);
};
this.getDay=function(){
return this.frmWithZero(this.date.getDate());
};
this.getHour=function(){
return this.frmWithZero(this.date.getHours());
};
this.getMinute=function(){
return this.frmWithZero(this.date.getMinutes());
};
this.getSecond=function(){
return this.frmWithZero(this.date.getSeconds());
};
this.getMillisecond=function(){
var ss=this.date.getMilliseconds();
if(this.isFmtZero==true && ss<10)
return "00"+ss;
else if(this.isFmtZero==true && ss<100)
return "0"+ss;
else return ss;
};
this.getWeek=function(){
return this.date.getDay();
};
this.setIsFmtZero=function(val){
this.isFmtZero=val;
};
this.frmWithZero = function(num){
if(this.isFmtZero==true && num<10)
return "0"+num;
else return num;
}
/*
 * 功能:根据输入表达式返回日期字符串
 * 参数:dateFmt:字符串,由以下结构组成   
 *      yy:长写年,YY:短写年mm:数字月,MM:英文月,dd:日,hh:时,
 *      mi:分,ss秒,ms:毫秒,we:汉字星期,WE:英文星期.
*/
this.getString=function(dateFmt){
if(typeof(dateFmt) != "string" )
throw (new Error(-1, 'getString()方法需要字符串类型参数!'));
var str=dateFmt;
str=str.replace(/yy/g,this.getFullYear());
str=str.replace(/YY/g,this.getYear());
str=str.replace(/mm/g,this.getMonth());
str=str.replace(/MM/g,this.monthArr[this.getMonth()-1]);
str=str.replace(/dd/g,this.getDay());
str=str.replace(/hh/g,this.getHour());
str=str.replace(/mi/g,this.getMinute());
str=str.replace(/ss/g,this.getSecond());
str=str.replace(/ms/g,this.getMillisecond());
str=str.replace(/we/g,this.weekArr[0][this.getWeek()]);
str=str.replace(/WE/g,this.weekArr[1][this.getWeek()]);
return str;
};

/* 功能 : 返回与某日期相距N天(N个24小时)的日期
 * 参数 : num number类型 可以为正负整数或者浮点数,默认为1;
 *        type 0(秒) or 1(天),默认为秒
 * 返回 : 新的PowerDate类型
 */
this.dateAfter=function(num,type){
num = (num == null?1:num);
if(typeof(num)!="number") throw new Error(-1,"dateAfterDays(num)参数为数值类型.");
var iType = (type==null?0:type);
var arr = [1000,86400000];
var dd = this.date.valueOf();
dd += num*arr[type];
this.date=new Date(dd);
return this;
};
//判断是否是闰年,返回true 或者 false
this.isLeapYear = function (){
var year = this.getFullYear();
return (0==year%4 && ((year % 100 != 0)||(year % 400 == 0)));
};

//返回该月天数
this.getDaysOfMonth = function (){
return (new Date(this.getFullYear(),this.getMonth(),0)).getDate();
};

//转换成大写日期(中文)
this.getChinaDate =  function(){
var year = this.getFullYear();
var month= this.getMonth();
var day = this.getDay();
var arrNum = ["零","一","二","三","四","五","六","七","八","九","十","十一","十二"];
var strTmp="";
for(var i=0,j=year.length;i<j;i++){
strTmp += arrNum[year.charAt(i)];
}
strTmp += "年";
strTmp += arrNum[month]+"月";
if(day<10)
strTmp += arrNum[day];
else if (day <20)
strTmp += "十"+arrNum[day-10];
else if (day <30 )
strTmp += "二十"+arrNum[day-20];
else
strTmp += "三十"+arrNum[day-30];
strTmp += "日";
return strTmp;
};

//日期比较函数,参数为pd类型如this日期晚于参数:1,相等:0 早于: -1
this.dateCompare = function(pd){
if(typeof(pd) != "object" || pd.toString() != "PowerDate")
 throw new Error(-1,"dateCompare(pd)的pd参数为PowerDate类型.");
var d = this.date.getTime() - pd.getDate().getTime();
return d>0?1:(d==0?0:-1);
}
/*功能:返回两日期之差
 *参数:pd   PowerDate对象
 *    type: 返回类别标识.yy:年,mm:月,ww:周,dd:日,hh:小时,mi:分,ss:秒,ms:毫秒
 *    intOrFloat :返回整型还是浮点型值 0:整型,不等于0:浮点型
 *    output : 输出提示,如:时间差为#周!
 */
this.calDateDistance = function (pd,type,intOrFloat,output){
if(typeof(pd) != "object" || pd.toString() != "PowerDate")
 throw new Error(-1,"calDateDistance(pd,type,intOrFloat)的pd参数为PowerDate类型.");
if(!((new RegExp(type+",","g")).test("yy,mm,ww,dd,hh,mi,ss,ms,")))
 throw new Error(-1,"calDateDistance(pd,type,intOrFloat,output)的type参数为非法.");
var iof = (intOrFloat==null?0:intOrFloat);
var miSecMain = this.date.valueOf();
var miSecSub  = pd.getDate().valueOf();
var num=0;
switch(type){
case "yy": num = this.getFullYear() - pd.getFullYear(); break;
case "mm": num = (this.getFullYear() - pd.getFullYear())*12+this.getMonth()-pd.getMonth(); break;
case "ww": num = this.fmtRtnVal((miSecMain-miSecSub)/7/86400000,iof); break;
case "dd": num = this.fmtRtnVal((miSecMain-miSecSub)/86400000,iof); break;
case "hh": num = this.fmtRtnVal((miSecMain-miSecSub)/3600000,iof); break;
case "mi": num = this.fmtRtnVal((miSecMain-miSecSub)/60000,iof); break;
case "ss": num = this.fmtRtnVal((miSecMain-miSecSub)/1000,iof); break;
case "ms": num = (miSecMain-miSecSub);break;
default:  break;
}
if(output)
return output.replace(/#/g,num);
else return num;
};
//返回整数或者两位小数的浮点数
this.fmtRtnVal = function (val,intOrFloat){
//alert(val);
return (intOrFloat == 0 ? Math.floor(val) : parseInt(val*100)/100);
};
//重载toString 方法,用来判断是否为PowerDate对象
this.toString=function(){ return "PowerDate"};

}

var d= new PowerDate(new Date());//实例化一个PowerDate对象
d.setIsFmtZero(true);//设置为用0补位输出
//alert(d.getString("yy-mm-dd hh:mi:ss.ms"));
//alert(d.getString("ms.ss:mi:hh dd-mm-yy"));
alert(d.calDateDistance(new  PowerDate("2005/5/31"),"dd"));//输出日期字符串

//-->
</script>

 

新加的两个关于周数的方法
//根据当前日期所在年和周数返回周日的日期
Date.prototype.RtnByWeekNum = function (weekNum){
if(typeof(weekNum) != "number")
throw new Error(-1,"RtnByWeekNum(weekNum)的参数是数字类型.");
var date = new Date(this.getFullYear(),0,1);
var week = date.getDay();
week = (week==0?7:week);
return date.dateAfter(weekNum*7-week,1);
}
//根据日期返回该日期所在年的周数
Date.prototype.getWeekNum = function (){
var dat = new Date(this.getFullYear(),0,1);
var week = dat.getDay();
week = (week==0?7:week);
var days = this.calDateDistance(dat,"dd")+1;
return ((days + 6 - this.getDay() - 7 + week)/7);
}

posted on 2006-04-03 16:01 汪杰 阅读(384) 评论(0)  编辑 收藏 引用 所属分类: hengxing网站js
只有注册用户登录后才能发表评论。

<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 467490
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜