Cyassin
Cyassin

Reputation: 1490

ISO8601 direct DateTime parse incorrect by single day

I am trying to pass two datetime fields (start date and end date) to my webapi server in which the ISO8601 format date string in my javascript appears to be passing correctly, but the server is interpretting both dates incorrectly by a single day.

/service/api/application/2016-09-01T14:00:00.000Z/2016-09-30T13:59:59.999Z/

results in:

Start Date ='2016-09-02 00:00:00'

End Date = '2016-10-01 00:00:00'

[Route("api/Application/{dateLodgedStart}/{dateLodgedEnd}")]
        [ResponseType(typeof(PagedResultObject<ApplicationObject>))]
        public HttpResponseMessage Get(DateTime? dateLodgedStart, DateTime? dateLodgedEnd) {
 //Do stuff
}

Any thoughts?

Upvotes: 1

Views: 173

Answers (1)

Enigmativity
Enigmativity

Reputation: 117104

Because you are in GMT+10 and you are parsing dates in "Zulu Time" - i.e. GMT+0 then the parser is automatically adding the 10 hours for you.

If we use C# for the parsing then we can see this using DateTimeOffset.Parse:

var startDate =
    DateTimeOffset
        .Parse("2016-09-01T14:00:00.000Z")
        .ToOffset(TimeSpan.FromHours(10.0));

var endDate =
    DateTimeOffset
        .Parse("2016-09-30T13:59:59.999Z")
        .ToOffset(TimeSpan.FromHours(10.0));            

Console.WriteLine(startDate);
Console.WriteLine(endDate);

This produces:

2016/09/02 00:00:00 +10:00
2016/09/30 23:59:59 +10:00

There does still appear to be an odd rounding error on your end date though.

Upvotes: 3

Related Questions