123

I am new to DotNet and C#. I want to convert a string in mm/dd/yyyy format to DateTime object. I tried the parse function like below but it is throwing a runtime error.

DateTime dt=DateTime.Parse("24/01/2013");

Any ideas on how may I convert it to datetime?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Fawad Shah
  • 1,680
  • 4
  • 15
  • 21

3 Answers3

304

You need to use DateTime.ParseExact with format "dd/MM/yyyy"

DateTime dt=DateTime.ParseExact("24/01/2013", "dd/MM/yyyy", CultureInfo.InvariantCulture);

Its safer if you use d/M/yyyy for the format, since that will handle both single digit and double digits day/month. But that really depends if you are expecting single/double digit values.


Your date format day/Month/Year might be an acceptable date format for some cultures. For example for Canadian Culture en-CA DateTime.Parse would work like:

DateTime dt = DateTime.Parse("24/01/2013", new CultureInfo("en-CA"));

Or

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA");
DateTime dt = DateTime.Parse("24/01/2013"); //uses the current Thread's culture

Both the above lines would work because the the string's format is acceptable for en-CA culture. Since you are not supplying any culture to your DateTime.Parse call, your current culture is used for parsing which doesn't support the date format. Read more about it at DateTime.Parse.


Another method for parsing is using DateTime.TryParseExact

DateTime dt;
if (DateTime.TryParseExact("24/01/2013", 
                            "d/M/yyyy", 
                            CultureInfo.InvariantCulture, 
                            DateTimeStyles.None,
    out dt))
{
    //valid date
}
else
{
    //invalid date
}

The TryParse group of methods in .Net framework doesn't throw exception on invalid values, instead they return a bool value indicating success or failure in parsing.

Notice that I have used single d and M for day and month respectively. Single d and M works for both single/double digits day and month. So for the format d/M/yyyy valid values could be:

  • "24/01/2013"
  • "24/1/2013"
  • "4/12/2013" //4 December 2013
  • "04/12/2013"

For further reading you should see: Custom Date and Time Format Strings

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Thanks a lot. I had already tried the DateTime.ParseExact() but I guess the trick was with the null as third argument. I was using CultureInfo.InvariantCulture instead which would give me a compilation error. – Fawad Shah Apr 01 '13 at 06:53
  • 1
    @user1872530, you are welcome, it should have worked for you, but I guess you are missing using statement for System.Globalization, so try `System.Globalization.CultureInfo.InvariantCulture` instead, – Habib Apr 01 '13 at 07:48
  • 1
    Thanks. I was using "dd/MM/yyyy" and the parse failed for "3/12/2016" changing it to "d/M/yyyy" worked as the day and month values < 10 no longer need to be prepended with zero. – ComeIn Aug 08 '16 at 14:41
  • if the string includes the time, "dd/M/yyyy H:mm:ss" can be used – Marisol Gutiérrez May 07 '20 at 17:14
22

use DateTime.ParseExact

string strDate = "24/01/2013";
DateTime date = DateTime.ParseExact(strDate, "dd/MM/yyyy", null)

null will use the current culture, which is somewhat dangerous. Try to supply a specific culture

DateTime date = DateTime.ParseExact(strDate, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Sam Jones
  • 4,443
  • 2
  • 40
  • 45
John Woo
  • 258,903
  • 69
  • 498
  • 492
9

You can use "dd/MM/yyyy" format for using it in DateTime.ParseExact.

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

DateTime date = DateTime.ParseExact("24/01/2013", "dd/MM/yyyy", CultureInfo.InvariantCulture);

Here is a DEMO.

For more informations, check out Custom Date and Time Format Strings

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364