游子日月长

笑渐不闻声渐悄,多情却被无情恼!

导航

js 日期证有效性验的通用方法

开发的理念是“为复用而开发,为使用而组装”,代码的复用度将是项目和产品的一个重要的技术指标。

var DateTools={

isDate:function(str)
{
var result = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if (result == null) return false;
var d = new Date(result[1], result[3] - 1, result[4]);
return (d.getFullYear() == result[1] && d.getMonth() + 1 == result[3] && d.getDate() == result[4]);
}
isDatetime:function(str)
{
var result = str.match(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
if (result == null) return false;
var d = new Date(result[1], result[3] - 1, result[4], result[5], result[6], result[7]);
return (d.getFullYear() == result[1] && (d.getMonth() + 1) == result[3] && d.getDate() == result[4] && d.getHours() == result[5] && d.getMinutes() == result[6] && d.getSeconds() == result[7]);
}

isTime: function(str)
{
var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
if (a == null) { return false; }
if (a[1] >= 24 || a[3] >= 60 || a[4] >= 60) {
return false
}
return true;
}

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


}


posted on 2016-11-11 10:31  游子日月长  阅读(186)  评论(0编辑  收藏  举报