I am saving dates with the followings formats 1985-01-04 and 19850104, then I need to show the format Year of this way 1985 January 4.
but, i got this got: 04 January 1985.
could I change this format? I'm using rails 6.
I have wanted to change this option default because I am implement testing with this format: 1985 January 4.
Use 'strftime' to format a date for your needs:
Time.now.strftime("%Y %B %d")
See this cheat sheet for many other options of date formatting with strftime:
https://www.shortcutfoo.com/app/dojos/ruby-date-format-strftime/cheatsheet
Related
In my rails app, I have the following configuration for date formats:
config/initializers/time_formats.rb
Date::DATE_FORMATS[:concise] = "%d/%b/%Y"
Time::DATE_FORMATS[:concise] = "%d/%b/%Y %H:%M %Z"
In my view, if I do:
MODEL.created_at.to_formatted_s(:concise)
... it correctly picks-up the :concise format for the datetime date type, and renders it as follows:
13/Oct/2020 05:00 UTC
Note that the %b is the short Month name - Oct in this case. If I only want to show the date, I would assume that I'd simply use the to_date method to convert the datetime to a date first:
MODEL.created_at.to_date.to_formatted_s(:concise)
This does render a date, but it doesn't pick up the :concise date format properly. It renders:
13/10/2020
Note it's not rendering October as 10, instead of formatting as %b. I've tried adding a :default date type in time_formats.rb but that didn't fix it.
Why is this happening just for the Date type?
(I'm using ruby 2.5.8, Rails 6.)
Looking for the best way to convert a string like "01/16/2016" into a friendly date format that rails can handle.
I have a calendar in which users can select a from_date and a to_date.. based on those params, my search will then filter results that fit the time periods.
Unfortunately, rails cannot handle the current format its in. Not sure the best way to go about this. I could change the search form's javascript to display the date differently, but I feel this format is most user friendly.
thx!
You can use the standard ruby Date class:
some_date = Date.strptime('01/16/2016', '%m/%d/%Y')
some_date will be an instance of Date, which then you can handle in rails and reformat in any way you want using strftime.
As like taglia said you can use the strptime
Date.strptime('01/16/2016', '%m/%d/%Y')
You can change the calendar date format from dd/mm/yyyy to mm/dd/yyyy, then you can use
require 'date'
date = DateTime.parse("16/01/2016")
=> #<DateTime: 2016-01-16T00:00:00+00:00 ((2457404j,0s,0n),+0s,2299161j)>
date.strftime('%a %b %d %H:%M:%S %Z %Y')
=> "Sat Jan 16 00:00:00 +00:00 2016"
You can use Date instead of DateTime if you want only the date.
I'm seeing some odd behavior in our application coming from a user working in what I believe to be Portuguese.
I realized that Rails seems to be mis-interpreting the dates she is submitting. Here's some examples:
Date.parse("ter, 30 abr 2013 07:00:00 GMT-07:00")
=> Thu, 30 May 2013
Date.parse("dom, 5 mai 2013 07:00:00 GMT-07:00 -07:00")
ArgumentError: invalid date
Is there something extra I need to do correctly identify dates being submitted in other languages?
Date are parsed with Ruby Date class with the following documentation
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-c-parse
I'm not sure it's a great idea to allow your users to type in the full date by themselves - they can mistype or assume different format. I suggest to use datepicker that with localized presentation based on the users location? One of the the many date pickers that allow this is http://docs.jquery.com/UI/Datepicker/Localization
I have a form input in my application that accepts dates in the format DD/MM/YYYY eg 02/05/2012 is 2nd May 2012.
What is the best way for me to convert this into a suitable format to be added to the database through ActiveRecord?
Is there a simply way of converting 02/05/2012 to 05/02/2012 before adding to the database?
This is similar to Getting rails to accept European date format (dd/mm/yyyy)
In your model, create a setter method, where "my_date" is your database field.
def my_date=(val)
Date.strptime(val, "%d/%m/%Y") if val.present?
end
For this specific format you can call DateTime.parse:
DateTime.parse("02/05/2012") # => Thu, 02 May 2012 00:00:00 +0000
2 Years later but it's something I came across too, here's my fix.
In your view, use:
<%= l article.created_at, format: :euro %> **make sure to add the l
And then in /config/locales/en.yml add:
en:
time:
formats:
euro: "%d/%m/%Y"
** you can also just add, euro: "%D" to display in the us date format.
Thanks.
I have a birthday field in my database which has a date value as yyyy-mm-dd. I want to display it as being more human friendly. So 2011-11-15 should show up as Tuesday, November 15th, 2011.
The very strange thing is that when i do a #user.birthday in rails console, the value does show up in the format I want. But not on the web.
I am not sure what's going on.
The reason it does not show up in the right format is that Rails in the view does call a to_s if necessary.
You should the Internationalization and Localization of Rails to do that.
So in your example, I18n.l #user.birthday should do the trick. You should check what the default date format for your locale is, this is located at `config/locales/.yml. You may add your format by following the explanation in "How to store custom translations". So by adding
en:
date:
formats:
default: "%A, %B %d,%Y"
this format will be used where ever you call I18n.l on a date.
#user.birthday.strftime "%A, %B %d,%Y"
Try this <model>.created_at.to_date.to_s(:long)