Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to use the <a href="http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx" rel="nofollow noreferrer"><code>ParseExact</code> method</a>. This takes a string as its second argument that specifies the format the datetime is in, for example:</p> <pre><code>// Parse date and time with custom specifier. dateString = "2011-29-01 12:00 am"; format = "yyyy-dd-MM h:mm tt"; try { result = DateTime.ParseExact(dateString, format, provider); Console.WriteLine("{0} converts to {1}.", dateString, result.ToString()); } catch (FormatException) { Console.WriteLine("{0} is not in the correct format.", dateString); } </code></pre> <p>If the user can specify a format in the UI, then you need to translate that to a string you can pass into this method. You can do that by either allowing the user to enter the format string directly - though this means that the conversion is more likely to fail as they <em>will</em> enter an invalid format string - or having a combo box that presents them with the possible choices and you set up the format strings for these choices.</p> <p>If it's likely that the the input will be incorrect (user input for example) it would be better to use <code>TryParseExact</code> rather than exceptions to handle the error case:</p> <pre><code>// Parse date and time with custom specifier. dateString = "2011-29-01 12:00 am"; format = "yyyy-dd-MM h:mm tt"; DateTime result; if (DateTime.TryParseExact(dateString, format, provider, DateTimeStyles.None, out result)) { Console.WriteLine("{0} converts to {1}.", dateString, result.ToString()); } else { Console.WriteLine("{0} is not in the correct format.", dateString); } </code></pre>
    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. 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.
    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