Anders Lindén Anders Lindén - 3 months ago 15
AngularJS Question

ColumnMapping.DataTableToObjectList gives me DateTime properties lacking timezone information

I have a webapi function that retrieves data from a T-SQL database using SqlConnection, SqlCommand etc. I get a DataTable which I convert to a data structure using

var foobars = ColumnMapping.DataTableToObjectList<Foobar>(dt)

The Foobar class contains DateTime fields.

When I return my foobars from the webapi method, those datetimes will be sent over the network in the following format:


which seems to be a timezone-less datetime. AngularJS will treat it as 12:00, and javascript´s Date.getHours() will return 14 (I am in the +2 timezone).

If I do this operation before I return from my Web api method:

foobar.d = foobar.d.ToUniversalTime();

the string will get an additional Z


and both AngularJS and javascript will agree about which time that is (12:00, which is also the indended time).

If I do

foobar.d = foobar.d.DateStart.ToLocalTime();

the string will be


and AngularJS and javascript both reports it is 14:00, two hours more than the intended time).

From this, I conclude that

  • A DateTime object can be timezoneless, but if you try to convert it, it will be treated as if it was UTC.

  • ColumnMapping.DataTableToObjectList creates structures where DateTime objects are timezoneless.

I do not want to "patch" every DateTime object, that would be an error source (that would involve calling ToUniversalTime() on every DateTime object).

Can I specify a locale when I call ColumnMapping.DataTableToObjectList?


In webapi you can configure your JsonFormatter to serialize your datetimes how you want:

public static class WebApiConfig
    public static void Register(HttpConfiguration config)
        config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;

This way you don't have to do it for every datetime, assuming I'm understanding your issue...