Rails convert string containing datetime to date - ruby-on-rails

I have a String which contains the following string 2012-03-06 00:00:00 UTC I want to change it into a date object so that is should look like this 03-06-2012 and the same is to be converted in String 03-06-2012 so that the jquery datepicker can take it.
All this conversion needs to be done at view .html.erb

Date.parse("2012-03-06 00:00:00 UTC").strftime("%d-%m-%Y")

You can do this
date = DateTime.now
puts date.to_date.to_s
which gives "2013-08-21"

date = Date.parse('2012-03-06 00:00:00 UTC')
# => Tue, 06 Mar 2012
date.strftime('%d-%m-%Y')
# => "06-03-2012"

Related

Convert Active Support timezone original format into a string

I'm trying to convert Active Support timezone original format into a string. I want to store it in an array of characters then parse each needed data individually.
Time.zone = current_user.timezone
date_and_time = Time.zone.now
Now
date_and_time = Thu, 21 Apr 2016 20:58:04 PDT -07:00
Ruby method ( to_s ) does not convert it. I found other ways to convert it to but all of them will change the format to numbers only, I want the day to stay the same because I will store it in a variable then use it in a different method.
You can use .to_formatted_s(DATE_FORMAT) for this.
time = Time.now # => Thu Jan 18 06:10:17 CST 2007
time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
time.to_formatted_s(:long) # => "January 18, 2007 06:10"
time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
time.to_formatted_s(:iso8601) # => "2007-01-18T06:10:17-06:00"
A list of all DATE_FORMATS and more information can be found here:
http://api.rubyonrails.org/classes/Time.html#method-i-to_formatted_s
You can try this
date_and_time.strftime("%a %d %b %Y")
Also You can check this guide, to get format you want
You should get what you want using this :
date_and_time.strftime("%a %d %b %Y %H:%M:%S UTC %:z")
Please see strftime Docs for more info
Explanation
Reason for hardcoding UTC is so that according to the docs
%z - Time zone as hour and minute offset from UTC
So i believe it should be UTC all the time.

Rails Convert UNIX timestamp to SQL DateTime

I have timestamp as "1432927800000"
I want to save it in DateTime column of MySQL. (SQL DateTime column stores date in this format : 2010-06-24 11:30:00)
I have tried
uTime = params[:fromDate] #contains "1432927800000"
Date.new(uTime).to_formatted_s(:db) #Comparison of String with 0 failed.
DateTime.strptime("1318996912",'%s') #Give weird date: Wed, 03 Sep 47377 12:00:00 +0000
Date.parse(uTime).to_s(:db) #Invalid Date
I am expecting 2015-5-30 1:00:00 from "1432927800000"
Instead of strftime will be better to use predefined :db pattern from ActiveSupport:
2.1.0 :002 > Time.at(1432927800000/1000).to_s(:db)
=> "2015-05-29 22:30:00"
to_s is alias for to_formatted_s method and defined in Time and DateTime classes.
Try this I Hope this will help you
I have tried it in my Rails console and I got following.
uTime = 1432927800000
Time.at(uTime/1000).strftime("%Y-%m-%d %I:%M:%S")
## Output
"2015-05-30 01:00:00"
This a link where you which will helps you to convert unix timestamp
http://www.epochconverter.com/

Rails - A better way to parse the time with a timezone?

Time.parse returns a Time object that does not have a timezone. I would like to keep the timezone information. Is there a better way to do this then the following code?
def parse_with_timezone( string_input)
/(.*)([+-]\d\d):?(\d\d)$/.match( string_input) do |match|
tz = ActiveSupport::TimeZone[match[2].to_i.hours + match[3].to_i.minutes]
tz.parse( match[1])
end
end
The input is a string like this "2012-12-25T00:00:00+09:00". This function outputs a TimeWithZone object.
Were you looking for a specific timezone of the current local one?
# Current zone
1.9.3p194> Time.zone.parse('2012-12-25T00:00:00+09:00')
=> Mon, 24 Dec 2012 15:00:00 UTC +00:00
Console was set at UTC for above but will work for whatever you have configured
# Specific timezone
1.9.3p194> Time.find_zone('Wellington').parse('2012-12-25T00:00:00+09:00')
=> Tue, 25 Dec 2012 04:00:00 NZDT +13:00
I notice you're trying to pass +9 so as an example
1.9.3p194> Time.zone = 'Tokyo'
=> "Tokyo"
1.9.3p194> Time.zone.parse('2012-12-25T00:00:00+09:00')
=> Tue, 25 Dec 2012 00:00:00 JST +09:00
Gives you the right result.
What about the Rails Timezone API: http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html
I prefer to use Chronic for all my date/time parsing needs.

Given a DateTime, how do I get the range of times with the same date in a particular time zone?

I have a DateTime object representing a particular date, as in "2011-01-15 00:00:00 UTC" to represent January 15th. I would like to produce the range of times in a particular time zone that have the same date.
The method signature would probably be something like
def day_range_for(date, tz)
# ...
end
For example, if I have range_for(DateTime.parse('2011-01-15'), 'CST'), then I want the result to be a range like 2011-01-15 00:00:00 -0600 .. 2011-01-15 23:59:59 -0600.
Can you take the input string instead of the object? If you pass in a DateTime object, its a tad counter intuitive because the time the object represents isn't the actual time you are looking for. It would conform to my expectations if you either passed in the correct, absolute DateTime, or the string itself. The actual meat of the problem is entirely handled by ActiveSupport, which I think you are using since you tagged this question with Rails. How does this look?
def range_for(input, tz=nil)
if tz.is_a?(String)
tz = ActiveSupport::TimeZone.new(tz)
else
tz = Time.zone
end
if input.acts_like?(:date) || input.acts_like?(:time)
d = input.in_time_zone(tz)
else
d = tz.parse(input)
end
return d.beginning_of_day..d.end_of_day
end
Have a look:
ruby-1.9.2-p0 > range_for('2011-01-15', 'Alaska')
=> Sat, 15 Jan 2011 00:00:00 AKST -09:00..Sat, 15 Jan 2011 23:59:59 AKST -09:00
ruby-1.9.2-p0 > range_for(Time.zone.now)
=> Mon, 10 Jan 2011 00:00:00 EST -05:00..Mon, 10 Jan 2011 23:59:59 EST -05:00
ruby-1.9.2-p0 > range_for('2011-01-15', 'EST')
=> Sat, 15 Jan 2011 00:00:00 EST -05:00..Sat, 15 Jan 2011 23:59:59 EST -05:00
How about:
def day_range_for date, zone
(DateTime.new(date.year,date.month,date.day,0,0,0,zone)..DateTime.new(date.year,date.month,date.day,23,59,59,zone))
end
day_range_for(DateTime.parse('2011-01-15'), 'CST')
#=> #<DateTime: 2011-01-15T00:00:00-06:00 (9822307/4,-1/4,2299161)>..#<DateTime: 2011-01-15T23:59:59-06:00 (212161917599/86400,-1/4,2299161)>

Ruby on Rails: how do I convert this date

Wed Sep 22 13:15:02 -0400 2010 to this format 2010-08-23 13:15:02 -0400
The left is Time.now
The right is 30.days.ago =\
You can use the to_s(:db) method in Time class to convert it to a database-friendly format.
Time.now.to_s(:db) # => "2010-09-22 17:50:41"
If you really need the time zone offset info, you could add a custom format to Time::DATE_FORMATS, e.g.
Time::DATE_FORMATS[:db_with_zone_offset] = lambda { |time|
time.strftime("%Y-%m-%d %H:%M:%S #{time.formatted_offset(false)}")
}
after which you can simply call
Time.now.to_s(:db_with_zone_offset) => # "2010-09-22 17:48:21 +0000"
Both are different data types.
>> Time.now.class
=> Time
>> 30.days.ago.class
=> ActiveSupport::TimeWithZone
use the strftime method to format it.
If you want to have format in database format, then you can use:
Time.now
=> Wed Sep 22 19:54:24 +0200 2010
Time.now.to_s(:db)
=> "2010-09-22 19:54:48"
Time.now.utc.to_s(:db)
=> "2010-09-22 17:55:16"

Resources