我的知识库

知识等于力量

« Java Web 框架,full stack概览一个人运作/零推广的网站开通第1月访问人数突破10万人的试验 »

Javascript关于日期的各种技巧和方法总结

<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>



/*
******************************************
日期函数扩充 
******************************************
*/



/*
===========================================
//转换成大写日期(中文)
===========================================
*/

Date.prototype.toCase 
= function()
{
var digits= new Array('零','一','二','三','四','五','六','七','八','九','十','十一','十二');
var unit= new Array('年','月','日','点','分','秒');

var year= this.getYear() + "";
var index;
var output="";

////////得到年
for (index=0;index<year.length;index++ )
{
output 
+= digits[parseInt(year.substr(index,1))];
}

output 
+=unit[0];

///////得到月
output +=digits[this.getMonth()] + unit[1];

///////得到日
switch (parseInt(this.getDate() / 10))
{
case 0:
output 
+=digits[this.getDate() % 10];
break;
case 1:
output 
+=digits[10+ ((this.getDate() % 10)>0?digits[(this.getDate() % 10)]:"");
break;
case 2:
case 3:
output 
+=digits[parseInt(this.getDate() / 10)] + digits[10]  + ((this.getDate() % 10)>0?digits[(this.getDate() % 10)]:"");
default:

break;
}

output 
+=unit[2];

///////得到时
switch (parseInt(this.getHours() / 10))
{
case 0:
output 
+=digits[this.getHours() % 10];
break;
case 1:
output 
+=digits[10+ ((this.getHours() % 10)>0?digits[(this.getHours() % 10)]:"");
break;
case 2:
output 
+=digits[parseInt(this.getHours() / 10)] + digits[10+ ((this.getHours() % 10)>0?digits[(this.getHours() % 10)]:"");
break;
}

output 
+=unit[3];

if(this.getMinutes()==0&&this.getSeconds()==0)
{
output 
+="";
return output;
}


///////得到分
switch (parseInt(this.getMinutes() / 10))
{
case 0:
output 
+=digits[this.getMinutes() % 10];
break;
case 1:
output 
+=digits[10+ ((this.getMinutes() % 10)>0?digits[(this.getMinutes() % 10)]:"");
break;
case 2:
case 3:
case 4:
case 5:
output 
+=digits[parseInt(this.getMinutes() / 10)] + digits[10+ ((this.getMinutes() % 10)>0?digits[(this.getMinutes() % 10)]:"");
break;
}

output 
+=unit[4];

if(this.getSeconds()==0)
{
output 
+="";
return output;
}


///////得到秒
switch (parseInt(this.getSeconds() / 10))
{
case 0:
output 
+=digits[this.getSeconds() % 10];
break;
case 1:
output 
+=digits[10+ ((this.getSeconds() % 10)>0?digits[(this.getSeconds() % 10)]:"");
break;
case 2:
case 3:
case 4:
case 5:
output 
+=digits[parseInt(this.getSeconds() / 10)] + digits[10+ ((this.getSeconds() % 10)>0?digits[(this.getSeconds() % 10)]:"");
break;
}

output 
+=unit[5];



return output;
}



/*
===========================================
//转换成农历
===========================================
*/

Date.prototype.toChinese 
= function()
{
//暂缺
}


/*
===========================================
//是否是闰年
===========================================
*/

Date.prototype.isLeapYear 
= function()
{
return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)));
}


/*
===========================================
//获得该月的天数
===========================================
*/

Date.prototype.getDayCountInMonth 
= function()
{
var mon = new Array(12);

    mon[
0= 31; mon[1= 28; mon[2= 31; mon[3= 30; mon[4]  = 31; mon[5]  = 30;
    mon[
6= 31; mon[7= 31; mon[8= 30; mon[9= 31; mon[10= 30; mon[11= 31;

if(0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0))&&this.getMonth()==2)
{
return 29;
}

else
{
return mon[this.getMonth()];
}

}


/*
===========================================
//获得日期为星期几 (0为星期天)
===========================================
*/

Date.prototype.weekOfDay 
= function()
{
return this.getDay();
}


/*
===========================================
//日期比较
===========================================
*/

Date.prototype.Compare 
= function(objDate)
{
if(typeof(objDate)!="object" && objDate.constructor != Date)
{
return -2;
}


var d = this.getTime() - objDate.getTime();

if(d>0)
{
return 1;