js 获取系统当前时间

JS获取当前的日期和时间的方法:
var myDate = new Date();
myDate.getYear(); //获取当前年份(2位)
myDate.getFullYear(); //获取完整的年份(4位,1970-????)
myDate.getMonth(); //获取当前月份(0-11,0代表1月)
myDate.getDate(); //获取当前日(1-31)
myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
myDate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数)
myDate.getHours(); //获取当前小时数(0-23)
myDate.getMinutes(); //获取当前分钟数(0-59)
myDate.getSeconds(); //获取当前秒数(0-59)
myDate.getMilliseconds(); //获取当前毫秒数(0-999)
myDate.toLocaleDateString(); //获取当前日期(2015/8/13)
myDate.toLocaleTimeString(); //获取当前时间(下午:12:08:06)
myDate.toLocaleString( ); //获取日期与时间(2015/8/13 下午2:38:03)



日期时间脚本库方法列表:

(1)Date.prototype.isLeapYear 判断闰年
(2)Date.prototype.Format 日期格式化
(3)Date.prototype.DateAdd 日期计算
(4)Date.prototype.DateDiff 比较日期差
(5)Date.prototype.toString 日期转字符串
(6)Date.prototype.toArray 日期分割为数组
(7)Date.prototype.DatePart 取日期的部分信息
(8)Date.prototype.MaxDayOfDate 取日期所在月的最大天数
(9)Date.prototype.WeekNumOfYear 判断日期所在年的第几周
(10)StringToDate 字符串转日期型
(11)IsValidDate 验证日期有效性
(12)CheckDateTime 完整日期时间检查
(13)daysBetween 日期天数差

 

(1)判断闰年 

 

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

 

(2)日期格式化

 

 

 格式 YYYY/yyyy/YY/yy 表示年份
MM/M 月份
W/w 星期
dd/DD/d/D 日期
hh/HH/h/H 时间
mm/m 分钟
ss/SS/s/S 秒
 1  Date.prototype.Format = function(formatStr)
 2         {
 3             var str = formatStr;
 4             var Week = ['日','一','二','三','四','五','六'];
 5 
 6             str=str.replace(/yyyy|YYYY/,this.getFullYear());
 7             str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
 8 
 9             str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth());
10             str=str.replace(/M/g,this.getMonth());
11 
12             str=str.replace(/w|W/g,Week[this.getDay()]);
13 
14             str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
15             str=str.replace(/d|D/g,this.getDate());
16 
17             str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
18             str=str.replace(/h|H/g,this.getHours());
19             str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
20             str=str.replace(/m/g,this.getMinutes());
21 
22             str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
23             str=str.replace(/s|S/g,this.getSeconds());
24 
25             return str;
26         }

(3)求两个时间的天数差 日期格式为 YYYY-MM-dd

 

 1 function daysBetween(DateOne,DateTwo)
 2         {
 3             var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-'));
 4             var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1);
 5             var OneYear = DateOne.substring(0,DateOne.indexOf ('-'));
 6 
 7             var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-'));
 8             var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1);
 9             var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-'));
10 
11             var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
12             return Math.abs(cha);
13         }

 

(4)日期计算

 1 Date.prototype.DateAdd = function(strInterval, Number) {
 2             var dtTmp = this;
 3             switch (strInterval) {
 4                 case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number));
 5                 case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number));
 6                 case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number));
 7                 case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number));
 8                 case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number));
 9                 case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
10                 case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
11                 case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
12             }
13         }

(5)比较日期差 dtEnd 格式为日期型或者有效日期格式字符串

 1 Date.prototype.DateDiff = function(strInterval, dtEnd) {
 2             var dtStart = this;
 3             if (typeof dtEnd == 'string' )//如果是字符串转换为日期型
 4             {
 5                 dtEnd = StringToDate(dtEnd);
 6             }
 7             switch (strInterval) {
 8                 case 's' :return parseInt((dtEnd - dtStart) / 1000);
 9                 case 'n' :return parseInt((dtEnd - dtStart) / 60000);
10                 case 'h' :return parseInt((dtEnd - dtStart) / 3600000);
11                 case 'd' :return parseInt((dtEnd - dtStart) / 86400000);
12                 case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7));
13                 case 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1);
14                 case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear();
15             }
16         }

(6)日期输出字符串,重载了系统的toString方法

 1 Date.prototype.toString = function(showWeek)
 2         {
 3             var myDate= this;
 4             var str = myDate.toLocaleDateString();
 5             if (showWeek)
 6             {
 7                 var Week = ['日','一','二','三','四','五','六'];
 8                 str += ' 星期' + Week[myDate.getDay()];
 9             }
10             return str;
11         }

(7)日期合法性验证

格式为:YYYY-MM-DD或YYYY/MM/DD
 1 function IsValidDate(DateStr)
 2         {
 3             var sDate=DateStr.replace(/(^\s+|\s+$)/g,''); //去两边空格;
 4             if(sDate=='') return true;
 5             //如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为''
 6             //数据库中,合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式
 7             var s = sDate.replace(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,'');
 8             if (s=='') //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D
 9             {
10                 var t=new Date(sDate.replace(/\-/g,'/'));
11                 var ar = sDate.split(/[-/:]/);
12                 if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate())
13                 {
14                     //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
15                     return false;
16                 }
17             }
18             else
19             {
20                 //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
21                 return false;
22             }
23             return true;
24         }

(8)日期时间检查

 1 function CheckDateTime(str)
 2         {
 3             var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/;
 4             var r = str.match(reg);
 5             if(r==null)return false;
 6             r[2]=r[2]-1;
 7             var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]);
 8             if(d.getFullYear()!=r[1])return false;
 9             if(d.getMonth()!=r[2])return false;
10             if(d.getDate()!=r[3])return false;
11             if(d.getHours()!=r[4])return false;
12             if(d.getMinutes()!=r[5])return false;
13             if(d.getSeconds()!=r[6])return false;
14             return true;
15         }
(9)把日期分割成数组
 1 Date.prototype.toArray = function()
 2         {
 3             var myDate = this;
 4             var myArray = Array();
 5             myArray[0] = myDate.getFullYear();
 6             myArray[1] = myDate.getMonth();
 7             myArray[2] = myDate.getDate();
 8             myArray[3] = myDate.getHours();
 9             myArray[4] = myDate.getMinutes();
10             myArray[5] = myDate.getSeconds();
11             return myArray;
12         }
(10)取得日期数据信息
参数 interval 表示数据类型

y 年 m月 d日 w星期 ww周 h时 n分 s秒
 1 Date.prototype.DatePart = function(interval)
 2         {
 3             var myDate = this;
 4             var partStr='';
 5             var Week = ['日','一','二','三','四','五','六'];
 6             switch (interval)
 7             {
 8                 case 'y' :partStr = myDate.getFullYear();break;
 9                 case 'm' :partStr = myDate.getMonth()+1;break;
10                 case 'd' :partStr = myDate.getDate();break;
11                 case 'w' :partStr = Week[myDate.getDay()];break;
12                 case 'ww' :partStr = myDate.WeekNumOfYear();break;
13                 case 'h' :partStr = myDate.getHours();break;
14                 case 'n' :partStr = myDate.getMinutes();break;
15                 case 's' :partStr = myDate.getSeconds();break;
16             }
17             return partStr;
18         }
(11)取得当前日期所在月的最大天数
1 Date.prototype.MaxDayOfDate = function()
2         {
3             var myDate = this;
4             var ary = myDate.toArray();
5             var date1 = (new Date(ary[0],ary[1]+1,1));
6             var date2 = date1.dateAdd(1,'m',1);
7             var result = dateDiff(date1.Format('yyyy-MM-dd'),date2.Format('yyyy-MM-dd'));
8             return result;
9         }
(12)取得当前日期所在周是一年中的第几周  
 1 Date.prototype.WeekNumOfYear = function()  
 2 {   
 3     var myDate = this;  
 4     var ary = myDate.toArray();  
 5     var year = ary[0];  
 6     var month = ary[1]+1;  
 7     var day = ary[2];  
 8     document.write('< script language=VBScript\> \n');  
 9     document.write('myDate = Datue(''+month+'-'+day+'-'+year+'') \n');  
10     document.write('result = DatePart('ww', myDate) \n');  
11     document.write(' \n');  
12     return result;  
13 }  
(13)字符串转成日期类型
格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd
 1 function StringToDate(DateStr)
 2         {
 3 
 4             var converted = Date.parse(DateStr);
 5             var myDate = new Date(converted);
 6             if (isNaN(myDate))
 7             {
 8                 //var delimCahar = DateStr.indexOf('/')!=-1?'/':'-';
 9                 var arys= DateStr.split('-');
10                 myDate = new Date(arys[0],--arys[1],arys[2]);
11             }
12             return myDate;
13         }



若要显示:当前日期加时间(如:2009-06-12 12:00)

 

 1 function CurentTime()
 2         {
 3             var now = new Date();
 4 
 5             var year = now.getFullYear();       //
 6             var month = now.getMonth() + 1;     //
 7             var day = now.getDate();            //
 8 
 9             var hh = now.getHours();            //
10             var mm = now.getMinutes();          //
11 
12             var clock = year + "-";
13 
14             if(month < 10)
15                 clock += "0";
16 
17             clock += month + "-";
18 
19             if(day < 10)
20                 clock += "0";
21 
22             clock += day + " ";
23 
24             if(hh < 10)
25                 clock += "0";
26 
27             clock += hh + ":";
28             if (mm < 10) clock += '0';
29             clock += mm;
30             return(clock);
31         }

 

 

 
posted @ 2015-08-13 15:34  不得不爱xxy  阅读(954)  评论(0编辑  收藏  举报