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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:28:37+00:00 2026-06-06T15:28:37+00:00

My goal is to get a Time instance from a DateTime instance This has

  • 0

My goal is to get a Time instance from a DateTime instance

This has been previously discussed here and I am still confused : Convert to/from DateTime and Time in Ruby

For me in irb running ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0], things work perfectly

1.9.3p0 :001 > require 'Date'
 => true 
1.9.3p0 :002 > require 'Time'
 => true 
1.9.3p0 :003 > t=DateTime.now
 => #<DateTime: 2012-07-01T01:57:32+05:30 ((2456109j,73652s,621060000n),+19800s,2299161j)> 
1.9.3p0 :004 > t2=t.to_time
 => 2012-07-01 01:57:32 +0530 
1.9.3p0 :005 > t.class
 => DateTime 
1.9.3p0 :006 > t2.class
 => Time 
1.9.3p0 :007 > 

However when working with rails console v 3.2.3 on same ruby platform

1.9.3p0 :001 > t=DateTime.now
 => Sun, 01 Jul 2012 02:00:00 +0530 
1.9.3p0 :002 > t.class
 => DateTime 
1.9.3p0 :003 > t2=t.to_time
 => Sun, 01 Jul 2012 02:00:00 +0530
1.9.3p0  > t2.class
 => DateTime

What to do to get a Time instance from DateTime in rails?

  • 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-06T15:28:38+00:00Added an answer on June 6, 2026 at 3:28 pm

    According to the Rails documentation for DateTime.to_time(),

    If self has an offset other than 0, self will just be returned unaltered

    To change to a 0 offset, use DateTime.utc().

    1.9.3p194 :005 > t3=t2.utc
     => Sat, 30 Jun 2012 20:43:21 +0000 
    1.9.3p194 :006 > t3.offset
     => (0/1) 
    1.9.3p194 :007 > t4=t3.to_time
     => 2012-06-30 20:43:21 UTC 
    1.9.3p194 :008 > t4.class
     => Time 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is Kernel.Get() threadsafe? My goal is share an instance of my kernel among all
I've been a web developer for quite some time and what has helped me
my goal is to get lots of rows from a translation table. I use
My goal is to get a weighted average from one table, based on another
This is a followup to Clojure: Compile time insertion of pre/post functions My goal
Goal : Get a screenshot of a map with overlays(MKOverlayView) as well as the
My goal is to get various SEO-relevant information for various sites. Basically all the
How do i do that? Actually my main goal is to get which checkbox
Goal My goal is to to get Twitter Bootstrap to worth with my Play
The goal is to let users login via Twitter, get OAuth token and secret

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.