关于日期的问题也问的很多,但实现方法却大同小异,写了一个powerDate类
梅雪香 2005-10-28 01:58:41 嘿嘿,看了会失望的,一时之间也没想到有什么功能要实现的,所以只写了一个格式化的方法,希望大家来完善,或者提出功能来.
<script language="JavaScript">
<!--
function PowerDate(timeString){
this.date=null;
if(timeString!="") this.date=new Date(timeString);
else this.date=new Date();
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"];
this.getFullYear=function(){
return this.date.getFullYear();
};
this.getYear=function(){
return this.date.getYear();
};
this.getMonth=function(){
var mm=this.date.getMonth()+1;
if(this.isFmtZero==true && mm<10)
return "0"+mm;
else return mm;
};
this.getDay=function(){
var dd=this.date.getDate();
if(this.isFmtZero==true && dd<10)
return "0"+dd;
else return dd;
};
this.getHour=function(){
var hh=this.date.getHours();
if(this.isFmtZero==true && hh<10)
return "0"+hh;
else return hh;
};
this.getMinute=function(){
var mi=this.date.getMinutes();
if(this.isFmtZero==true && mi<10)
return "0"+mi;
else return mi;
};
this.getSecond=function(){
var ss=this.date.getSeconds();
if(this.isFmtZero==true && ss<10)
return "0"+ss;
else return ss;
};
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;
};
/*
功能:根据输入表达式返回日期字符串
参数:dateFmt:字符串,由以下结构组成 yy:长写年,YY:短写年mm:数字月,MM:英文月,dd:日,hh:时,mi:分,ss秒,ms:毫秒,we:汉字星期,WE:英文星期.
isFmtZero:布尔值,true:需要用0补位,false:不需要用0补位
*/
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天前(后)的日期
//返回与另一日期之间的时间间隔
//
}
PowerDate.prototype.fmtWithZero= function(){
}
var d= new PowerDate("");
d.setIsFmtZero(true);
alert(d.getString("yy-mm-dd hh:mi:ss.ms"));
//-->
</script>
<body>
</body>