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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:37:26+00:00 2026-06-05T04:37:26+00:00

I am using a js calendar that inserts a string into a text field

  • 0

I am using a js calendar that inserts a string into a text field for submission as a datetime value. Not sure if it matters but I am using SQLite for now.

I am setting the format as follows (in an initializer):

Time::DATE_FORMATS[:default] = '%m/%d/%Y %H:%M'
Time::DATE_FORMATS[:db] = '%m/%d/%Y %H:%M'

This isn’t working because Rails is apparently validating the date using a different format.

For example, if I set it as follows:

myobject.my_datetime = '06/30/2012 00:00'

it ends up null (the SQL query itself generated by ActiveRecord sets it to NULL). If I set it to a day less than or equal to 12 the date part ends up correct (but the generated SQL query still shows the string with the day before the month, i.e. in the above example the SQL query has the string as ’30/06/2012 05:00:000000′).

As you can see it also seems to be adding 5 hours for UTC(?) even though I have config.time_zone set to eastern time.

Behavior is the same either from console or via form submission.

Any suggestions on what I am missing?

UPDATE:

When I follow jdoe’s suggestion and do this:

myobject.my_datetime = Time.strptime(’06/30/2012 08:00′, ‘%m/%d/%Y %H:%M’)

The generated query now looks correct (ignoring the 4 hour difference):

UPDATE "myjoin_table" SET "my_datetime" = '06/30/2012 04:00.000000' .....

and it winds up in the database that way.

But when I reload the object the value in the AR object is initialized to nil.

I guess I’ll just parse the string piece by piece in my controller action and set the parameters to update_attributes manually. This is harder than it should be.

  • 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-05T04:37:28+00:00Added an answer on June 5, 2026 at 4:37 am

    ActiveRecord stores date/time in UTC+00:00. If you’re living in the +5 zone, ActiveRecord will subtract 5 hours on assigning automatically. So, your current time zone just says how many hours has to be added/subtracted from your local time.

    About parsing. Did you try the following?

    myobject.my_datetime = Time.strptime('06/30/2012 00:00', '%m/%d/%Y %H:%M')
    

    Rails time/date settings are made for showing, not for assignment. It’s not a brilliant idea to get strings from a user and assign them to your date/time attributes. You’d better use proper time objects in assignment instead of stings. The strptime method will raise the ArgumentError if some user is trying to be a hacker. Your record.date_time='string' fails silently.


    UPD: Usage Example

    p = Product.first
    p.last_visited = Time.strptime('06/30/2012 00:00', '%m/%d/%Y %H:%M')
    p.save # leads to UPDATE "products" SET "last_visited" = '2012-06-29 21:00:00.000000'
    Product.last_visited.localtime.strftime('%m/%d/%Y %H:%M') # => "06/30/2012 00:00"
    

    As you see, time is stored and retrieved w/o any problems.

    UPD 2: I18n

    Locate your config/locales/en.yml. Add the following to it:

    en:
      time:
        formats:
          default: '%m/%d/%Y %H:%M'
    

    To parse your time use t(or translate) helper:

    @time = Time.strptime('06/30/2012 00:00', t('time.formats.default'))
    

    Use l(or localize) helper to show time in your format:

    = l @time
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wanting to insert a datetime into a MySql data base using Java
I am using the WPF Calendar that is part of the WPF Toolkit. I
There are a few different 3rd party code frameworks that include calendar controls, but
I'm working on an app that has a reminder component. I'm using Calendar Store
I've noticed that google calendar doesn't allow me to put html into the body
I'm developing an Android APP that connects to Google Calendar using GData API for
I have a calendar that uses jQuery and jmpopups to pull Event Details into
I'm using a script calendar that when I choose a date, it convert it
I'm using Keith Wood's jQuery datepicker http://keith-wood.name/datepick.html to make a calendar that the user
I'm using: Calendar c = Calendar.getInstance(); with which i get a current time, String

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.