判斷日期,輸出成YYYY/MM/DD形式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>轉換日期</title>
<script type="text/javascript">
String.prototype.IsDate=function(){
var dateReg=/^[1-3]\d{3}(\/|-)?((1[0-2])|(0?[1-9]))(\/|-)?((0?[1-9])|([1-2]\d)|(3[01]))$/;
var rdelimiter="/";
var nDate;
if(dateReg.test(this)){
var iDate=this.replace(/\//g,"").replace(/-/,"");
if(iDate.length == 6){
nDate = iDate.substr(0, 4)+rdelimiter+"0"+iDate.substr(4, 1)+rdelimiter+"0"+iDate.substr(5, 1);
}else{
var nMonth = parseInt(iDate.substr(4, 2));
if(nMonth > 12){ //月份為一位
nDate = iDate.substr(0, 4)+rdelimiter+"0"+iDate.substr(4, 1)+rdelimiter+iDate.substr(5, 2);
}else{
var nDay = "0"+iDate.substr(6, 2);
nDate = iDate.substr(0, 4)+rdelimiter+"0"+iDate.substr(4, 2)+rdelimiter+"0"+nDay.substr(nDay.length-2, 2);
}
}
if(isNaN(new Date(nDate))){
return false;
}else{
return true;
}
}else{
return false;
}
}
/**//*********************************************************
** 名稱:getFormatDate() **
** 功能:格式化日期 **
** 參數: iDate :轉換前日期YYYY-M/D或YYYY/MM/DD **
** idelimiter :轉換前日期分格符 **
** rdelimiter :轉換後的分格符,默認為"/" **
** 返回: String,YYYY+分格符+MM+分格符+DD **
*********************************************************/
function getFormatDate(iDate, idelimiter, rdelimiter){
var regStr;
var delimiter;
var tDate;
var rDate;
var rMonth;
var rDay;
if(rdelimiter == ""){
rdelimiter="/";
}
//替換掉分隔符
for(var i=0; i<idelimiter.length; i++){
delimiter=idelimiter.substr(i, 1);
if(delimiter == "/"){
iDate=iDate.replace(/\//g, "");
}else{
regStr = "iDate=iDate.replace(/"+delimiter+"/g, '');";
eval(regStr);
}
}
if(iDate.length == 6){
nDate = iDate.substr(0, 4)+rdelimiter+"0"+iDate.substr(4, 1)+rdelimiter+"0"+iDate.substr(5, 1);
}else{
var nMonth = parseInt(iDate.substr(4, 2));
if(nMonth > 12){ //月份為一位
nDate = iDate.substr(0, 4)+rdelimiter+"0"+iDate.substr(4, 1)+rdelimiter+iDate.substr(5, 2);
}else{
var nDay = "0"+iDate.substr(6, 2);
nDate = iDate.substr(0, 4)+rdelimiter+"0"+iDate.substr(4, 2)+rdelimiter+"0"+nDay.substr(nDay.length-2, 2);
}
}
tDate=new Date(nDate);
if(isNaN(tDate)){
throw("日期非法");
}
rMonth = "00"+(tDate.getMonth()+1).toString();
rMonth = rMonth.substr(rMonth.length-2, 2);
rDay = "00"+tDate.getDate().toString();
rDay = rDay.substr(rDay.length-2, 2);
rDate = tDate.getFullYear().toString() + rdelimiter + rMonth + rdelimiter + rDay;
return rDate;
}
function test(){
var cText=document.getElementById("test");
if(cText.value.IsDate()){
cText.value=getFormatDate(cText.value, "/-", "/");
return true;
}else{
alert("日期格式不正確或非法,正確輸入格式是YYYY/MM/DD或YYYYMMDD");
return false;
}
}
</script>
</head>
<body>
<input type="text" id="test" />
<input type="button" value="測試" onclick="test()" />
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>轉換日期</title>
<script type="text/javascript">
String.prototype.IsDate=function(){
var dateReg=/^[1-3]\d{3}(\/|-)?((1[0-2])|(0?[1-9]))(\/|-)?((0?[1-9])|([1-2]\d)|(3[01]))$/;
var rdelimiter="/";
var nDate;
if(dateReg.test(this)){
var iDate=this.replace(/\//g,"").replace(/-/,"");
if(iDate.length == 6){
nDate = iDate.substr(0, 4)+rdelimiter+"0"+iDate.substr(4, 1)+rdelimiter+"0"+iDate.substr(5, 1);
}else{
var nMonth = parseInt(iDate.substr(4, 2));
if(nMonth > 12){ //月份為一位
nDate = iDate.substr(0, 4)+rdelimiter+"0"+iDate.substr(4, 1)+rdelimiter+iDate.substr(5, 2);
}else{
var nDay = "0"+iDate.substr(6, 2);
nDate = iDate.substr(0, 4)+rdelimiter+"0"+iDate.substr(4, 2)+rdelimiter+"0"+nDay.substr(nDay.length-2, 2);
}
}
if(isNaN(new Date(nDate))){
return false;
}else{
return true;
}
}else{
return false;
}
}
/**//*********************************************************
** 名稱:getFormatDate() **
** 功能:格式化日期 **
** 參數: iDate :轉換前日期YYYY-M/D或YYYY/MM/DD **
** idelimiter :轉換前日期分格符 **
** rdelimiter :轉換後的分格符,默認為"/" **
** 返回: String,YYYY+分格符+MM+分格符+DD **
*********************************************************/
function getFormatDate(iDate, idelimiter, rdelimiter){
var regStr;
var delimiter;
var tDate;
var rDate;
var rMonth;
var rDay;
if(rdelimiter == ""){
rdelimiter="/";
}
//替換掉分隔符
for(var i=0; i<idelimiter.length; i++){
delimiter=idelimiter.substr(i, 1);
if(delimiter == "/"){
iDate=iDate.replace(/\//g, "");
}else{
regStr = "iDate=iDate.replace(/"+delimiter+"/g, '');";
eval(regStr);
}
}
if(iDate.length == 6){
nDate = iDate.substr(0, 4)+rdelimiter+"0"+iDate.substr(4, 1)+rdelimiter+"0"+iDate.substr(5, 1);
}else{
var nMonth = parseInt(iDate.substr(4, 2));
if(nMonth > 12){ //月份為一位
nDate = iDate.substr(0, 4)+rdelimiter+"0"+iDate.substr(4, 1)+rdelimiter+iDate.substr(5, 2);
}else{
var nDay = "0"+iDate.substr(6, 2);
nDate = iDate.substr(0, 4)+rdelimiter+"0"+iDate.substr(4, 2)+rdelimiter+"0"+nDay.substr(nDay.length-2, 2);
}
}
tDate=new Date(nDate);
if(isNaN(tDate)){
throw("日期非法");
}
rMonth = "00"+(tDate.getMonth()+1).toString();
rMonth = rMonth.substr(rMonth.length-2, 2);
rDay = "00"+tDate.getDate().toString();
rDay = rDay.substr(rDay.length-2, 2);
rDate = tDate.getFullYear().toString() + rdelimiter + rMonth + rdelimiter + rDay;
return rDate;
}
function test(){
var cText=document.getElementById("test");
if(cText.value.IsDate()){
cText.value=getFormatDate(cText.value, "/-", "/");
return true;
}else{
alert("日期格式不正確或非法,正確輸入格式是YYYY/MM/DD或YYYYMMDD");
return false;
}
}
</script>
</head>
<body>
<input type="text" id="test" />
<input type="button" value="測試" onclick="test()" />
</body>
</html>