javascript - How to separate date input bootstrap Date Range Picker - Stack Overflow

I have form that use daterangepicker url :I was able to attach daterangepicker into my form with this

I have form that use daterangepicker

url :

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!

I have form that use daterangepicker

url : https://github./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!

Share Improve this question asked Jul 29, 2018 at 14:34 Dicky RaamboDicky Raambo 5232 gold badges14 silver badges31 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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');

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]);

You can split the date range string by ' - ' delimiter on your back-end. If you use node.js you could try to do

[startDate, endDate] = '07/12/2018 - 07/28/2018'.split(' - ');

Or if you use PHP

list($startDate, $endDate) = explode(' - ', $_POST['start_date']);

The other solution is to use set the values before form submission For example, In HTML:

<form action="index.php" method="post">
    <input type="text" class="date_range">
    <input type="hidden" name="start_date">
    <input type="hidden" name="end_date">
    <input type="submit">
</form>

In JS:

$('input.date_range').daterangepicker();
$('form').submit(function (ev, picker) {
    [startDate, endDate] = $('.date_range').val().split(' - ');
    $(this).find('input[name="start_date"]').val(startDate);
    $(this).find('input[name="end_date"]').val(endDate);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744274266a4566251.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信