Format date when using :as=>:string

4,485 views
Skip to first unread message

sopwithpup

unread,
Feb 20, 2012, 11:29:26 AM2/20/12
to plataformate...@googlegroups.com
I've got three date fields for which I'm using :as=>:string and jquery datepicker. This works fine to input a date but when I edit the date I'm getting the DB's default formatting displayed  i.e. yyyy-mm-dd when I want dd/mm/yyyy.

Is there a way of doing this easily, or do I have to create three lots of custom getters/setters as per this article?


Can I not be DRY-er that this>

Carlos Antonio da Silva

unread,
Feb 21, 2012, 7:47:50 AM2/21/12
to plataformate...@googlegroups.com
Just give the formatted value to the input:

    f.input :my_date, :as => :string, :input_html => { :value => localize(f.object.my_date) }

localize comes from I18n and will lookup the format in your i18n file (which I suppose you already have changed for dd/mm/yyyy, right?).

You can also give the format if you want:

    localize(f.object.my_date, :format => "%d/%m/%Y)

-- 
At.
Carlos Antonio

sopwithpup

unread,
Feb 21, 2012, 9:28:12 AM2/21/12
to plataformate...@googlegroups.com
Fantastic, that did the trick.  Hoped it would be simple!

Thanks very much, love the library

James Hughes

unread,
Mar 9, 2012, 3:27:02 PM3/9/12
to plataformate...@googlegroups.com
    f.input :my_date, :as => :string, :input_html => { :value => localize(f.object.my_date) }

I'm just curious, is there a way to encapsulate this in a custom input? I did some searching around the gem, looking at StringInput and such, but it wasn't clear to me. For example, I'd like to have:

 f.input :my_date, :as => :date_picker

with the corresponding app/inputs/date_picker_input.rb:
class DatePickerInput < SimpleForm::Inputs::StringInput

  def input
  # do something to inject :input_html => { :value => localize(f.object.my_date) }
  end

end


Carlos Antonio da Silva

unread,
Mar 9, 2012, 3:45:41 PM3/9/12
to plataformate...@googlegroups.com
Yeah, you can have something similar to that quite easily :).. There are some examples on the wiki on how to build custom inputs, that should help you.

-- 
At.
Carlos Antonio

Katherina Fatiguso

unread,
Aug 27, 2015, 9:00:12 AM8/27/15
to SimpleForm
Hello Carlos,
I hope you can help me, please regarding how to set the local timezone (mine is AEST) Australian Eastern Standard Time, instead of getting the default UTC time.
I have a simple form that takes in the start time and it's by default in UTC time.
If I change the date and time and saved it, it will save it as UTC time.
But then in the show page, I displayed the time in local timezone and it is different with the UTC time (it's because AEST is 10 hours ahead of UTC).
How can I make the simple form to display the local time, instead of UTC time?

This is the portion of my simple form:
    <%= f.input :start_time, as: :string, input_html: { class: 'datetimepicker', value: localize(f.object.start_time, :format => "%d/%m/%Y  %I:%m %p %Z")} %>
    <%= f.input :end_time, as: :string, input_html: { class: 'datetimepicker', value: localize(f.object.end_time, :format => "%d/%m/%Y  %I:%m %p %Z")} %>

It will display in the form page:
8/08/2015  12:08 PM UTC

And this is my datetimepicker jquery in application.html.erb:

$('.datetimepicker').datetimepicker({
 format:'d M Y H:i'
});



How to make simpleform to display the local user's timezone on the form page?
Can you give some suggestions, please?

Thank you,
Katherina

Carlos Antonio da Silva

unread,
Aug 27, 2015, 9:18:13 AM8/27/15
to plataformate...@googlegroups.com
Hello Katherina,

I think the issue is that your application is talking UTC rather than AEST - if you want that to be your default timezone you have to configure the application to set the default to be other than UTC. Now if you want to use the user's timezone, you have to either ask this information for the user somewhere and store it in your database, so you can make all dates speak the user's timezone. Another option is to user Rails #in_time_zone` method available on date/time/datetime objects, so you could say "f.object.start_time.in_time_zone('AEST')" (or something like that).

Another option is to use javascript to convert that date for the user timezone based on the browser, but that's beyond the scope of Rails. You should be able to look at the datetimepicker library and see if it does not have any option to implement that.

I think this issue is out of the scope of Simple Form, though, it's more an application concern. I hope this helps to give you some direction, let us know if you need anything else.

--
You received this message because you are subscribed to the Google Groups "SimpleForm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

Katherina Fatiguso

unread,
Aug 27, 2015, 9:30:55 PM8/27/15
to SimpleForm
Thank you for your reply Carlos.
I just decided now to keep the time in UTC time and do not show the timezone to the audiences, just show the date and time only to avoid confusion.
The time zone stored in my database is in UTC timezone.
And this website will be used within Australia only anyway.
But I'd have a look at the option in Rails for .in_time_zone anyway. Thanks again.


On Thursday, August 27, 2015 at 11:18:13 PM UTC+10, Carlos Antonio da Silva wrote:
Hello Katherina,

I think the issue is that your application is talking UTC rather than AEST - if you want that to be your default timezone you have to configure the application to set the default to be other than UTC. Now if you want to use the user's timezone, you have to either ask this information for the user somewhere and store it in your database, so you can make all dates speak the user's timezone. Another option is to user Rails #in_time_zone` method available on date/time/datetime objects, so you could say "f.object.start_time.in_time_zone('AEST')" (or something like that).

Another option is to use javascript to convert that date for the user timezone based on the browser, but that's beyond the scope of Rails. You should be able to look at the datetimepicker library and see if it does not have any option to implement that.

I think this issue is out of the scope of Simple Form, though, it's more an application concern. I hope this helps to give you some direction, let us know if you need anything else.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simpleform+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

Carlos Antonio da Silva

unread,
Aug 28, 2015, 8:02:15 AM8/28/15
to plataformate...@googlegroups.com
Storing UTC is probably the best thing you can do. You can configure the application timezone so that Active Record automatically converts the UTC dates on the database to the timezone you specify - which is nice and easy to do.

in_time_zone allows you to force any date in a specific timezone, so if you're using a single tz to display all your dates across the app, maybe the configuration is what you need. Look for config.time_zone inside config/application.rb.

Hope that helps, good luck!

To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simp...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

--
You received this message because you are subscribed to the Google Groups "SimpleForm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simp...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio
Reply all
Reply to author
Forward
0 new messages