c# - DateTime with timezone in ASP MVC Web API + EF -


i using asp mvc web api + ef , clients getting datetime without information timezone. trying setup settings in webapiconfig without success:

config.formatters.jsonformatter.serializersettings.datetimezonehandling  = datetimezonehandling.local; 

the 1 way working me: create new instance of datetime datetimekind.local:

    public ienumerable<clientdto> execute()     {         var clients = this.dbcontext.clients.select(             m => new clientdto         {             id = m.id,             notificationsendingtime = m.notificationsendingtime,             . . .         }).tolist();          clients.foreach(m => m.notificationsendingtime =              m.notificationsendingtime.hasvalue              ? new datetime(m.notificationsendingtime.value.ticks, datetimekind.local)              : m.notificationsendingtime);          return clients;     } 

but in case have use .tolist() , set each item new datetime timezone.

how can setup webapi or ef add information timezone automatically? thanks.

update

it seems i've found solution. html:

<!-- timepicker --> <input id="notificationsendingtime"        name="notificationsendingtime"        type="text"        class="form-control"        data-ng-model="notificationsendingtime"        bs-timepicker        data-time-format="hh:mm"        data-time-type="date"        data-length="1" data-minute-step="30"        data-arrow-behavior="picker" />  <!-- timezone --> <button type="button" class="btn btn-default full-width time-zone" ng-model="formdata.timezoneid"         data-html="1" data-animation="" placeholder="time zone..."         ng-options="timezone.id timezone.friendlyname timezone in timezones" bs-select>     action <span class="caret"></span> </button> 

my clientdto:

public class clientdto {     public int id { get; set; }     public datetime? notificationsendingtime { get; set; }      public datetimeoffset? notificationsendingtimeoffset     {                 {             if (!this.notificationsendingtime.hasvalue)             {                 return null;             }              var timezoneinfo = timezoneinfo.findsystemtimezonebyid(string.isnullorwhitespace(this.timezoneid) ? "greenwich standard time" : this.timezoneid);             var offset = timezoneinfo.converttimefromutc(this.notificationsendingtime.value, timezoneinfo);             return offset;         }     }     public string timezoneid { get; set; } } 

updating of client on server-side:

if (command.commandarg.notificationsendingtime.hasvalue) {     var timezoneinfo = timezoneinfo.findsystemtimezonebyid(string.isnullorwhitespace(command.commandarg.timezoneid) ? "greenwich standard time" : command.commandarg.timezoneid);     var utc = timezoneinfo.converttimetoutc(command.commandarg.notificationsendingtime.value, timezoneinfo);     command.commandarg.notificationsendingtime = utc.date + new timespan(utc.hour, 0, 0); }  client.notificationsendingtime = command.commandarg.notificationsendingtime; client.timezoneid = command.commandarg.timezoneid;  this.dbcontext.savechanges(); 

in angularjs controller after fetching data:

$scope.notificationsendingtime = $scope.formdata.notificationsendingtimeoffset; 

in angularjs controller before submitting data:

    $scope.formdata.notificationsendingtime  = $filter('date')($scope.notificationsendingtime, 'hh:mm'); 

the timepicker works in browsers! thanks.

if storing time zone info in separate column think it's not simple make ef conversion stuff. suggested put functionality in model itself. simple example dto looks candidate:

[datacontract] public class client {     [datamember]     public datetime created { get; set; }      [datamember]     public string createdtimezoneid { get; set; } }  public class clientdto {     private readonly client _client;      public clientdto(client client)     {         _client = client;     }      public datetimeoffset created     {                 {             //todo: should moved in separate helper method.             var timezoneinfo = timezoneinfo.findsystemtimezonebyid(_client.createdtimezoneid);             return new datetimeoffset(_client.created, timezoneinfo.baseutcoffset);         }     } } 

by using datetimeoffset type freeing worrying time zone handling. json.net working type:

// simulate db call var clients = new list<client> {     new client     {         created = new datetime(2015, 4, 27, 11, 48, 22, datetimekind.unspecified),         createdtimezoneid = "fle standard time"     } };  var clientdtos = clients.select(client => new clientdto(client)); var json = jsonconvert.serializeobject(clientdtos); 

the resulting json be:

[{"created":"2015-04-27t11:48:22+02:00"}] 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -