0
votes

I'm using the bootstrap-datepicker-rails gem for datetime pickers. When I select a date, in the params it comes across in mm/dd/yyyy" format. I see this in the controller:

"start_date"=>"05/01/2018"

However, the model thinks it's dd/mm/yyyy:

(byebug) start_date
Fri, 05 Jan 2018

Here is my HTML:

<input data-provide="datepicker" class="form-control" 
format="mm/dd/yyyy" placeholder="mm/dd/yyyy" type="text" 
value="05/01/2018" name="gig[start_date]" id="gig_start_date">

How do I make the dates consistent?

2

2 Answers

0
votes

There are two ways of doing it.

Through js or in rails controller,

  1. rails way

First convert params string into Date, and Then using strftime function convert it into particular format.

def date_conversion(params_date)
  d = params_date.to_date
  converted_date = d.strftime("%d/%m/%Y") #use any format here
end

You can use Date.strptime(params_date, "%d/%m/%Y") method also

  1. In js,

    var date = $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' }).val();

Hope this will work

0
votes

This is what I did to get it to work:

<script>
$('#datepicker').datepicker({format: 'yyyy-mm-dd'});
</script>