0

I know a lot of questions about this has been answered. I have tried for about 3 hours with no luck. I am using angular-ui datetime picker, the format is

"2015-02-08T06:00:00.000Z"

Error message is string was not recognized as a datetime

at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style) at System.DateTime.ParseExact(String s, String format, IFormatProvider provider) at TransparentEnergy.Controllers.apiDocumentController.d__2.MoveNext() in c:\Development\TransparentEnergy\TransparentEnergy\ControllersAPI\apiDocumentController.cs:line 67

Controller

 string docDate = provider.FormData["DocumentDate"];
 model.DocumentDate = DateTime.ParseExact(docDate, "yyyy-MM-dd'T'HH:mm:ss'Z'", CultureInfo.GetCultureInfo("en-US"));

Angular-UI

 $scope.open = function ($event) {
    $event.preventDefault();
    $event.stopPropagation();

    $scope.opened = true;
};
$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
$scope.format = $scope.formats[3];

Update

 string docDate = provider.FormData["DocumentDate"];
            model.DocumentDate = DateTime.ParseExact(docDate, "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);

3 Answers 3

1

Is there any specific reason why you are not using DateTime.Parse()?

The format you have specified "2015-02-08T06:00:00.000Z" is ISO date time format. refer to http://en.wikipedia.org/wiki/ISO_8601

The one you have shown is UTC time and when you use

DateTime.Parse("2015-02-08T06:00:00.000Z")

you get local date-time. According to the timezone of the server \ pc you are running code on.

You can use

DateTime.Parse("2015-02-08T06:00:00.000Z").ToUniversalTime()

to get UTC. Does it help?

Sign up to request clarification or add additional context in comments.

3 Comments

no reason. just tried different stuff from examples. here is what the time format looks like at the string docDate breakpoint "\"2015-02-03T06:00:00.000Z\""
DateTime.Parse("2015-02-08T06:00:00.000Z").ToUniversalTime() is giving the same error
@texas697 All code sample would give errors. The reason are the quotes in the string. I agree with Jon Skeet that the quotes should be there. Ideally you should get rid of the quotes from the source itself. However, if you want to patch the code for the time being you could use DateTime.Parse("\"2015-02-03T06:00:00.000Z\"".Trim('"') ). i.e. DateTime.Parse(docDate.Trim('"') )
0

Look at the format you're passing:

"yyyy-MM-dd'T'HH:mm:ss'Z'"

That has no milliseconds, whereas your sample is "2015-02-08T06:00:00.000Z" which does have milliseconds. It looks like you want:

"yyyy-MM-dd'T'HH:mm:ss.fff'Z'"

Also, I'd suggest using CultureInfo.InvariantCulture rather than the US culture - they'll both work the same in this case, but I think it's clearer to use the invariant culture when you're basically talking about a machine-to-machine format.

You should also include DateTimeStyles.AssumeUniversal to take account of the Z.

4 Comments

still giving the same error. I updated my question with your code
@texas697: The code you've posted works fine when docDate is "2015-02-08T06:00:00.000Z" - I've just tried it. How certain are you about the value you're trying to parse? Ideally, post a short but complete program demonstrating the problem
this is from breakpoint at string docDate "\"2015-02-03T06:00:00.000Z\""
Right, so that contains quotes at the start and end - it's not clear why though. I suggest you look at the code performing the post; I wouldn't expect that to be in the value.
0

Try this format

yyyy-MM-dd'T'HH:mm:ss.fff'Z'

  var date = DateTime.ParseExact("2015-02-08T06:00:00.000Z", "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", CultureInfo.InvariantCulture);

2 Comments

here is what the time format looks like at the string docDate breakpoint "\"2015-02-03T06:00:00.000Z\""
For example your code from yours Update section working in my VS without exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.