日期:2014-05-16  浏览次数:20328 次

js时间验证(转)

?

http://demojava.iteye.com/blog/858349

?

// 验证输入的日期??

?

http://jeemiss.iteye.com/blog/507185?

?

验证短日期(2007-06-05)?
function strDateTime(str)?
{?
?? var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);??
?? if(r==null)return false;??
??????? var d= new Date(r[1], r[3]-1, r[4]);??
??????? return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);?
}?

验证长日期(2007-06-05 10:57:10)?
function strDateTime(str)?
{?
?? var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;??
?? var r = str.match(reg);??
?? if(r==null)return false;??
?? var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);??
?? return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);
}??


验证时间(10:57:10)?
function isTime(str)?
{?
?? var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);?
?? if (a == null) {alert(’输入的参数不是时间格式’); return false;}?
?? if (a[1]>24 || a[3]>60 || a[4]>60)?
?? {?
?????? alert("时间格式不对");?
?????? return false?
?? }?
??? return true;?
}?

比较两个日期大小?
function compareDate(d1,d2)?
{?
??? return ((new Date(d1.replace(/-/g,"\/"))) < (new Date(d2.replace(/-/g,"\/"))));?
}?