javascript - How to format JS Date as a C# compatible DateTime? -


i've passed javascript datetime picker date view mvc controller action.

the format of string when passed controller action thu jul 28 2016 17:05:00 gmt+0100 causes invalid operation exception converting string datetime. in c#.

this how assign value of date picker hidden field value:

$('#outagestart').val(e.date) 

how can format js date string c# compatible datetime?

the binding error thrown in c# custom model binder tries bind js date string value nullable<datetime>:

public class datetimebinder : system.web.mvc.imodelbinder {       public object bindmodel(controllercontext controllercontext, system.web.mvc.modelbindingcontext bindingcontext)     {         var value = bindingcontext.valueprovider.getvalue(bindingcontext.modelname);         bindingcontext.modelstate.setmodelvalue(bindingcontext.modelname, value);          return value.convertto(typeof(datetime), cultureinfo.currentculture);     } } 

alternatively can use timestamp conversion , user's timezone won't effect result

in js

mydateobject.gettime() //-> outputs timestamp (integer) 

in c# (if have .net 4.6+)

datetimeoffset.fromunixtimeseconds(timestamp); 

otherwise

system.datetime dtdatetime = new datetime(1970,1,1,0,0,0,0,system.datetimekind.utc); dtdatetime = dtdatetime.addseconds(timestamp).tolocaltime(); 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -