Apologies if the title is not clear, I' not really sure how to explain this clearly.
In a Rails app I'm pulling in users' data from a 3rd party provider's API. One of the attributes is the user's birthday.
I am storing the hashed data in my database and, in certain circumstances, need to display the user's birthday in the view.
The problem is that the birthday is not formatted as a Date. Within the hash, it is in the format mm/dd/yyyy. This means that my usual date scoped formats don't work on it.
Given that I am extracting the birthday from the hashed data column as follows
<%= #user.hashed_data["info"]["birthday"] %>
what is the best/ most efficient way to handle this so that I can display localized date formats?
Do I need to split on the / symbols, and then recombine the resulting string to a date? Or am I overcomplicating this?
Thanks for any suggestions.
Try:
Date.strptime('01/31/2011', '%m/%d/%Y')
# => #<Date: 2011-01-31 (4911185/2,0,2299161)>
The simplest way to convert plain text to date or datetime is to use the chronic gem.
A very usefull parameter that you can pass to the chronic parser is the :endian_precedence . See my answer here for more details.
Regards, Dorian
Related
I use php and dynatable.js (version 0.3.1) to create dynamic tables with a sort and search function. This setup works very well even with the U.S. formatted date from the SQL database. But I need the german date format in the table (dd.mm.yy or dd.mm.yyyy) to sort the entries.
I can't find a solution for my problem in the documentation from dynatable.js. Has someone had a similar problem with the date format in dynatable and maybe a hack or other kind of solution?
Very simple question but could not find a solution or figure it out yet.
How to convert this string "04/30/2015" to valid datetime for storing inside postgres database?
I tried several ways to no avail:
new_date = "#{ params[:interview][:expire_at]}00:00:00".to_datetime
This should do:
Date.strptime(params[:interview][:expire_at], '%m/%d/%Y')
using .to_datetime will be reduntant at that point, considering it should exactly do what you are trying to achieve.
"30-4-2015".to_datetime as well as "2015/30/4".to_datetime will be accepted, so it would need some extra formatting to your string to work.
.to_datetime requires the day to be passed before the month. no matter if the year is pre- or appended, or what seperator you use, check the examples in the apidoc.
However, using Date appears to be more straight forward.
Use this method:
Date.strptime(params[:interview][:expire_at], '%m/%d/%Y').to_datetime
I want to allow teachers to be able to login to my Rails 3.2 app and be able to set when they are available. So instead of having two datetime fields where an actual date is stored is stored for starts_at and ends_at, I'd like for them just to say I'm available on "Mondays between 4:00pm and 5:00pm" with all three values being dropdowns.
The orignal way I approached this was having a string for day and using the time_select method in my form for my starts_at and ends_at. Unfortunately, time_select still comes with the date.
I'm just looking for the cleanest way to allow weekly scheduling. Is this possible? If it is, is there an easier way to do this? Thanks in advance for your tips.
take a look at
https://github.com/mzararagoza/rails-fullcalendar-icecube
its a small appointment app that i think that you can take allot out of it.
I'm scoping out how to handle user-generated dates with rails and was hoping to get some thoughts on the best way to handle them. Ultimately I want to display dates/times provided by the user in HTML, and I also want to sort by the dates (ascending and descending, depending). My initial thought was to create the db as time: string and date:string and then convert these strings to datetime values. Is there a better way to go about this? I'm using RoR 3.1.
Any thoughts and ideas would be greatly appreciated so I don't start down the wrong path only to realize at a later date.
Yes, there is a better way to go about it. ActiveRecord supports the following types for times:
datetime
timestamp
time
date
So pick the type that matches your intent (time for time of day, date for a date, ...) and then let AR do its job and deal with the details. Then you'll get the right data in the database and the right objects in your Ruby; once you have the right objects in your Ruby everything will sort and compare and what not properly.
My database has a datetime field, and I want to be able to create new entries. Obviously the Rails datetime_select helper isn't the most user friendly thing to have in your form.
I'd rather have a text field for the datetime (or one for the date, and one for the time) and interpret the inputs like PHP strtotime can.
I might just be searching the wrong keywords. Surely this has been discussed in great depth somewhere.
Thanks
:0)
Check out Railscast #32, I've used this method a few times and it works pretty well.