javascript中有没有判断日期的函数?

feng_zi 2001-09-17 01:21:51
请问高手在javascript中有没有象vbscript中isdate一样的函数呢?
如没有这样的函数,如何才能简单地判断一个字符串是不是一个日期形式呢?
...全文
121 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wybm 2001-09-17
  • 打赏
  • 举报
回复
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<TITLE>checkdata</TITLE>
<script language="javascript">
<!--


// PURPOSE: Check to see if the string passed in is a valid time.
// A valid time is defined as a string which is postfixed with either
// "PM" or "AM". Next it checks to see if there is a colon in the
// string. If there is, it makes sure that at least one digit preceeds
// it and two proceed it.

function chkdate(objName) {
var strDatestyle = "US"; //United States date style
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var datefield = objName;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);
strMonthArray[0] = "Jan";
strMonthArray[1] = "Feb";
strMonthArray[2] = "Mar";
strMonthArray[3] = "Apr";
strMonthArray[4] = "May";
strMonthArray[5] = "Jun";
strMonthArray[6] = "Jul";
strMonthArray[7] = "Aug";
strMonthArray[8] = "Sep";
strMonthArray[9] = "Oct";
strMonthArray[10] = "Nov";
strMonthArray[11] = "Dec";
strDate = datefield.value;
if (strDate.length==0) {
return false;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
strDateArray = strDate.split(strSeparatorArray[intElementNr]);
if (strDateArray.length != 3) {
return false;
}
else {
strDay = strDateArray[0];
strMonth = strDateArray[1];
strYear = strDateArray[2];
if (strYear.length>4){
return false;
}
}
}
}

// US style
if (strDatestyle == "US") {
strTemp = strDay;
strDay = strMonth;
strMonth = strTemp;
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
for (i = 0;i<12;i++) {
if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
intMonth = i+1;
strMonth = strMonthArray[i];
i = 12;
}
}
if (isNaN(intMonth)) {
return false;
}
}
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
return false;
}
if (intMonth>12 || intMonth<1) {
return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
return false;
}
if (intMonth == 2) {
if (intday < 1) {
return false;
}
if (LeapYear(intYear) == true) {
if (intday > 29) {
return false;
}
}
else {
if (intday > 28) {
return false;
}
}
}
if (strDatestyle == "US") {
datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
}
else {
datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
}
return true;
}
function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}

function checkdata(){
var strdate=document.form1.strdate;
if (chkdate(strdate)==false){
alert("The date is Invalid!")
document.form1.strdate.select();
document.form1.strdate.focus();
return false;
}
}
//-->
</script>
</HEAD>
<BODY onload="javascript:test()">
<form name="form1" method="post" action="">
<table width="100%" border="0">
<tr>
<td width="45%">
<div align="right">Please input the Date:</div>
</td>
<td width="55%">
<input type="text" name="strdate">
</td>
</tr>

<tr>
<td colspan="2">
<div align="center"> </div>
</td>
</tr>
<tr>
<td colspan="2">
<div align="center">
<input type="button" name="Button" value="Submit" onClick="javascript:checkdata()" style="cursor:hand">
</div>
</td>
</tr>
</table>
</form>
</BODY>
</HTML>
feng_zi 2001-09-17
  • 打赏
  • 举报
回复
to karma:
能不能再回答一下,满足我上面条件的呢?对于javascript我可是有些了解而已,现在给朋友做个网站,又不想用vbscript,所以……
  如果分不够,再加……
feng_zi 2001-09-17
  • 打赏
  • 举报
回复
求救!
feng_zi 2001-09-17
  • 打赏
  • 举报
回复
我想要的是格式如:
2001-10-1 12:30
而上面的不认这种格式,只认01-10-1 12:30,而还认01-13-1 12:30
我想要它判断是否为正确的日期
karma 2001-09-17
  • 打赏
  • 举报
回复
function isdate(str)
{
var dt = new Date(str);
if (isNaN(dt))
return false;
else
return true;
}

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧