To convert a dutch string to DateTime and a DateTime to a dutch string in C# use the class:

    public class DateTimeHelper
    {
        /// <summary>
        /// Returns a DateTime object if conversion from dutch string succeeded.
        /// Return DateTime.MinValue if conversion from dutch string failed.
        /// </summary>
        /// <param name="dateTime">The string to convert</param>
        /// <returns>See description</returns>
        public DateTime ConvertDutchStringToDateTime(string dateTime)
        {
            DateTime result = DateTime.MinValue;
            bool conversionResult = DateTime.TryParseExact(dateTime, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out  result);
            return result;
        }
        /// <summary>
        /// Return a dutch datetime formatted string.
        /// </summary>
        /// <param name="dateTime">DateTime object to convert</param>
        /// <returns>See description</returns>
        public string ConvertDateTimeToDutchString(DateTime dateTime)
        {
            return dateTime.ToString("dd-MM-yyyy");
        }
    }


UnitTest class

    [TestFixture]
    public class DateTimeHelperTest
    {
        [Test]
        public void ConvertDutchStringToDateTime_Basic()
        {
            DateTimeHelper helper = new DateTimeHelper();
            DateTime dateTime = helper.ConvertDutchStringToDateTime("12-01-2009");
            Assert.That(dateTime, Is.EqualTo(new DateTime(2009,1,12)));
        }
        [Test]
        public void ConvertDateTimeToDutchString_Basic()
        {
            DateTimeHelper helper = new DateTimeHelper();
            string result = helper.ConvertDateTimeToDutchString(new DateTime(2009, 1, 12));
            Assert.That(result, Is.EqualTo("12-01-2009"));
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.