Date display format for loading date and time

I use the following plugin to select the date and time.

https://eonasdan.imtqy.com/bootstrap-datetimepicker/

In most cases, it works great. However, in some cases, I display the date in the input field as MM / DD / YYYY, but when I send the server I need it as YYYY-MM-DD

What is the best way to do this? I am thinking of overriding the submit function for the form. But I kind of want to avoid this, since I would like to somehow do it all with the help of my events, so that I can keep the code in one place, and not add a bunch of logic for each form.

+4
source share
4 answers

, Bootstrap 3 Datepicker Moment.js, Moment.js Parse ( , DatePicker) Moment.js Format ( ISO, , ASP.NET , DateTime)?

, , :

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

(. ): e.Date e.date .

:

  • dp.change(e) Date, , , .
  • , e.Date false, if.
  • $ ("# datepicker1") - , .
  • $ ("# datetime-submit") - .

OP, - "--", :

submitDateString = e.Date.format("YYYY-MM-DD");

Moment.js . , .

+5

. , .

MM/DD/YYYY

, YYYY/MM/DD

MM/DD/YYYY YYYY/MM/DD java script ,

0

:

function date_time_picker_field_report($field, format)
{
    var id_of_field = $field.attr('id');
    var name_of_field = $field.attr('name');

    var id_copy_of_field = id_of_field+'_copy';
    var name_copy_of_field = name_of_field+'_copy';

    //Set $field to be a copy that is used for actual display of date info
    $field.attr('id', id_copy_of_field);
    $field.attr('name', name_copy_of_field);

    //create hidden input to track field behind the scenes (better value)
    $('<input>').attr({
        type: 'hidden',
        id: id_of_field,
        name: name_of_field
    }).insertAfter($field);


   $field.on("dp.change", function(e) 
    {   
        //If we have a space seperator it has a time; otherwise just a date (time has 2 spaces; between date + time and AM/PM (12 hour))
        var does_date_have_time = format.indexOf(' ') != -1;
        var date = e.date;

        var formated_date = null;

        if (does_date_have_time)
        {
            formated_date = date.format("YYYY-MM-DD HH:mm");
        }
        else
        {
            formated_date = date.format("YYYY-MM-DD");
        }

        $('#'+id_of_field).val(formated_date);
   });

    var defaultDate = null;

    if (id_of_field == 'start_date')
    {
        defaultDate = moment();
        defaultDate.set('hour', 0);
        defaultDate.set('minute', 0);
    }
    else if(id_of_field == 'end_date')
    {
        defaultDate = moment();
        defaultDate.set('hour', 23);
        defaultDate.set('minute', 59);
    }

   $field.datetimepicker({format: format, locale: LOCALE, defaultDate: defaultDate});   
}
0
source
<input type="hidden" id="fromDate"> 
<input id="fromDateDisplay" class="form-control datefilter" data-date-format="DD/MM/YYYY" readonly="readonly">

$('#fromDateDisplay').datetimepicker({
    pickTime: false
});

$('#fromDateDisplay').on("dp.change",function (e) {
    $(this).blur().change();
    var d=new Date($("#fromDateDisplay").val());  
    $("#fromDate").val(dateParserForDatePicker(d)); 
});

//this function is use for converting date into MM/DD/YYYY format display
function dateParserForDatePicker(date) {    
    return ('0'+(date.getMonth()+1)).slice( -2 )+"/"+('0'+(date.getDate())).slice(-2)+"/"+date.getFullYear();
} 
0
source

Source: https://habr.com/ru/post/1598782/


All Articles