Getting rails to accept European date format (dd/mm/yyyy)
It may be just a case of the due_at
value being nil. In your case it's an empty string, but ignored because of the :reject_if
option on accepts_nested_attributes_for
, and so it remains as nil.
>> Date.strptime(nil, "%d/%m/%Y")
TypeError: can't dup NilClass
Take care of it with some conditional then.
self.due_at = Date.strptime(self.due_at,"%d/%m/%Y").to_time unless self.due_at.nil?
Default Date Format in Rails (Need it to be ddmmyyyy)
In your settings file: config/environment.rb"
my_date_formats = { :default => '%d/%m/%Y' }
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(my_date_formats)
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(my_date_formats)
source:
http://thedevelopercorner.blogspot.com/2009/03/change-default-date-format-in-ruby-on.html
How to change date format from mm/dd/yyyy to dd/mm/yyyy Ruby on Rails
Two steps:
- You need to convert your string into
Date
object. For that, useDate#strptime
. - You can use
Date#strftime
to convert theDate
object into preferred format.
See implementation below:
str = '01/14/2018'
date = Date.strptime(str, '%m/%d/%Y')
=> #<Date: 2018-01-14 ((2458133j,0s,0n),+0s,2299161j)>
date.strftime('%d-%m-%Y')
=> "14-01-2018"
date.strftime('%Y-%m-%d')
=> "2018-01-14"
Displaying the date pattern string in Rails i18n localization
That won't work because that's not how the format is specified. For English, this is how the date formats are specified:
formats:
default: "%Y-%m-%d"
long: "%B %d, %Y"
short: "%b %d"
Here are the docs for these percent placeholders in case you're curious.
To solve your problem, I'd create a date, localize it, and replace the parts:
date = Date.new(2000, 12, 31)
I18n.l(date).sub('2000', 'yyyy').sub('12', 'mm').sub('31', 'dd')
# => "yyyy-mm-dd"
Note that this might not work if the locale uses a 2 digit year format. Let's try it for some locales (using the default from rails-i18n):
def get_date_string(locale = I18n.current)
date = Date.new(2000, 12, 31)
I18n.l(date, locale: locale)
.sub('2000', 'yyyy')
.sub('12', 'mm')
.sub('31', 'dd')
end
formats = %i[en en-US en-GB es de fr pt].map do |locale|
[locale, get_date_string(locale)]
end.to_h
formats
will be:
{
:en=>"yyyy-mm-dd",
:"en-US"=>"mm-dd-yyyy",
:"en-GB"=>"dd-mm-yyyy",
:es=>"dd/mm/yyyy",
:de=>"dd.mm.yyyy",
:fr=>"dd/mm/yyyy",
:pt=>"dd/mm/yyyy"
}
How do I format a date to mm/dd/yyyy in Ruby?
The strftime
method can be used to format times:
Time.now.strftime("%m/%d/%Y")
Rails convert date format
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
Formatting Date/Time in Ruby to YYYY-MM-DD HH:MM:SS
You can use the Time#strftime
method to format the time into a variety of different formats, including the one which you need. It takes a parameter which specifies the format of the date output. Look at the documentation for this method for more instructions about how to use it.
Time.now.strftime("%F %T")
The %F
specifies that we would like the date in YYYY-MM-DD
format. This is followed by a space, then the %T
specifier, which produces a HH:MM:SS
time output.
To incorporate this into your code:
"erstellzeit": "#{Time.now.strftime("%F %T")}"
Alternatively, if you're getting an additional +0000
output, try:
"erstellzeit": "#{Time.now.strftime("%F %H:%M:%S")}"
Related Topics
Why Does Ruby '**' Operator Have Higher Precedence Than Unary '-'
Error: Null Value in Column "Id" Violates Not-Null Constraint
How to Call a JavaScript Function from an HTML.Erb
Pass Arguments by Reference to a Block with the Splat Operator
Obfuscating Rails App on Mri Ruby/Jruby for a Enterprise Customer
Secure Erasing of Password from Memory in Ruby
Determining Method's Visibility on the Fly
How to Refer to a Submodule's "Full Path" in Ruby
Getting Hash with Symbol as Keys for Mongo in Rails
Ruby Regexp to Replace Equations
How to Collapse Double Splat Arguments into Nothing
Devise Rendering Default Views from Gem Instead of Generated Ones
Uri::Invalidurierror: Bad Uri(Is Not Uri) Testing Rails Controllers