Jul
04
2005
Convert string to datetime
Posted by admin under
.NET
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;
}
I realize this is pretty much specialized for swedish locals, but you get the idea and could probably easily make something out of it.
As you can see I also specify 1900-01-01 as my NULL/NOT DEFINED/NOT VALID date but that's simply how my other systems wanted it to be.
RESULTS:
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
result showed in with swedish locals with this function:
private void Form1_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;
}
Note: to think about - how do you handle two digit years? I have chosen to draw the line at 79. So numbers under 80 are treated as 20xx and numbers above as 19xx. It fitted the business requirements best but you can of course do it as you like.