Tuesday, March 9, 2010

Convert string to datetime

This is a favorite function I used a lot when getting data from remote systems. For example I once needed to retrieve data from an AS/400 box, and the file format was already specified. However, date fields in the file could look pretty different (6, 8, 10 digits with or without delimiters between year/month/day), depending on certain aspects so my timesaving all-purpose reoutine for getting date values look like this: 

 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 ::






Output::

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