I don't know why this is so difficult and I'm sure there's an easy answer, but i've been looking for about an hour and cannot find it. I have a form where I want someone to enter a time into a text field in the form hh:mm:ss . What I have now is:
<%= f.text_field :complete_time %>
:complete_time is an attribute of data type 'time'. So if someone enters '15:53:00', the :complete_time is stored as:
2000-01-01 15:53:00 UTC
I can't figure out how to let the user set the date of the time. Above the time field, I also have a datepicker field, but it is mapped to its own attribute. I would use the date picker in conjunction with the time text field if i knew how, but I don't. How do I let the user pick the date of the time object?
Well, there is this plugin you can use in with date picker to pick the time, it's really easy to use. http://trentrichardson.com/examples/timepicker
Related
Reading through the docs, I see a date picker and time picker field separately but not just one timestamp field that includes both. Is there any way to accomplish this same idea outside the box?
What about datetimepicker? Check it out here: https://app.slack.com/block-kit-builder/
I'm trying to get rid of fixing date input by user on Date field in Orbeon forms.
I had formatted my Date field to DD/MM/YYYY and when I type:
44/07/2022
It automatically changes to 13/08/2022 (moves extra days to next month)
I just want to validate wrong input without "fixing" it and ask user to correct it by himself. Is it possible to disable this feature by prop or make any workarounds? I checked documentation and didn't find any clues.
I'm using Rails 4, and also Twitter Bootstrap.
I'd like to select the date with a date picker, but have a separately visible component for selecting the hour and minutes.
I've had a look at smalot bootstrap-datetimepicker https://github.com/smalot/bootstrap-datetimepicker , but looking at the Demo Page, they don't demonstrate any ability to show date and time separately. You have to choose date, and then later on choose time, which doesn't feel very intuitive.
I've also looked at Eonasdan bootstrap-datetimepicker https://github.com/Eonasdan/bootstrap-datetimepicker , but the time picking for it, even in inline mode, is not intuitive - will people know they can just click on the hour value to change it?
I'm thinking of just using a date picker for picking the date, and selecting the hour and minute myself, but it kind of feels wrong handing off part of a datetime to a gem/library and handling the rest of it myself.
I came across Separate date and time form fields in Rails , which is asking about this kind of problem, but it's a question from September 2010.
How do I select the date with a date picker, but have simple and immediately visible selection of time?
First, unless you find a plugin that does what you want off the rack, then yes, it's up to you to handle it, and yes, it feels kinda wrong - depending on how you do it.
Not sure what you had in mind, but the way it feels "the most wrong" is if your form has a single "date time" field under the hood, and you use javascript to botch together the date from the plugin and the time from your own setup, and store them in your datetime field. The nice thing about this is your rails app just gets a single datetime field and knows exactly what to do with it.
Here's how I'd approach it:
Keeping in mind that forms don't necessarily have to map 1-to-1 with your models, I'd split it in the controller layer, and conceptually think of "a form with two fields: date, and time", and then in your controller (or a form object, which is probably better for this situation) you'd stitch the date and time together, before saving them to your model. This approach means you can have separate validation on each field, which is probably also what you want (because I'd assume it's possible for users to input a valid date, but an invalid time or vica versa).
In terms of handling the date with a plugin and the time yourself, that's now fine - they're two completely separate fields from the perspective of your form, so there's nothing dirty about it. It just means you need the extra logic in your controller layer to split the datetime when you display the form, and merge the date and time back into one when you save the form.
Edit: if you haven't heard of form objects, check out https://github.com/apotonick/reform and http://railscasts.com/episodes/416-form-objects
I have separate date and time field: ie Start date , Start time, End date, End time. Im using jquery time picker, and date pair plugin.
If I fill either one of start date or end date, they will both be filled in with values automatically. And same in Start time and end time, they will be both filled in. So I am testing these scenarios:
Start date and End date are filled in with values. Start time and end time are blank. After clicking submit, I have the right error, no problem, but it is returning the start time and end time with values. which I dont want.
Start time and End time are filled in with values. Start date and end date are blank. After clicking submit, ofc I have the right error, no problem, but it is returning the start date and end date with values (ie: the date today). which I dont want.
I have a feeling this is the normal behaviour of jquery plugin time picker after failed submission, it will automatically set default values if left blank in first submission.
This is not a big deal to be honest, but Im just concern for better UX.
PLease help.
Inspect your form. It seems that the input fields have the same name, so if you fill start_date the form will pick this value and fill it into start_time.
I was able to solve it by simple using a jquery.
(I'm using Rails 3.2.3 on Ruby MRI 1.9.2)
Everything is stored centrally in UTC. This is good. Each User has a 'timezone' property. My ApplicationController has a before filter to adjust Time.zone to the User's stored timezone. This is also good.
Given the above, if my User completes a datetime select with a time, that time is expected to be in Time.zone and Rails will automatically adjust and save this as UTC. This too, is good.
Now: I wish to enable my Users to fill out a time in another time zone and then have it stored as UTC. However, Rails is expecting the completed date time select to have been filled out in the (previously set by ApplicationController) Time.zone. Therefore Rails adjusts to UTC incorrectly.
How do I achieve my goal of saving a time that has been input as being in a third time zone?
Illustration:
My usage scenario is a User in Florida adjusting a date for a Document belonging to a Hotel on the West Coast. They are looking to enter a time for the West Coast.
I'm using jQuery to style text boxes with a styled picker, so I have a method to output a string to the text box:
<%= f.text_field(:created_at, :value => adjusted_to_hotel_time(#document.created_at), :class => 'text datetime_picker') %>
def adjusted_to_hotel_time(time)
time.in_time_zone(#current_hotel.timezone).to_s(:admin_jquery) # formatted for the jQuery datetime_picker text fields.
end
This is working perfectly, but Rails adjusts to UTC incorrectly when the #document is saved. I don't know what I don't know - how can I 'tag' the data entered in that field as being in #current_hotel.timezone, so that Rails will offset to UTC correctly when it saves the parent object?
Cracked it!
Basically, the string that is submitted to params is representing a time in a Hotel's timezone. We have to use the built in 'use_zone' method to temporarily set the global Time.zone to that Hotel's.
We then pass the method a block where we produce the value that Rails is expecting, by using the timezone of the Hotel, rather than the User. That means that Rails' conversion to UTC results in the correct time in the db - as the time entered on the form has been converted to the Users timezone. The offsets have cancelled each other out, effectively.
#document.created_at = Time.use_zone(#current_hotel.timezone) {Time.zone.parse("#{params[:document][:created_at]}").in_time_zone(#current_hotel.timezone)}
We're basically changing the timezone of the Time object here without converting the actual time of that Time object when the timezone changes. (Good luck parsing that sentence!)
This looks to be working fine for me, but I've been looking at this for too long and I'd love to see a better way/more Rails-ey way of doing this!
The datetime column type only stores a date and time, but not the time-zone. You may need to create a secondary column to preserve the time zone used to interpret the UTC time saved there.
These two values could combine to re-create your initial input.