javascript - Bootstrap date time picker display format vs value format -


i using following plugin selecting date , time.

https://eonasdan.github.io/bootstrap-datetimepicker/

it works great in cases. in cases display date in input field mm/dd/yyyy when submit server, need yyyy-mm-dd

what best way accomplish this? thinking of overriding submit function form. kind of want avoid somehow using events can contain code in 1 place instead of adding bunch of logic each form.

ashish , chris code may work, since bootstrap 3 datepicker requires moment.js, why not use moment.js parse (to parse date custom or locale format chosen datepicker) , moment.js format (to convert date in iso format e.g. sent asp.net controller or other endpoints accepting kind of format datetime)?

so code way shorter, this:

  $('#datetimepicker1').on("dp.change",     function (e) {       var submitdatestring = '';       if (e.date) {         submitdatestring = e.date.format();       }       $("#datetime-submit").val(submitdatestring);     }); 

explanation:

  • the dp.change event (e) has date property, moment object, don't need care date format chose datepicker.
  • when datepicker cleared e.date false, if check.
  • $("#datepicker1") input field bind bootstrap datepicker.
  • $("#datetime-submit") hidden input field.

to answer op, if desired format submit "yyyy-mm-dd", use:

submitdatestring = e.date.format("yyyy-mm-dd"); 

i discovered moment.js , love it. use make date parsing & displaying easier.


Comments