I have a form that received dates via two variable :start and :end. When I input the date they look right, but when I save, the model receive a date with the days and months flipped, and when the day is greater than 12, say 13th of March, it will simply show nil error.
My form field look like:
<div class="input-group date" id="datetimepicker4" data-target-input="nearest">
<%= f.text_field(:start, value: f.object.start ? f.object.start.strftime('%m/%d/%Y %l:%M %p') : nil, class: "form-control datetimepicker-input", data: {target: "#datetimepicker4"}, placeholder: "#{t :From}", required: true) %>
<div class="input-group-append" data-target="#datetimepicker4" data-toggle="datetimepicker">
<div class="input-group-text"><span class="fas fa-calendar-alt"></span></div>
</div>
</div>
Same for :end variable
The input form looks like :
The results looks like:
Looks like you try to use US date format, but rails is parsing submitted params with month and day switched. I am not sure if you would prefer to use US or international date format and I advise you to check how rails is handling parsing submitted params.
Also, have a look at defaults of calendar date time picker you use.
UPDATE.
I will try to provide more details. In one of my projects I am using a similar date time picker that looks like this
When submitted you will have the following params in our controller
{"date_time_picker_input_field"=>"2020-01-20"}.
The reason my date time picker formats date as YYYY-MM-DD is that in the view code I am using an option that is taken by datetimepicker code upon initialisation, something similar to
date_format: 'yyyy-mm-dd',
(this can be different in the date time picker you use and I suggested earlier to check documentation on options you can use).
I don't have to use strftime on date time provided to text_field, because date time JS code will format the date in format of your choice.
Related
I'm really new to Rails and I'm using v6.1.4. In my application I'm using I18n localization. At other places for view-only label dates(not using BIP) I use
<%=l #client.active_date.to_date %>
and it works. However, in Best-In-Place there is a different syntax.
<span class="date-container"><%= best_in_place task, :due_date, :as => :date, class: 'hyperlink' %></span>
This causes the dates to be displayed in the yyyy-mm-dd format throughout. I want the dates to be formatted according to the locales currently in my application. I'm using the JQuery datepicker, but it is activated after I click on the date, and the format is changed to what I have set on the jquery setDefaults option.
How do I tackle this?
I think this post addresses your question.
You could add a display_as: :formatted_date to your method call and define that formatted_date method in your model. From there you should be able to format dates as needed.
here is the form
<div class="field form-group">
<h6><%= form.label :start_date %></h6>
<div class="input-group">
<%= form.date_field :start_date, class: "form-control form-input", required: true %>
</div>
</div>
when start date printed it is printing in following format
2020-10-15 00:00:00 UTC
i want it in format of Date.current i.e 2020-10-15 so that i can compare it with Date.current
Bad idea
Do not store formatted strings in the database as you're destroying your data modeling to solve a trivial formatting concern that should be solved in the application.
Its really as easy as:
Thing.start_date.stftime('%Y-%m-%d')
There are also tons of other more elegant ways like helper methods and decorators.
Use the actual timestamp/date/time type columns that are suitable for your database and the domain.
If you store dates/times as strings/integers or something else silly you (and whatever poor sod has to maintain your monstrosity) will pay for it immensely when querying the data as you can't actually use it as a date without casting.
Like for example this:
Thing.where(start_date: to..from)
Turns into a monster:
# This code will only work on Postgres...
Thing.where("TO_DATE(start_date, 'YYYY-MM-DD') BETWEEN ? AND ?", to, from)
you if want to get date in particular format, then please use strftime method. Ref: strftime
In your case use like this:
obj.start_date.strftime("%Y-%m-%d")
Happy Coding :-)
I have added datepicker in _form.html.erb page to pick date at the time of creation. but when i click on create button it is showing field cannot be blank. when i see the params, it is showing "dob"=>"08/17/2012", but it is not passing the validation. I am not sure weather it is a format problem. can anyone help me to solve this problem. thnks
my application.html.erb file contains:-
<%= javascript_include_tag "jquery-1.8.0.min" %>
<%= javascript_include_tag "jquery-ui-1.8.23.custom.min" %>
<!--%= javascript_include_tag "application" %-->
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
and _form.html.erb contains:-
<div class="span10 date-wid">
<%= f.text_field :dob, :id => 'datepicker' %>
</div>
It probably is your format. The correct format to save time to a database is this
For the date , March 1st, 2012 or 3/1/2012 :
20120301
So the order goes Year, Month, Day in the structure of 4 digits, 2 digits, 2 digits. Protip, you can add more which would be your hours minutes and seconds as well.
So make sure your datepicker is producing that as well :
$(".date-single-view").datepicker({
dateFormat: "yyyy-mm-dd"
});
That will add -, but it should still work. You can also do other things like have the datepicker on select drop the data format to a hidden input div, and the input field is just for show.
You can also use the Chronic gem to properly parse various different means of time.
https://github.com/mojombo/chronic
I'm using a date select in a rails 3 form.
<%=f.date_select :date %>
I would like to restrict the dates so that you can only pick dates that fall on a Sunday. Is there any way of going about doing this?
I'm also trying to stop dates which have already passed from appearing.
Thanks for any help in advance!
Rails date_select field generates three dropdown to select the parts of the date. There is no chanche, that you modify for example the month, and the day will still be sunday.
You must write some js magic to enforce such a role, or find an already existing datepicker and limit it. Or alternatively, you let the user to select a week, and calculate the exact date of sunday from that.
Ok having studied this out a bit further I don't think this is possible due to the format of the date_select field. The closest I can get is
<%=f.date_select :date, start_year: Time.now.year %>
so that at least you can't select dates from previous years. I've implemented the restriction on days and months that have past by setting up the view to automatically delete records that aren't relevant:
<% if(service.date < Date.today) %>
<% service.destroy %>
<% end %>
Not perfect but does the job in my case.
I have an attribute in my user database of type "Date".
I want to show it in the "show view", in any format.
What shall I write in show.html.erb so that I can see the contents of this field of a user object ?
Thanks :)
You might want to store the date format in a config file and/or a helper so you can easily modify it and use it globally wherever you are printing it in a view.
You can print a date like this:
<%= #user.datecolumn.strftime('%B %d, %Y') %>
See this article where the date formats are listed and there is more information on the subject:
http://www.linux-mag.com/id/4070/
<%= user.created_at.strftime("%B %m") %>
Take a look at the strftime method: http://www.ruby-doc.org/core-1.9.3/Time.html#method-i-strftime