c# - Deserialization JSON time MVC -


i try time ajax request datetype json . result :

 /date(1436688000000)/  

heres code :

view:

  <div class="col-sm-8">        @html.dropdownlistfor(model => model[i].movieshowtime.single().movieid, selectmovieid, "select movie", new { id = "moviename", name = "moviename" })       @html.dropdownlistfor(x => x[i].movieshowtimeid, enumerable.empty<selectlistitem>(), "--loading value--", new { id = "showtime", name = "showtime" })       @html.validationmessagefor(model=>model[i].movieshowtimeid)   </div> 

controller :

        public jsonresult getshowtime(int? movieid)     {          var data = (from m in db.movieshowtimes                     m.movieid == movieid                     select new                     {                         id = m.movieshowtimeid,                         name = m.showtime                     }).tolist();          return json(data, jsonrequestbehavior.allowget);     } 

ajax : .

           $(function () {             $('#moviename').change(function () {             $.ajax({             type: "post",             url: '@url.action("getshowtime", "timescreening")',             data: { movieid: $('#moviename').val() },             datatype : 'json',             success: function (data) {                 $('#showtime').html('');                  //alert(changedateformat("\/date(1319266795390+0800)\/"));                 $.each(data, function (id, option) {                     var name = changedateformat(option.name)                     $('#showtime').append($('<option></option>').val(option.id).html(option.name));                 });              },             error: function (xhr, ajaxoptions, throwneror) {                 alert("false" + xhr +"..."+ ajaxoptions +"... "+ throwneror);             }         });     }); }); 

i see threds convert form json c# datetime none of them have resolved problem . follow post: json-serialization-and-deserialization-in-asp-net jave me closet answer , in date.

code:

function changedateformat(jsondate) {  jsondate = jsondate.replace("/date(", "").replace(")/", "");  if (jsondate.indexof("+") > 0) {      jsondate = jsondate.substring(0, jsondate.indexof("+"));  }  else if (jsondate.indexof("-") > 0) {      jsondate = jsondate.substring(0, jsondate.indexof("-"));  }   var date = new date(parseint(jsondate, 10));  var month = date.getmonth() + 1 < 10 ?      "0" + (date.getmonth() + 1) : date.getmonth() + 1;  var currentdate = date.getdate() < 10 ? "0" + date.getdate() : date.getdate();  return date.getfullyear() + "-" + month + "-" + currentdate;  } 

this answer : format microsoft json date no ugly parsing gave me datetime.now , no close time.

this answer : asp.net mvc jsonresult date format same.

and artical same.. dates-and-json

so.. need do?

i have set of functions

// start datetime converters function dateconverter(date) {     var aux = null;     if (date != null) {         aux = date.substring(6);         aux = aux.substring(0, aux.indexof(')'));     }      return aux != null ? getisodate(new date(parseint(aux))) : ""; }  function datetimeconverter(date) {     var aux = null;     if (date != null) {         aux = date.substring(6);         aux = aux.substring(0, aux.indexof(')'));     }      return aux != null ? getisodatetime(new date(parseint(aux))) : ""; }  function getisodate(d) {     // padding function     var s = function (a, b) { return (1e15 + + "").slice(-b) };      // default date parameter     if (typeof d === 'undefined') {         d = new date();     };      // return iso datetime     return zeropad(d.getdate(), 2) + '/' +     zeropad(s(d.getmonth() + 1, 2), 2) + '/' +     zeropad(d.getfullyear(), 4); }  function getisodatetime(d) {     // padding function     var s = function (a, b) { return (1e15 + + "").slice(-b) };      // default date parameter     if (typeof d === 'undefined') {         d = new date();     };      // return iso datetime     return zeropad(d.getdate(), 2) + '/' +     zeropad(s(d.getmonth() + 1, 2), 2) + '/' +     zeropad(d.getfullyear(), 4) + " " +     zeropad(d.gethours(), 2) + ":" +     zeropad(d.getminutes(), 2) + ":" +     zeropad(d.getseconds(), 2); }  function zeropad(num, places) {     var 0 = places - num.tostring().length + 1;     return array(+(zero > 0 && zero)).join("0") + num; } // end datetime converters 

example: in table show createddate:

{     "mrender": function (oaux, otype, odata) {         return dateconverter(odata.createddate);     }, }, 

if want date , time use datetimeconverter

what i'm doing

inside function dateconverter , datetimeconverter catch number without \date... "1436688000000".

then inside getisodate, in first line:

var s = function (a, b) { return (1e15 + + "").slice(-b) }; 

is padding function. day 2 become "02" if use like:

s(d.getdate(), 2) 

if date action returns null or invalid:

if (typeof d === 'undefined') {      d = new date(); }; 

the other padding function zeropad function s() doesn't remove left numbers, example:

var = 3000; var sa = s(3000, 2); var zpa = zeropad(3000, 2); 

sa become "00" while zpa keep "3000"

ps: can't remember why used s function... think forgot delete after creating zeropad.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -