请问怎样判断日起格式是正确的
像2003,2003/01,2003/01/01 这三种我都认为是正确的日期格式,
问题点数:0、回复次数:6Top
1 楼zq_hhu(海鸥飞飞)回复于 2003-09-02 16:57:05 得分 0
try
{ Convert.ToDateTime(string);
"right";
}
catch
{ "error";}Top
2 楼cyp503(谁怕?一蓑烟雨任平生)回复于 2003-09-02 17:08:29 得分 0
javascript:
function validateDate(sDate, arrValidFormats, bAllowEmpty)
{
var sErrorMsg = "";
var bValidFound = false;
var bEmpty = false;
sDate = trim(sDate);
if(arrValidFormats == null)
arrValidFormats = g_arrValidDateFormats;
if(!bAllowEmpty && sDate == "") // If empty
{
sErrorMsg = "- " + sName + sErrIsEmpty;
}
else if(sDate != "") // Check date
{
var sAllDates = "";
for(var i = 0; i < arrValidFormats.length; i++) // Go through all valid formats
{
var sValidDate = arrValidFormats[i];
var bThisValid = true;
if(sDate.length == sValidDate.length) // Only if correct length.
{
var iYear, iMonth, iDate, rgExp;
var iYearLen = (sValidDate.lastIndexOf("Y") != -1) ? sValidDate.lastIndexOf("Y") - sValidDate.indexOf("Y") + 1 : 0;
var iMonthLen = (sValidDate.lastIndexOf("M") != -1) ? sValidDate.lastIndexOf("M") - sValidDate.indexOf("M") + 1 : 0;
var iDateLen = (sValidDate.lastIndexOf("D") != -1) ? sValidDate.lastIndexOf("D") - sValidDate.indexOf("D") + 1 : 0;
if(iYearLen != 0) // Check year
{
iYear = sDate.slice(sValidDate.indexOf("Y"), sValidDate.indexOf("Y") + iYearLen);
rgExp = new RegExp("[0-9]{" + iYearLen + "}");
if(iYear.search(rgExp) == -1) // If a valid year number
bThisValid = false;
}
if(iMonthLen != 0 && bThisValid) // Check month
{
iMonth = sDate.slice(sValidDate.indexOf("M"), sValidDate.indexOf("M") + iMonthLen);
rgExp = new RegExp("[0-9]{" + iMonthLen + "}");
if(iMonth.search(rgExp) > -1)
{
// Check if from 1 to 12
if(iMonth < 1 || iMonth > 12)
bThisValid = false;
}
else
bThisValid = false;
}
if(iDateLen != 0 && bThisValid) // Check date
{
iDate = sDate.slice(sValidDate.indexOf("D"), sValidDate.indexOf("D") + iDateLen);
rgExp = new RegExp("[0-9]{" + iDateLen + "}");
if(iDate.search(rgExp) > -1)
{
if(iDate < 1 || iDate > 31)
bThisValid = false;
// Check if correct nr of days for the month (months with 30 days)
if(iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11)
{
if(iDate == 31)
bThisValid = false;
}
if(iMonth == 2) // Check february
{
if(iDate > 29) // If more than 29 days
bThisValid = false;
if(iYear) // Check leap year (if a year exist)
{
if(iDate == 29 && ((iYear / 4) != parseInt(iYear / 4)))
bThisValid = false;
}
}
}
else
bThisValid = false;
}
if(bThisValid) // Check separators
{
rgExp = new RegExp("[^YMD]", "g"); // Search for everything except YMD
rgExp2 = new RegExp("[^0-9]", "g"); // Search for everything except 0-9
var arrMatches = sValidDate.match(rgExp);
var arrMatches2 = sDate.match(rgExp2);
if(arrMatches != null)
{
for(var i2 = 0; i2 < arrMatches.length; i2++)
{
if(arrMatches2 == null ||
arrMatches2.length < arrMatches.length ||
arrMatches[i2] != arrMatches2[i2])
bThisValid = false;
}
}
}
}
else
bThisValid = false;
if(bThisValid)
{
bValidFound = true;
g_sLastValidDateFormat = sValidDate;
}
// Create string that might have to be presented in error message.
sAllDates += "\"" + arrValidFormats[i] + "\", ";
if(i == arrValidFormats.length-1)
sAllDates = sAllDates.slice(0, sAllDates.length-2);
}
// if(!bValidFound) // No valid at all found
// {sErrorMsg = "- " + sErrValidateDate + ".\n";}
// else
//{
//sErrorMsg=sDate;
// sDate=sErrorMsg;
// return "";
// }
if(bThisValid)
{
sDate=sAllDates;
return "";
}
else
{
sErrorMsg="- "+sErrValidateDate + ".\n";
}
}
return sErrorMsg;
}
Top
3 楼cyp503(谁怕?一蓑烟雨任平生)回复于 2003-09-02 17:10:06 得分 0
使用方法
var sErrMsg = validateDate(myForm.myDate.value, "Date",
new Array("YYMMDD", "YYYY-MM-DD"),
false);
if(sErrMsg != "")
alert(sErrMsg);
else
alert("Everything is okay!");
Extra feature:
Top
4 楼batisituta(秋实)回复于 2003-09-02 17:14:32 得分 0
public static bool isDayAvail(string strDate)
{
try
{ Convert.ToDateTime(strDate);
return true;
}
catch
{
return false;
}
}//end isDayAvail()Top
5 楼yewei4u(yewei)回复于 2003-09-02 17:17:50 得分 0
try
{
DateTime.Parse()
}
catch
{
不是日期型
}Top
6 楼YAOTIEBING(寻找失去的自我)回复于 2003-10-09 10:51:29 得分 0
try
{
DateTime.Parse(你的日期)
}
catch
{
不是日期型
}
Top




