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 7807779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:57:35+00:00 2026-06-02T02:57:35+00:00

We have an application where user sets his/her preferred date/time format. User is expected

  • 0

We have an application where user sets his/her preferred date/time format. User is expected to enter datetime in configured format across the application. Now the problem is for few formats the datetime is parsed wrongly while create/update ActiveRecord.

For example user has set date/time format in hh:mm dd/MM/yyyy. Now if user enters 17:00 04/05/2012 it parses it as 5 PM 5 Apr, 2012 where it should be 5 PM 4 May, 2012

1.8.7 :004 > a = Article.create!(:name => 'a1', :published_at => '17:00 04/05/2012')
 => #<Article id: 2, name: "a1", published_at: "2012-04-05 17:00:00", created_at: "2012-04-16 13:54:36", updated_at: "2012-04-16 13:54:36"> 

I understand that ruby/rails has predefined set of formats against which the date/time string is evaluated and if matches any format in sequence it parses given string with that format.

Another example

1.8.7 :006 > a = Article.create!(:name => 'a1', :published_at => '17:00 15/12/2012')
 => #<Article id: 4, name: "a1", published_at: nil, created_at: "2012-04-16 13:55:43", updated_at: "2012-04-16 13:55:43"> 

Here user entered 15 December, 2012 which was parsed to nil

Someone has already asked similar question here and on ruby-forum but the solution is not applicable in my case as I cannot configure a single datetime format across the application as each user will have his own preferred datetime format.

Now to work with multiple datetime format, I am planning to manipulate it before the params is passed to the ActiveRecord. So the steps would like

  1. Read datetime from params map
  2. Parse it with user’s preferred datetime format
  3. Format datetime to the default format used by ActiveRecord
  4. Replace current datetime in params with re-formatted datetime

Here is a snippet of the controller

class ArticlesController < ApplicationController
  before_filter :format_date_time, :only => [:create, :update]
  ...
  ...
  private
    def format_date_time
      datetime = params[:datetime]
      params[:datetime] = DateTime.strptime(datetime, pref_date_time_format).strftime('%c')
    end
end

Would anyone please verify if it is right approach or not? I would be appreciable if someone suggests better ways to accomplish this.

Thanks,
Amit Patel

  • 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-06-02T02:57:37+00:00Added an answer on June 2, 2026 at 2:57 am

    I would go with the approach I mentioned. If anyone has better way please keep sharing.

    Thanks, Amit Patel

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

Sidebar

Related Questions

I currently have a application which will display the time allocated to a user
I have an application that sets the user's background. Is it possible to remove
I have an application where user can download documents. User has option to download
I have an application where the user selects the dates of a first statement
We have a current application where user login credentials are stored in a SQL
I have an application where the user can write up some questions and add
I have a application whereby a user can destroy other users attached to the
I have a rails application with User and Machine data models, as well as
We have a customer that would like to modify application user messages that we
Is it good idea to have application engine written in c++ and user interface

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.