Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate date input bootstrap Date Range Picker

I have form that use daterangepicker

url : https://github.com/dangrossman/daterangepicker

I was able to attach daterangepicker into my form with this simple script.

script 

//Date range picker
$('.daterp').daterangepicker();

/script

and just add class to my form.

class="form-control daterp"

so it will be

<input type="text" name="start_date" style="width: 100%;" required="" class="form-control daterp" id="id_start_date">

when I click submit its give me like 07/29/2018 - 07/29/2018 submitted to my database.

that not what I need.

imagine my database table like this

#table shift
+---------------+---------------+
| start         | end           |
+---------------+---------------+
| 2018-01-01    | 2018-01-05    | 
| 2018-01-08    | 2018-01-14    |
| ....          | ....          |
+---------------+---------------+

What I need when I click submit on my form field with value 07/29/2018 - 07/29/2018

07/29/2018 is going to start

and

07/29/2018 is going to end

but with only single form field

how to do that?...

thank you!

like image 806
Dicky Raambo Avatar asked Oct 30 '25 00:10

Dicky Raambo


2 Answers

You can check out this link

$('#someinput').daterangepicker(options, callback);
//parse the start date & end date
startDate = $('#someinput').data('daterangepicker').startDate;
endDate = $('#someinput').data('daterangepicker').endDate;

//format it 
Start = start_date.format('YYYY-MM-DD');
End = end_date.format('YYYY-MM-DD');
like image 104
H.M Maruf Avatar answered Nov 01 '25 16:11

H.M Maruf


If you want it to be sent to the form in a single field, then it seems to be that you're going to need to parse that single field out on the back end using whatever code your back end is running.

But it sounds to me like what you really want is to separate the dates into two separate fields. So change the name of the main field to "date_range" and add two hidden fields called "start_date" and "end_date", then either on form submit or on an event tied to the date range picker, split the values like so:

$('#start_date').val( $('#date_range').text().split(' - ')[0]);
$('#end_date').val( $('#date_range').text().split(' - ')[1]);
like image 31
Rick Root Avatar answered Nov 01 '25 15:11

Rick Root