关闭

js的时间操作方法

344人阅读 评论(0) 收藏 举报
分类:


1.js获取系统时间格式为YYYY-MM-DD HH:MM:SS

复制代码
 1 function curDateTime(){  
 2     var d = new Date();   
 3     var year = d.getYear();   
 4     var month = d.getMonth()+1;   
 5     var date = d.getDate();   
 6     var day = d.getDay();   
 7     var hours = d.getHours();   
 8     var minutes = d.getMinutes();   
 9     var seconds = d.getSeconds();   
10     var ms = d.getMilliseconds();     
11     var curDateTime= year;  
12     if(month>9)  
13     curDateTime = curDateTime +"-"+month;  
14     else  
15     curDateTime = curDateTime +"-0"+month;  
16     if(date>9)  
17     curDateTime = curDateTime +"-"+date;  
18     else  
19     curDateTime = curDateTime +"-0"+date;  
20     if(hours>9)  
21     curDateTime = curDateTime +" "+hours;  
22     else  
23     curDateTime = curDateTime +" 0"+hours;  
24     if(minutes>9)  
25     curDateTime = curDateTime +":"+minutes;  
26     else  
27     curDateTime = curDateTime +":0"+minutes;  
28     if(seconds>9)  
29     curDateTime = curDateTime +":"+seconds;  
30     else  
31     curDateTime = curDateTime +":0"+seconds;  
32     return curDateTime;   
33     }  
复制代码

可以用alert(curDateTime())进行测试

 

2.js对两个YYYY-MM-DD HH:MM:SS时间进行比较

复制代码
 1 function comptime(beginTime, endTime){  
 2     //var beginTime = "2009-09-21 00:00:00";  
 3 //var endTime = "2009-09-21 00:00:01";  
 4     var beginTimes=beginTime.substring(0,10).split('-');  
 5     var endTimes=endTime.substring(0,10).split('-');  
 6     beginTime=beginTimes[1]+'-'+beginTimes[2]+'-'+beginTimes[0]+' '+beginTime.substring(10,19);  
 7     endTime=endTimes[1]+'-'+endTimes[2]+'-'+endTimes[0]+' '+endTime.substring(10,19);  
 8     //alert(Date.parse(endTime));  
 9 //alert(Date.parse(beginTime));   
10     var a =(Date.parse(endTime)-Date.parse(beginTime))/3600/1000;  
11     if(a<0){  
12         //alert("endTime小!"); 
13         return "small";
14     }else if (a>0){  
15         //alert("endTime大!");  
16         return "big";
17     }else if (a==0){  
18         //alert("时间相等!"); 
19         return "equal";
20     }else{  
21         return 'exception'  
22     }  
23     }  
复制代码

3.js比较两个YYYY-MM-DD 的时间格式

复制代码
 1 03.function duibi(a,b)  
 2 04.{  
 3 05.var arr=a.split("-");  
 4 06.var starttime=new Date(arr[0],arr[1],arr[2]);  
 5 07.var starttimes=starttime.getTime();  
 6 08.var arrs=b.split("-");  
 7 09.var lktime=new Date(arrs[0],arrs[1],arrs[2]);  
 8 10.var lktimes=lktime.getTime();  
 9 11.if(starttimes>=lktimes)  
10 12.{  
11 13.alert('开始时间大于离开时间,请检查');  
12 14.return false;  
13 15.}  
14 16.else  
15 17.return true;  
16 18.}  
复制代码


4.以下是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(); //获取当前日期 
var mytime=myDate.toLocaleTimeString(); //获取当前时间 
myDate.toLocaleString( ); //获取日期与时间 
日期时间脚本库方法列表 
Date.prototype.isLeapYear 判断闰年
  Date.prototype.Format 日期格式化 
Date.prototype.DateAdd 日期计算 
Date.prototype.DateDiff 比较日期差 
Date.prototype.toString 日期转字符串
  Date.prototype.toArray 日期分割为数组 
Date.prototype.DatePart 取日期的部分信息 
Date.prototype.MaxDayOfDate 取日期所在月的最大天数 
Date.prototype.WeekNumOfYear 判断日期所在年的第几周 
StringToDate 字符串转日期型 IsValidDate 验证日期有效性 
CheckDateTime 完整日期时间检查 
daysBetween 日期天数差

以下是代码示例:

复制代码
  1 //--------------------------------------------------- 
  2 // 判断闰年 
  3 //--------------------------------------------------- 
  4 Date.prototype.isLeapYear = function() 
  5 { 
  6 return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)));
  7  } 
  8 
  9 //--------------------------------------------------- 
 10 // 日期格式化 
 11 // 格式 YYYY/yyyy/YY/yy 表示年份 
 12 // MM/M 月份 
 13 // W/w 星期 
 14 // dd/DD/d/D 日期 
 15 // hh/HH/h/H 时间 
 16 // mm/m 分钟 
 17 // ss/SS/s/S 秒 
 18 //--------------------------------------------------- 
 19 Date.prototype.Format = function(formatStr) 
 20 { 
 21 var str = formatStr; 
 22 var Week = ['日','一','二','三','四','五','六']; 
 23 
 24 str=str.replace(/yyyy|YYYY/,this.getFullYear()); 
 25 str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));
 26  
 27 str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth());
 28  str=str.replace(/M/g,this.getMonth()); 
 29 
 30 str=str.replace(/w|W/g,Week[this.getDay()]); 
 31 
 32 str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
 33  str=str.replace(/d|D/g,this.getDate()); 
 34 
 35 str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
 36  str=str.replace(/h|H/g,this.getHours()); 
 37 str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
 38  str=str.replace(/m/g,this.getMinutes()); 
 39 
 40 str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
 41  str=str.replace(/s|S/g,this.getSeconds()); 
 42 
 43 return str; 
 44 } 
 45 
 46 //+--------------------------------------------------- 
 47 //| 求两个时间的天数差 日期格式为 YYYY-MM-dd 
 48 //+--------------------------------------------------- 
 49 function daysBetween(DateOne,DateTwo) 
 50 { 
 51 var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-')); 
 52 var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1); 
 53 var OneYear = DateOne.substring(0,DateOne.indexOf ('-')); 
 54 
 55 var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-')); 
 56 var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1); 
 57 var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-')); 
 58 
 59 var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
 60  return Math.abs(cha); 
 61 } 
 62 
 63 
 64 //+--------------------------------------------------- 
 65 //| 日期计算 
 66 //+--------------------------------------------------- 
 67 Date.prototype.DateAdd = function(strInterval, Number) { 
 68 var dtTmp = this; 
 69 switch (strInterval) { 
 70 case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number)); 
 71 case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number)); 
 72 case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number)); 
 73 case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number)); 
 74 case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number)); 
 75 case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
 76  case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
 77  case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds());
 78  } 
 79 } 
 80 
 81 //+--------------------------------------------------- 
 82 //| 比较日期差 dtEnd 格式为日期型或者 有效日期格式字符串 
 83 //+--------------------------------------------------- 
 84 Date.prototype.DateDiff = function(strInterval, dtEnd) { 
 85 var dtStart = this; 
 86 if (typeof dtEnd == 'string' )//如果是字符串转换为日期型 
 87 { 
 88 dtEnd = StringToDate(dtEnd); 
 89 } 
 90 switch (strInterval) { 
 91 case 's' :return parseInt((dtEnd - dtStart) / 1000); 
 92 case 'n' :return parseInt((dtEnd - dtStart) / 60000); 
 93 case 'h' :return parseInt((dtEnd - dtStart) / 3600000); 
 94 case 'd' :return parseInt((dtEnd - dtStart) / 86400000); 
 95 case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7)); 
 96 case 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1);
 97  case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear(); 
 98 } 
 99 } 
100 
101 //+--------------------------------------------------- 
102 //| 日期输出字符串,重载了系统的toString方法 
103 //+--------------------------------------------------- 
104 Date.prototype.toString = function(showWeek) 
105 { 
106 var myDate= this; 
107 var str = myDate.toLocaleDateString(); 
108 if (showWeek) 
109 { 
110 var Week = ['日','一','二','三','四','五','六']; 
111 str += ' 星期' + Week[myDate.getDay()]; 
112 } 
113 return str; 
114 } 
115 
116 //+--------------------------------------------------- 
117 //| 日期合法性验证 
118 //| 格式为:YYYY-MM-DD或YYYY/MM/DD 
119 //+--------------------------------------------------- 
120 function IsValidDate(DateStr) 
121 { 
122 var sDate=DateStr.replace(/(^\s+|\s+$)/g,''); //去两边空格; 
123 if(sDate=='') return true; 
124 //如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为'' 
125 //数据库中,合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式 
126 var s = sDate.replace(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,'');
127  if (s=='') //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D 
128 { 
129 var t=new Date(sDate.replace(/\-/g,'/')); 
130 var ar = sDate.split(/[-/:]/); 
131 if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate()) 
132 { 
133 //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。'); 
134 return false; 
135 } 
136 } 
137 else 
138 { 
139 //alert('错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。'); 
140 return false; 
141 } 
142 return true; 
143 } 
144 
145 //+--------------------------------------------------- 
146 //| 日期时间检查 
147 //| 格式为:YYYY-MM-DD HH:MM:SS 
148 //+--------------------------------------------------- 
149 function CheckDateTime(str) 
150 { 
151 var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/; 
152 var r = str.match(reg); 
153 if(r==null)return false; 
154 r[2]=r[2]-1; 
155 var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]); 
156 if(d.getFullYear()!=r[1])return false; 
157 if(d.getMonth()!=r[2])return false; 
158 if(d.getDate()!=r[3])return false; 
159 if(d.getHours()!=r[4])return false; 
160 if(d.getMinutes()!=r[5])return false; 
161 if(d.getSeconds()!=r[6])return false; 
162 return true; 
163 } 
164 
165 //+--------------------------------------------------- 
166 //| 把日期分割成数组 
167 //+--------------------------------------------------- 
168 Date.prototype.toArray = function() 
169 { 
170 var myDate = this; 
171 var myArray = Array(); 
172 myArray[0] = myDate.getFullYear(); 
173 myArray[1] = myDate.getMonth(); 
174 myArray[2] = myDate.getDate(); 
175 myArray[3] = myDate.getHours(); 
176 myArray[4] = myDate.getMinutes(); 
177 myArray[5] = myDate.getSeconds(); 
178 return myArray; 
179 } 
180 
181 //+--------------------------------------------------- 
182 //| 取得日期数据信息 
183 //| 参数 interval 表示数据类型 
184 //| y 年 m月 d日 w星期 ww周 h时 n分 s秒 
185 //+--------------------------------------------------- 
186 Date.prototype.DatePart = function(interval) 
187 { 
188 var myDate = this; 
189 var partStr=''; 
190 var Week = ['日','一','二','三','四','五','六']; 
191 switch (interval) 
192 { 
193 case 'y' :partStr = myDate.getFullYear();break; 
194 case 'm' :partStr = myDate.getMonth()+1;break; 
195 case 'd' :partStr = myDate.getDate();break; 
196 case 'w' :partStr = Week[myDate.getDay()];break; 
197 case 'ww' :partStr = myDate.WeekNumOfYear();break; 
198 case 'h' :partStr = myDate.getHours();break; 
199 case 'n' :partStr = myDate.getMinutes();break; 
200 case 's' :partStr = myDate.getSeconds();break; 
201 } 
202 return partStr; 
203 } 
204 
205 //+--------------------------------------------------- 
206 //| 取得当前日期所在月的最大天数 
207 //+--------------------------------------------------- 
208 Date.prototype.MaxDayOfDate = function() 
209 { 
210 var myDate = this; 
211 var ary = myDate.toArray(); 
212 var date1 = (new Date(ary[0],ary[1]+1,1)); 
213 var date2 = date1.dateAdd(1,'m',1); 
214 var result = dateDiff(date1.Format('yyyy-MM-dd'),date2.Format('yyyy-MM-dd')); 
215 return result; 
216 } 
217 
218 //+--------------------------------------------------- 
219 //| 取得当前日期所在周是一年中的第几周 
220 //+--------------------------------------------------- 
221 Date.prototype.WeekNumOfYear = function() 
222 { 
223 var myDate = this; 
224 var ary = myDate.toArray(); 
225 var year = ary[0]; 
226 var month = ary[1]+1; 
227 var day = ary[2]; 
228 document.write('< script language=VBScript\> \n'); 
229 document.write('myDate = DateValue(''+month+'-'+day+'-'+year+'') \n'); 
230 document.write('result = DatePart('ww', myDate) \n'); 
231 document.write(' \n'); 
232 return result; 
233 } 
234 
235 //+--------------------------------------------------- 
236 //| 字符串转成日期类型 
237 //| 格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd 
238 //+--------------------------------------------------- 
239 function StringToDate(DateStr) 
240 { 
241 
242 var converted = Date.parse(DateStr); 
243 var myDate = new Date(converted); 
244 if (isNaN(myDate)) 
245 { 
246 //var delimCahar = DateStr.indexOf('/')!=-1?'/':'-'; 
247 var arys= DateStr.split('-'); 
248 myDate = new Date(arys[0],--arys[1],arys[2]); 
249 } 
250 return myDate; 
251 }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:57556次
    • 积分:1389
    • 等级:
    • 排名:千里之外
    • 原创:78篇
    • 转载:64篇
    • 译文:0篇
    • 评论:3条
    最新评论