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

  • Home
  • SEARCH
  • 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 7766751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:30:04+00:00 2026-06-01T15:30:04+00:00

I want date part from an instance of ActiveSupport::TimeWithZone . I used to_date function

  • 0

I want date part from an instance of ActiveSupport::TimeWithZone. I used to_date function for that but it returns date one day earlier.

For example, if datetime is 2012-04-11 09:05:00 UTC, and if I call to_date then it returns 2012-04-10 but 2012-04-11.

Also I am not using specific timezone (defaults to UTC)

The application is running on Ruby 1.8.7 and Rails 3.0.2

Can anyone please tell me why it gives wrong date? Also please suggest me if there is better way to get date (instance of Date) part from given DateTime (instance of ActiveSupport::TimeWithZone)

EDIT :
someone is also facing same issue here http://pastebin.com/sn8ExZiQ

NOTE : Time.zone returns 'UTC'

  • 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-01T15:30:05+00:00Added an answer on June 1, 2026 at 3:30 pm

    Thanks Jignesh. Here I am sharing your findings and solutions.

    There may be some gem that is overriding to_date implementation and it may be implemented incorrectly and this overridden version might be getting called.

    In my case the culprit was ruby-units gem

    Root Cause : ruby-units gem included in the application’s Gemfile

    Problem Analysis :

    Gemfile

    # Ruby-units overrides String class #to method, hence placed before Rails
    gem "ruby-units" # Loads first and then rails is loaded
    
    gem "rails", "3.0.11"
    
    ..
    ..
    

    time.rb file (ruby-units gem codebase)

    ..
    ..
    
    unless Time.instance_methods.include?(:to_date)
        # :nocov_19:
        # @return [Date]
        def to_date
         x=(Date.civil(1970,1,1)+((self.to_f+self.gmt_offset)/86400.0)-0.5)
         Date.civil(x.year, x.month, x.day)
        end
        # :nocov_19:
    end
    
    ..
    ..
    

    Lets say that current time in UTC time-zone is
    Wed, 11 Apr 2012 10:12:17 UTC +00:00 represented by a ActiveSupport::TimeWithZone
    instance.
    From the rails application when we execute <TimeWithZone>.to_date it returns
    a date 2012-04-10 which is incorrect.

    The culprit involved in above incorrect behavior is the implementation of to_date method
    provided by ruby-units gem

    Below is a sample program to demonstrate the above incorrect behaviour. The to_date method is identical to one implemented by ruby-units gem, except that an argument is added to the method namely date_time and the self in the implementation is replaced by the argument
    ‘date_time’.

    Sample Ruby Program to confirm the findings above:

    require 'rubygems'
    require 'active_support/all'
    
    class TestDT
     def to_date(date_time)
         #x=(Date.civil(1970,1,1)+((self.to_f+self.gmt_offset)/86400.0)-0.5)
         x=(Date.civil(1970,1,1)+((date_time.to_f+date_time.gmt_offset)/86400.0)-0.5)
         Date.civil(x.year, x.month, x.day)
     end
    end
    
    tdt = TestDT.new
    utc_time = Time.now.in_time_zone('UTC')
    puts tdt.to_date(utc_time)
    

    Output (date at the time of writing this was Wed, 11 Apr 2012 08:35:12 UTC +00:00):

    $ ruby test_date_time.rb
    2012-04-10
    

    Solution:

    1. Remove the ruby-units gem from the Gemfile or load it after rails gem
    2. Workaround: Instead of executing datetime.to_date , use datetime.to_s.to_date
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SELECT GETDATE() Returns: 2008-09-22 15:24:13.790 I want that date part without the time part:
I want to implement a thread-safe function to remove the time part from java.util.Date.
I want to extract just the date part from a timestamp in PostgreSQL. I
I want to switch between different projects, and one part of that is changing
I want the date to be sn at my page, I used this fn
I want to extract data from database where field end_ date is less than
Is it possible to fetch only the month part from a sql date entry?
I want to retrieve a date field value from a List and store it
I am trying to truncate the seconds part from the current Date Dim nowTime
Basically I want to know I a user chooses from my settings what day

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.