自己写的时间操作类,可以解决一些时间问题,就当是练练手了。
/*
*时间操作类
*作者:林坚
*主要功能:时间的格式化,时间的计算
*修改时间:2007-1-23
*随意转载,尊重版权。
*QQ 282514577
*Email lotin_2001@163.com
*还请大家多指教,有BUG通过一下,测试不是很认真
*/
function DateUtils(){
this.thisDate=null;
this.patternStr="";
this.monthArr=[["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"],
["January","February","March","April","May","June","July","August","September","October","November","December"]];
this.weekArr=[["星期天","星期一","星期二","星期三","星期四","星期五","星期六"],["SUN","MON","TUR","WED","THU","FRI","SAT"]];
/*
* 构造函数
* DateUtils() 默认当天日期
* DateUtils(date) 日期Date型
* DateUtils(datestr,fmtstr) 日期字符串 格式化字符串 (未实现)
* DateUtils(year,month,date) 年 月 日
* DateUtils(year,month,date,hour,minute,second) 年 月 日 时 分 秒
*/
switch(arguments.length){
case 0:{
this.thisDate=new Date();
this.patternStr="yyyy-MM-dd HH:mm:ss";
break;
}
case 1:{
if(typeof(arguments[0]) == "object" ){
this.thisDate=arguments[0];
}else{
this.thisDate=new Date();
}
this.patternStr="yyyy-MM-dd HH:mm:ss";
break;
}
case 2:{
this.thisDate=new Date();
this.patternStr="yyyy-MM-dd HH:mm:ss";
break;
}
case 3:{
this.thisDate=new Date();
this.thisDate.setFullYear(arguments[0]);
this.thisDate.setMonth(arguments[1]-1);
this.thisDate.setDate(arguments[2]);
this.thisDate.setHours(0);
this.thisDate.setMinutes(0);
this.thisDate.setSeconds(0);
this.patternStr="yyyy-MM-dd HH:mm:ss";
break;
}
case 6:{
this.thisDate=new Date();
this.thisDate.setFullYear(arguments[0]);
this.thisDate.setMonth(arguments[1]-1);
this.thisDate.setDate(arguments[2]);
this.thisDate.setHours(arguments[3]);
this.thisDate.setMinutes(arguments[4]);
this.thisDate.setSeconds(arguments[5]);
this.patternStr="yyyy-MM-dd HH:mm:ss";
break;
}
default:{
this.thisDate=new Date();
this.patternStr="yyyy-MM-dd HH:mm:ss";
}
}
if(typeof(this.thisDate) != "object")
throw (new Error(-1, '构造DateUtils方法失败,检查输入参数!'));
/*
* 通过时间字符串设置时间.
* 输入参数:时间字符串,时间匹配格式
*/
//0
this.setDateByString = function(dateStr,formatStr){
}
/*
* 设置描述日期格式的模式字符串
*/
//1
this.setFormatPattern = function(pattern){
this.patternStr=pattern;
}
/*
* 返回描述日期格式的模式字符串
*/
//2
this.getFormatPattern = function(){
return this.patternStr;
}
/*
* 返回格式化时间字符串,默认为"yyyy-MM-dd HH:mm:ss";
*
* y 年 Year (yyyy)1996 (yy)96
M 年中的月份 Month (MMMM)July (MMM)JUL; (MM)07
d 月份中的天数 Number (dd)10
H 小时数(0-23) HH 23
h 12小时制(1-12) hh 11
m 分钟数 mm 59
s 秒数 ss 01
E 星期中的天数 EE SUN EC 星期天
*/
//3
this.getDateByFormatPattern = function(){
var str=this.patternStr;
str=str.replace(/yyyy/g,String(this.thisDate.getFullYear()));
str=str.replace(/yy/g,String(this.thisDate.getFullYear()).substr(2,2));
str=str.replace(/MMMM/g,String(this.monthArr[1][this.thisDate.getMonth()]));
str=str.replace(/MMM/g,String(this.monthArr[0][this.thisDate.getMonth()]));
str=str.replace(/MM/g,String(this.getMonth()));
str=str.replace(/dd/g,String(this.getDate()));
str=str.replace(/HH/g,String(this.getHours()));
str=str.replace(/hh/g,String(this.getOneDightHours()));
str=str.replace(/mm/g,String(this.getMinutes()));
str=str.replace(/ss/g,String(this.getSeconds()));
str=str.replace(/EE/g,String(this.weekArr[1][this.thisDate.getDay()]));
str=str.replace(/EC/g,String(this.weekArr[0][this.thisDate.getDay()]));
return str;
}
//4
this.setDate = function(date){
this.thisDate=date;
}
/*
*返回Date 型的时间
*/
//5
this.getDate = function(){
return this.thisDate;
}
//6
this.formatZero = function(num){
if(num<10){
return '0'+num;
}
else
return num;
}
//7
this.getYear = function(){
return this.thisDate.getFullYear();
}
//8
this.setYear = function(year){
return this.thisDate.setFullYear(year);
}
//9
this.getMonth = function(){
return this.formatZero(this.thisDate.getMonth()+1);
}
//10
this.setMonth = function(month){
this.thisDate.setMonth(month-1);
}
//11
this.getDate = function(){
return this.formatZero(this.thisDate.getDate());
}
//12
this.setDate = function(day){
this.thisDate.setDate(day);
}
//13
this.getOneDightHours = function(){
return this.formatZero(this.changeToOneDightTime(this.getHours()));
}
//14
this.getHours = function(){
return this.formatZero(this.thisDate.getHours());
}
this.setHours = function(hours){
this.thisDate.setHours(hour);
}
//15
this.getMinutes = function(){
return this.formatZero(this.thisDate.getMinutes());
}
this.setMinutes = function(minutes){
this.thisDate.setMinutes(minutes);
}
//16
this.getSeconds = function(){
return this.formatZero(this.thisDate.getSeconds());
}
this.setSeconds = function(seconds){
this.thisDate.setSeconds(seconds);
}
//17
this.getMillisecond = function(){
return this.thisDate.getMilliseconds();
}
//18
this.changeToOneDightTime = function(date){
if(date==0){
return 12;
}else if((date>0)&&(date<=12)){
return date;
}else if((date>12)&&(date<=23)){
return date-12;
}else
return 12;
}
/*
* 年计算
* IN 操作数
* OUT 计算后的日期
*/
//19
this.addYear = function(num){
this.setYear(this.getYear()+num);
return this.getDateByFormatPattern();
}
/*
* 月份计算
* IN 操作数
* OUT 计算后的日期
*/
//20
this.addMonth = function(num){
this.setMonth(this.thisDate.getMonth()+1+num);
return this.getDateByFormatPattern();
}
/*
* 日计算
* IN 操作数
* OUT 计算后的日期
*/
//21
this.addDay = function(num){
this.setDate(this.thisDate.getDate()+num);
return this.getDateByFormatPattern();
}
this.getDateDistance = function (){
}
/*
* 计算两个时间的日期间隔数
* dateDistance(date) 返回输入日期与当前日期的间隔数。
* dateDistance(begin,end) 返回end与begin间隔数。
*/
this.dateDistance = function(){
switch(arguments.length){
case 1:{
arguments[0].setHours(0);
arguments[0].setMinutes(0);
arguments[0].setSeconds(0);
var testDate=new Date(this.thisDate.getFullYear(),this.thisDate.getMonth(),this.thisDate.getDate());
var distance=testDate.getTime()-arguments[0].getTime();
return Math.ceil(distance/86400000);
}
case 2:{
arguments[0].setHours(0);
arguments[0].setMinutes(0);
arguments[0].setSeconds(0);
arguments[1].setHours(0);
arguments[1].setMinutes(0);
arguments[1].setSeconds(0);
var distance=arguments[1].getTime();-arguments[0].getTime();
return Math.ceil(distance/86400000);
}
default:{
throw (new Error(-1, '调用dateDistance()方法失败,检查输入参数!'));
}
}
}
/*
*判断是否是闰年,返回true 或者 false
*/
this.isLeapYear = function(){
var year=this.getYear();
return (0==year%4 && ((year % 100 != 0)||(year % 400 == 0)));
};
/*
*返回该月天数
*/
this.getDaysOfMonth = function(){
return (new Date(this.getYear(),this.getMonth(),0)).getDate();
};
}