public static DateTime GetDateValue(string sVal)
{
DateTime dtRet;
try
{
int nAdd = 1900;
if (Convert.ToInt32(sVal.Substring(0, 2)) < 80)
nAdd = 2000;
if (sVal.Length == 6)
{
//YYMMDD
dtRet = new DateTime(nAdd + Convert.ToInt32(sVal.Substring(0, 2)), Convert.ToInt32(sVal.Substring(2, 2)), Convert.ToInt32(sVal.Substring(4, 2)), 0, 0, 0);
return dtRet;
}
if (sVal.Length == 8)
{
if (sVal.IndexOf("-") > 0)
{
//YY-MM-DD
dtRet = new DateTime(nAdd + Convert.ToInt32(sVal.Substring(0, 2)), Convert.ToInt32(sVal.Substring(3, 2)), Convert.ToInt32(sVal.Substring(6, 2)), 0, 0, 0);
return dtRet;
}
//YYYYMMDD
dtRet = new DateTime(Convert.ToInt32(sVal.Substring(0, 4)), Convert.ToInt32(sVal.Substring(4, 2)), Convert.ToInt32(sVal.Substring(6, 2)), 0, 0, 0);
return dtRet;
}
if (sVal.Length == 10)
{
//YYYY-MM-DD
dtRet = new DateTime(Convert.ToInt32(sVal.Substring(0, 4)), Convert.ToInt32(sVal.Substring(5, 2)), Convert.ToInt32(sVal.Substring(8, 2)), 0, 0, 0);
return dtRet;
}
return Convert.ToDateTime(sVal);
}
catch (Exception){}
dtRet = new DateTime(1900, 01, 01, 0, 0, 0);
return dtRet;
}
protected void Page_Load(object sender, EventArgs e)
{
textBox1.Text += "010102->" + GetDateValue("010102").ToString("yyyy-MM-dd") + Environment.NewLine;
textBox1.Text += "790102->" + GetDateValue("790102").ToString("yyyy-MM-dd") + Environment.NewLine;
textBox1.Text += "800102->" + GetDateValue("800102").ToString("yyyy-MM-dd") + Environment.NewLine;
textBox1.Text += "810102->" + GetDateValue("810102").ToString("yyyy-MM-dd") + Environment.NewLine;
textBox1.Text += "20000102->" + GetDateValue("20000102").ToString("yyyy-MM-dd") + Environment.NewLine;
textBox1.Text += "01-01-02->" + GetDateValue("01-01-02").ToString("yyyy-MM-dd") + Environment.NewLine;
textBox1.Text += "79-01-02->" + GetDateValue("79-01-02").ToString("yyyy-MM-dd") + Environment.NewLine;
textBox1.Text += "80-01-02->" + GetDateValue("80-01-02").ToString("yyyy-MM-dd") + Environment.NewLine;
textBox1.Text += "810102->" + GetDateValue("81-01-02").ToString("yyyy-MM-dd") + Environment.NewLine;
textBox1.Text += "2000-01-02->" + GetDateValue("2000-01-02").ToString("yyyy-MM-dd") + Environment.NewLine;
}
Here is the .aspx file ::
010102->2001-01-02
790102->2079-01-02
800102->1980-01-02
810102->1981-01-02
20000102->2000-01-02
01-01-02->2001-01-02
79-01-02->2079-01-02
80-01-02->1980-01-02
810102->1981-01-02
2000-01-02->2000-01-02
No comments:
Post a Comment