Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to use <a href="http://msdn.microsoft.com/en-us/library/system.datetime.parseexact.aspx"><code>DateTime.ParseExact</code></a> with format <code>"dd/MM/yyyy"</code></p> <pre><code>DateTime dt=DateTime.ParseExact("24/01/2013", "dd/MM/yyyy", CultureInfo.InvariantCulture); </code></pre> <p>Its safer if you use <code>d/M/yyyy</code> 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. </p> <hr> <p>Your date format <code>day/Month/Year</code> might be an acceptable date format for some cultures. For example for Canadian Culture <code>en-CA</code> <code>DateTime.Parse</code> would work like:</p> <pre><code>DateTime dt = DateTime.Parse("24/01/2013", new CultureInfo("en-CA")); </code></pre> <p>Or </p> <pre><code>System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA"); DateTime dt = DateTime.Parse("24/01/2013"); //uses the current Thread's culture </code></pre> <p>Both the above lines would work because the the string's format is acceptable for <code>en-CA</code> culture. Since you are not supplying any culture to your <code>DateTime.Parse</code> call, your current culture is used for parsing which doesn't support the date format. Read more about it at <a href="http://msdn.microsoft.com/en-us/library/system.datetime.parse(v=vs.110).aspx">DateTime.Parse</a>. </p> <hr> <p>Another method for parsing is using <a href="http://msdn.microsoft.com/en-us/library/system.datetime.tryparse(v=vs.110).aspx"><code>DateTime.TryParseExact</code></a></p> <pre><code>DateTime dt; if (DateTime.TryParseExact("24/01/2013", "d/M/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt)) { //valid date } else { //invalid date } </code></pre> <p>The <code>TryParse</code> group of methods in .Net framework doesn't throw exception on invalid values, instead they return a <code>bool</code> value indicating success or failure in parsing. </p> <p><strong>Notice</strong> that I have used single <code>d</code> and <code>M</code> for day and month respectively. Single <code>d</code> and <code>M</code> works for both single/double digits day and month. So for the format <code>d/M/yyyy</code> valid values could be:</p> <ul> <li>"24/01/2013"</li> <li>"24/1/2013"</li> <li>"4/12/2013" //4 December 2013</li> <li>"04/12/2013"</li> </ul> <p>For further reading you should see: <a href="http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx">Custom Date and Time Format Strings</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload