none
Unable to parse the date in the format "M/d/yyyy H:m:s" using DateTime.ParseExact(String, String, IFormatProvider) RRS feed

  • Question

  • I am working on a windows phone silverlight application.I have a requirement where in I want to parse the date string of format "M/d/yyyy H:m:s" (eg: 3/18/2015 14:4:5) to DateTime struct.
    This is what I tried:
         
         string dateString = "3/18/2015 14:4:5";
         string format = @"M/d/yyyy H:m:s";
         CultureInfo provider = CultureInfo.InvariantCulture;
         DateTime.ParseExact(dateString,format,provider);

    But I am getting the following output:
    03-18-2015 14:04:05

    Required Output:
    3/18/2015 14:4:5

    I want the DateSeparator to be "/" and doesn't want an addition of extra '0' to any of the parameters like day,month,year,minutes etc to make it two digits.
    Wednesday, March 18, 2015 11:07 AM

Answers

All replies

  • A DateTime object doesn't have any particular output format. You must convert the value to a string to get the desired output. You could use the ToString method and pass the format that you want:

                string dateString = "3/18/2015 14:4:5";
                string format = @"M/d/yyyy H:m:s";
                CultureInfo provider = CultureInfo.InvariantCulture;
                DateTime date = DateTime.ParseExact(dateString, format, provider);
                //date doesn't have any specific format, it is just a dateTime object that stores hours, minutes, seconds, etc.
    
                string ds = date.ToString("M/d/yyyy H:m:s", provider); //3/18/2015 14:4:5
                //ds is a formatted string
    

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question.

    • Proposed as answer by Blackwood Wednesday, March 18, 2015 12:01 PM
    Wednesday, March 18, 2015 11:17 AM
  • When you convert the date back to a string set the format as follows with InvariantCulture and you should get the result 3/18/2015 14:4:5.

    string dateString = "3/18/2015 14:4:5";
    string format = @"M/d/yyyy H:m:s";
    CultureInfo provider = CultureInfo.InvariantCulture;
    var dt = DateTime.ParseExact(dateString, format, provider);
    dt.ToString("M/d/yyyy H:m:s", CultureInfo.InvariantCulture);


    • Edited by JayChase Wednesday, March 18, 2015 11:20 AM
    Wednesday, March 18, 2015 11:18 AM
  • Thanks for the answer. My actual problem here is I am using System.Net.Http.HttpClient to make a POST request.

    And the server requires me to send the Date header in this format  "M/d/yyyy H:m:s". But when I try to add the date string using :

    httpClient.DefaultRequestHeaders.Add("Date", "3/18/2015 14:4:5")

    I get an FormatException.


    So I tried adding the date in the following way:

    string format = @"M/d/yyyy H:m:s";

    DateTime CultureInfo provider = CultureInfo.InvariantCulture

    httpClient.DefaultRequestHeaders.Date = DateTime.ParseExact("3/18/2015 14:4:5",format,provider);

    But when I check the final request that is being sent using Fiddler(I have set up a proxy to monitor network requests from phone) the Date header has a value in the following format 03-18-2015 14:04:05.

    And my server rejects my request. How to add date header to the HttpClient object in the required format?

    Wednesday, March 18, 2015 12:39 PM
  • How to add a header value to an HTTP request is another issue that wasn't even mentioned in your original post. You should start a new thread if you have a new question. And please describe your issue clearly in the original post in the future.

    The reason why you get a FormatException is that the HttpClient tries to parse the string value into a DateTime object. The HttpWebRequest class also does this:
    http://stackoverflow.com/questions/11497177/c-sharp-httpwebrequest-date-header-formatting
    https://social.msdn.microsoft.com/forums/windowsapps/en-us/14187cc8-cba0-4995-aadd-0db8613cb43a/custom-date-header-format-for-httpclient


    You could try to use the TryAddWithoutValidation method:

    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Date", "3/18/2015 14:4:5");

    If this doesn't work you will have to pass the date string as some other request header and then take care of it at the receiving side:

    httpClient.DefaultRequestHeaders.Add("custom", "3/18/2015 14:4:5");


    Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question.

    • Marked as answer by Kaizen341 Wednesday, March 18, 2015 3:26 PM
    Wednesday, March 18, 2015 2:26 PM