سلام دوست عزیز من با استفاده از کد زیر میام 16 مدل مختلفی که کاربر تاریخ رو وارد میکنه به صورت یک فرمت خاص یعنی 0000/00/00 در میارم که کاربر راحتر باشه و من هم در تمام برنامه خود از یک فرمت خاص استفاده کنم اگر این رو به برنامه تقویم خود اضافه کنی به نظرم بعد نمیشه .
public string DateFormat(string Date)
{
string strD = "" ;
string strM = "";
string strY = "";
string strConvert = "";
strConvert = Date;
strConvert = strConvert.Replace("1", "0");
strConvert = strConvert.Replace("2", "0");
strConvert = strConvert.Replace("3", "0");
strConvert = strConvert.Replace("4", "0");
strConvert = strConvert.Replace("5", "0");
strConvert = strConvert.Replace("6", "0");
strConvert = strConvert.Replace("7", "0");
strConvert = strConvert.Replace("8", "0");
strConvert = strConvert.Replace("9", "0");
strConvert = strConvert.Replace("/", "/");
strConvert = strConvert.Replace("-", "/");
strConvert = strConvert.Replace(".", "/");
if (string.IsNullOrEmpty (strConvert))
{
return null;
}
switch (strConvert.ToString())
{
case "0":
strY = pdate(DateTime.Now).Substring(0, 4);
strM = pdate(DateTime.Now).Substring(5, 2);
strD = pdate(DateTime.Now).Substring(8, 2);
break;
case "0/0":
strY = pdate(DateTime.Now).Substring(0, 4);
strM = Date.Substring(0, 1);
strD = Date.Substring(2, 1);
break;
case "0000":
strY = pdate(DateTime.Now).Substring(0, 4);
strM = Date.Substring(0, 2);
strD = Date.Substring(2, 2);
break;
case "00/0":
strY = pdate(DateTime.Now).Substring(0, 4);
strM = Date.Substring(0, 2);
strD = Date.Substring(3, 1);
break;
case "0/00":
strY = pdate(DateTime.Now).Substring(0, 4);
strM = Date.Substring(0, 1);
strD = Date.Substring(2, 2);
break;
case "00/00":
strY = pdate(DateTime.Now).Substring(0, 4);
strM = Date.Substring(0, 2);
strD = Date.Substring(3, 2);
break;
case "000000":
strY = Date.Substring(0, 2);
strM = Date.Substring(2, 2);
strD = Date.Substring(4, 2);
break;
case "00/0/0":
strY = Date.Substring(0, 2);
strM = Date.Substring(3, 1);
strD = Date.Substring(5, 1);
break;
case "00/00/0":
strY = Date.Substring(0, 2);
strM = Date.Substring(3, 2);
strD = Date.Substring(6, 1);
break;
case "00/0/00":
strY = Date.Substring(0, 2);
strM = Date.Substring(3, 1);
strD = Date.Substring(5, 2);
break;
case "00000000":
strY = Date.Substring(0, 4);
strM = Date.Substring(4, 2);
strD = Date.Substring(6, 2);
break;
case "0000/0/0":
strY = Date.Substring(0, 4);
strM = Date.Substring(5, 1);
strD = Date.Substring(7, 1);
break;
case "00/00/00":
strY = Date.Substring(0, 2);
strM = Date.Substring(3, 2);
strD = Date.Substring(6, 2);
break;
case "0000/00/0":
strY = Date.Substring(0, 4);
strM = Date.Substring(5, 2);
strD = Date.Substring(8, 1);
break;
case "0000/0/00":
strY = Date.Substring(0, 4);
strM = Date.Substring(5, 1);
strD = Date.Substring(7, 2);
break;
case "0000/00/00":
strY = Date.Substring(0, 4);
strM = Date.Substring(5, 2);
strD = Date.Substring(8, 2);
break;
default:
strY ="0000";
strM = "00";
strD = "00";
break;
}
if (strY.Length == 2)
{
strY = pdate(DateTime.Now).Substring(0, 2) + strY;
}
if (strM.Length == 1)
{
strM = "0" + strM;
}
if (strD.Length == 1)
{
strD = "0" + strD;
}
strConvert = strY + "/" + strM + "/" + strD;
if ((Convert.ToInt32(strM.ToString()) >= 13 || Convert.ToInt32(strM.ToString()) <= 0))// || (iDay >= 32 || iDay <= 0) || (iYear < 1200 || iYear > 1500))
{
return null;
}
if ((Convert.ToInt32(strD.ToString()) >= 32 || Convert.ToInt32(strD.ToString()) <= 0))// || (iDay >= 32 || iDay <= 0) || (iYear < 1200 || iYear > 1500))
{
return null;
}
if ((Convert.ToInt32(strM.ToString()) >= 7 && Convert.ToInt32(strM.ToString()) <= 11) && (Convert.ToInt32(strD.ToString()) >= 31))// || (iDay >= 32 || iDay <= 0) || (iYear < 1200 || iYear > 1500))
{
return null;
}
if (((Convert.ToInt32(strY.ToString())+1) % 4 != 0) && (Convert.ToInt32(strM.ToString()) == 12) && (Convert.ToInt32(strD.ToString()) >= 30))// || (iDay >= 32 || iDay <= 0) || (iYear < 1200 || iYear > 1500))
{
return null;
}
else
{
return strConvert;
}
}