Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6837195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:26:52+00:00 2026-05-26T23:26:52+00:00

I’m having trouble working with the datetime format. Current situation: I’m having a database

  • 0

I’m having trouble working with the datetime format. Current situation:
I’m having a database with a few fields with datetime in it. The format is like 2011-11-16 15:07:00 UTC. I woud like to change this format throughout my application. I don’t care if it is saved this way in the database, but I want it a little bit more customer-friendly in the corresponding views (I’m from Germany, so that would be 16.11.2011 15:07).
I tried to use I18n, which worked at least for a few parts. I created a locale (and added default_locale to application.rb). Is there a way that rails uses this locale without altering the corresponding views? I couldn’t find it, so I (succesfully) tried the following:

For my index-view I’ve worked with a little function in the
application_helper.rb:

    module ApplicationHelper  
       def nice_date_form(the_date)
          if !the_date.nil? 
             I18n.l the_date, :format => :short
          end
       end
    end

Problems arose when I tried to use that approach with the edit/new views (_form.html.erb), it just didn’t work at all. After a while I came across a RailsCast about the topic virtual attributes, which at least temporary solved my problem. I can use a getter and setter methode in the model, but for different tables and fields that would be a whole lot of repetition.

_form.html.erb:

    (...)
    <div class="field">
    <%= f.label :abfahrt %><br />
    <%= f.text_field :nice_date_edit %> 
    </div>
    (...)

Long story short: I searched a long while now and don’t know where to continue. I guess that this can’t be the best approach to solve this issue? Is there a way to simplify that and set kind of a rails-wide format for datetime?

A few hints would be greatly appreciated! Thanks in advance.

Christian

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:26:53+00:00Added an answer on May 26, 2026 at 11:26 pm

    You can use strftime() for formatting datetime in the view label(customer-friendly in the corresponding views).

    Your helper method could be

    module ApplicationHelper  
       def nice_date_form(the_date)
          if !the_date.nil? 
             the_date.strftime(“%d.%m.%y %H:%M”)
          end
       end
    end
    

    For details take a look at Rails Datetime Format

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.