# VBForums CodeBank > CodeBank - C# >  C# - More Advanced String to Date Parsing

## hellswraith

The code will parse a date string that a user enters into a datetime variable.  I needed a way to parse the dates as just numbers as well as the normal formats that include seperators.

You will notice I have included a test to prove that my method works.  This is from Test Driven Development philosophy.  If you want to learn more about testing your code in .Net, go to the Nunit web site at www.nunit.org.  Notice that my test passes a bunch of different combos of strings through the method to ensure that I am getting the correct dates back.

I plan to extend this more in the future, but for now, this was all I needed.  If you have more efficient ways to make this work, please revise the code, run the test on it to ensure you didn't break existing functionality, then post it as a reply to this thread.  I haven't had the time to optimize yet.



```
using System;
using NUnit.Framework;

namespace VariantX.Utilities
{
	[TestFixture]
	public class Dates
	{
		public Dates(){}

		[Test]
		public void DateParseTest()
		{
			// Tests every combo with the slash as a seperator.
			Assert.AreEqual(1, ParseStringToDate("1/5/04").Month);
			Assert.AreEqual(5, ParseStringToDate("1/5/04").Day);
			Assert.AreEqual(2004, ParseStringToDate("1/5/04").Year);

			Assert.AreEqual(1, ParseStringToDate("1/5/2004").Month);
			Assert.AreEqual(5, ParseStringToDate("1/5/2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("1/5/2004").Year);

			Assert.AreEqual(1, ParseStringToDate("01/5/04").Month);
			Assert.AreEqual(5, ParseStringToDate("01/5/04").Day);
			Assert.AreEqual(2004, ParseStringToDate("01/5/04").Year);

			Assert.AreEqual(1, ParseStringToDate("01/5/2004").Month);
			Assert.AreEqual(5, ParseStringToDate("01/5/2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("01/5/2004").Year);

			Assert.AreEqual(1, ParseStringToDate("1/05/04").Month);
			Assert.AreEqual(5, ParseStringToDate("1/05/04").Day);
			Assert.AreEqual(2004, ParseStringToDate("1/05/04").Year);

			Assert.AreEqual(1, ParseStringToDate("1/05/2004").Month);
			Assert.AreEqual(5, ParseStringToDate("1/05/2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("1/05/2004").Year);

			Assert.AreEqual(1, ParseStringToDate("01/05/04").Month);
			Assert.AreEqual(5, ParseStringToDate("01/05/04").Day);
			Assert.AreEqual(2004, ParseStringToDate("01/05/04").Year);

			Assert.AreEqual(1, ParseStringToDate("01/05/2004").Month);
			Assert.AreEqual(5, ParseStringToDate("01/05/2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("01/05/2004").Year);


			// Tests every combo with the dash as a seperator.
			Assert.AreEqual(1, ParseStringToDate("1-5-04").Month);
			Assert.AreEqual(5, ParseStringToDate("1-5-04").Day);
			Assert.AreEqual(2004, ParseStringToDate("1-5-04").Year);

			Assert.AreEqual(1, ParseStringToDate("1-5-2004").Month);
			Assert.AreEqual(5, ParseStringToDate("1-5-2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("1-5-2004").Year);

			Assert.AreEqual(1, ParseStringToDate("01-5-04").Month);
			Assert.AreEqual(5, ParseStringToDate("01-5-04").Day);
			Assert.AreEqual(2004, ParseStringToDate("01-5-04").Year);

			Assert.AreEqual(1, ParseStringToDate("01-5-2004").Month);
			Assert.AreEqual(5, ParseStringToDate("01-5-2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("01-5-2004").Year);

			Assert.AreEqual(1, ParseStringToDate("1-05-04").Month);
			Assert.AreEqual(5, ParseStringToDate("1-05-04").Day);
			Assert.AreEqual(2004, ParseStringToDate("1-05-04").Year);

			Assert.AreEqual(1, ParseStringToDate("1-05-2004").Month);
			Assert.AreEqual(5, ParseStringToDate("1-05-2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("1-05-2004").Year);

			Assert.AreEqual(1, ParseStringToDate("01-05-04").Month);
			Assert.AreEqual(5, ParseStringToDate("01-05-04").Day);
			Assert.AreEqual(2004, ParseStringToDate("01-05-04").Year);

			Assert.AreEqual(1, ParseStringToDate("01-05-2004").Month);
			Assert.AreEqual(5, ParseStringToDate("01-05-2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("01-05-2004").Year);


			// Tests every combo with the period as a seperator.
			Assert.AreEqual(1, ParseStringToDate("1.5.04").Month);
			Assert.AreEqual(5, ParseStringToDate("1.5.04").Day);
			Assert.AreEqual(2004, ParseStringToDate("1.5.04").Year);

			Assert.AreEqual(1, ParseStringToDate("1.5.2004").Month);
			Assert.AreEqual(5, ParseStringToDate("1.5.2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("1.5.2004").Year);

			Assert.AreEqual(1, ParseStringToDate("01.5.04").Month);
			Assert.AreEqual(5, ParseStringToDate("01.5.04").Day);
			Assert.AreEqual(2004, ParseStringToDate("01.5.04").Year);

			Assert.AreEqual(1, ParseStringToDate("01.5.2004").Month);
			Assert.AreEqual(5, ParseStringToDate("01.5.2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("01.5.2004").Year);

			Assert.AreEqual(1, ParseStringToDate("1.05.04").Month);
			Assert.AreEqual(5, ParseStringToDate("1.05.04").Day);
			Assert.AreEqual(2004, ParseStringToDate("1.05.04").Year);

			Assert.AreEqual(1, ParseStringToDate("1.05.2004").Month);
			Assert.AreEqual(5, ParseStringToDate("1.05.2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("1.05.2004").Year);

			Assert.AreEqual(1, ParseStringToDate("01.05.04").Month);
			Assert.AreEqual(5, ParseStringToDate("01.05.04").Day);
			Assert.AreEqual(2004, ParseStringToDate("01.05.04").Year);

			Assert.AreEqual(1, ParseStringToDate("01.05.2004").Month);
			Assert.AreEqual(5, ParseStringToDate("01.05.2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("01.05.2004").Year);

			// Test with spaces as the seperator
			Assert.AreEqual(1, ParseStringToDate("1 5 04").Month);
			Assert.AreEqual(5, ParseStringToDate("1 5 04").Day);
			Assert.AreEqual(2004, ParseStringToDate("1 5 04").Year);

			Assert.AreEqual(1, ParseStringToDate("1 5 2004").Month);
			Assert.AreEqual(5, ParseStringToDate("1 5 2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("1 5 2004").Year);

			Assert.AreEqual(1, ParseStringToDate("01 5 04").Month);
			Assert.AreEqual(5, ParseStringToDate("01 5 04").Day);
			Assert.AreEqual(2004, ParseStringToDate("01 5 04").Year);

			Assert.AreEqual(1, ParseStringToDate("01 5 2004").Month);
			Assert.AreEqual(5, ParseStringToDate("01 5 2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("01 5 2004").Year);

			Assert.AreEqual(1, ParseStringToDate("1 05 04").Month);
			Assert.AreEqual(5, ParseStringToDate("1 05 04").Day);
			Assert.AreEqual(2004, ParseStringToDate("1 05 04").Year);

			Assert.AreEqual(1, ParseStringToDate("1 05 2004").Month);
			Assert.AreEqual(5, ParseStringToDate("1 05 2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("1 05 2004").Year);

			Assert.AreEqual(1, ParseStringToDate("01 05 04").Month);
			Assert.AreEqual(5, ParseStringToDate("01 05 04").Day);
			Assert.AreEqual(2004, ParseStringToDate("01 05 04").Year);

			Assert.AreEqual(1, ParseStringToDate("01 05 2004").Month);
			Assert.AreEqual(5, ParseStringToDate("01 05 2004").Day);
			Assert.AreEqual(2004, ParseStringToDate("01 05 2004").Year);

			// Test with only numbers.  Note, that only 6 and 8 digits are
			// supported.  Any other, and the date could be ambiguous.
			Assert.AreEqual(1, ParseStringToDate("010504").Month);
			Assert.AreEqual(5, ParseStringToDate("010504").Day);
			Assert.AreEqual(2004, ParseStringToDate("010504").Year);

			Assert.AreEqual(1, ParseStringToDate("01052004").Month);
			Assert.AreEqual(5, ParseStringToDate("01052004").Day);
			Assert.AreEqual(2004, ParseStringToDate("01052004").Year);

		}

		public DateTime ParseStringToDate(string DateString)
		{
			DateTime dtReturn = new DateTime();
			bool bSeperators = false;
			int iTemp;
			int iMonth;
			int iDay;
			int iYear;
			
			//First check that it is at least 6 characters or more.
			if(!(DateString.Length > 5))
				throw new Exception("Date string not in correct format.");

			// Next, see if the framework can parse it.
			try
			{
				dtReturn = DateTime.Parse(DateString);
				return dtReturn;
			}
			catch
			{}
			
			//Check to see if it has any seperators.  If not, it should parse to a int.
			try
			{
				iTemp = int.Parse(DateString);
				bSeperators = false;
			}
			catch
			{
				bSeperators = true;
			}

			if(!bSeperators)
			{

				if(DateString.Length == 6)
				{
					iMonth = int.Parse(DateString.Substring(0,2));
					iDay = int.Parse(DateString.Substring(2,2));
					iYear = int.Parse(DateString.Substring(4,2));
					iTemp = DateTime.Now.Year;
					iTemp = iTemp / 100;
					iTemp = iTemp * 100;
					iYear += iTemp;
					try
					{
						return new DateTime(iYear, iMonth, iDay);
					}
					catch
					{
						throw new Exception("Date string not in correct format.");
					}
				}
				else
				{
					if(DateString.Length == 8)
					{
						iMonth = int.Parse(DateString.Substring(0,2));
						iDay = int.Parse(DateString.Substring(2,2));
						iYear = int.Parse(DateString.Substring(4,4));
						try
						{
							return new DateTime(iYear, iMonth, iDay);
						}
						catch
						{
								throw new Exception("Date string not in correct format.");
						}
					}
					else
					{
						// Not a 6 or 8 digit number that was passed in.  Any other
						// combination would have ambiguity, therefor it is an error.
						throw new Exception("Date string not in correct format.");
					}
				}
			}
			else
			{
				// Looks like it is seperated by characters the framework doesn't support.
				// Next version will take this and parse it, but for now it is an error.
				throw new Exception("Date string not in correct format.");
			}
		}
	}
}
```

----------

