Convert string to datetime ruby on rails
You can use DateTime to parse the date from a specific format.
if the format you are looking to parse is "03/28/2018 1:46 AM" then you can do this.
date = DateTime.strptime('03/28/2018 1:46 AM', '%m/%d/%Y %I:%M %p')
# date to ISO 8601
puts date.to_time
# output: 2018-03-28 07:16:00 +0530
puts date.strftime("%m/%d/%Y")
# output: 03/28/2018
Date formats:
Date (Year, Month, Day):
%Y - Year with century (can be negative, 4 digits at least)
-0001, 0000, 1995, 2009, 14292, etc.
%m - Month of the year, zero-padded (01..12)
%_m blank-padded ( 1..12)
%-m no-padded (1..12)
%d - Day of the month, zero-padded (01..31)
%-d no-padded (1..31)
Time (Hour, Minute, Second, Subsecond):
%H - Hour of the day, 24-hour clock, zero-padded (00..23)
%k - Hour of the day, 24-hour clock, blank-padded ( 0..23)
%I - Hour of the day, 12-hour clock, zero-padded (01..12)
%l - Hour of the day, 12-hour clock, blank-padded ( 1..12)
%P - Meridian indicator, lowercase (``am'' or ``pm'')
%p - Meridian indicator, uppercase (``AM'' or ``PM'')
%M - Minute of the hour (00..59)
You can refer to all formats here.
Convert String to DateTime Ruby
require 'date'
▶ Date.parse "2015-11-01T10:00:00.00+08:00"
#⇒ #<Date: 2015-11-01 ((2457328j,0s,0n),+0s,2299161j)>
▶ DateTime.parse "2015-11-01T10:00:00.00+08:00"
#⇒ #<DateTime: 2015-11-01T10:00:00+08:00 ((2457328j,7200s,0n),+28800s,2299161j)>
or, even better:
▶ DateTime.iso8601 "2015-11-01T10:00:00.00+08:00"
#⇒ #<DateTime: 2015-11-01T10:00:00+08:00 ((2457328j,7200s,0n),+28800s,2299161j)>
convert String to DateTime
DateTime.strptime allows you to specify the format and convert a String to a DateTime.
Convert string to date time in Ruby
Here you go:
DateTime.strptime("03/30/2021 4:30 PM", "%m/%d/%Y %H:%M %p") # => Tue, 30 Mar 2021 16:30:00 +0000
And here is the strptime
method, https://ruby-doc.org/stdlib/libdoc/date/rdoc/DateTime.html
Convert string to datetime in specific timezone
You can use the in_time_zone method. For example:
DateTime.current.in_time_zone("Alaska")
# => Fri, 23 May 2014 07:21:30 AKDT -08:00
So for your use case:
params[:notify_at].to_datetime.in_time_zone(user.time_zone)
Pro Tip: If using Rails v4+ you can actually do this directly on the string:
"2014-07-05 14:30:00".in_time_zone("Alaska")
# => Sat, 05 Jul 2014 14:30:00 AKDT -08:00
UPDATE
You can parse a string directly into a time zone (where the String should already be IN that time zone) like this:
Time.zone.parse("2014-07-05 14:30:00")
# => Sat, 05 Jul 2014 14:30:00 CEST +02:00
So for your use case do:
user.time_zone.parse(params[:notify_at])
convert string to specific datetime format?
require 'date'
date = DateTime.parse("2011-05-19 10:30:14")
formatted_date = date.strftime('%a %b %d %H:%M:%S %Z %Y')
See strftime() for more information about formatting dates.
Rails convert datetimepicker date string to datetime data type
Don't you actually want a date? not a datetime?
In any case you can use
Date.strptime("12/13/2012", "%m/%d/%Y")
=> #<Date: 2012-12-13 ((2456275j,0s,0n),+0s,2299161j)>
DateTime.strptime("12/13/2012", "%m/%d/%Y")
=> #<DateTime: 2012-12-13T00:00:00+00:00 ((2456275j,0s,0n),+0s,2299161j)>
DateTime not working to convert string to date in rails
The issue is you are asking for seconds in your strptime
call, but your time doesn't have seconds. Try this instead:
a = "03/17/2019 8:30 AM"
DateTime.strptime(a, "%m/%d/%Y %I:%M %P")
Or add seconds to your time.
Related Topics
Accessing a Ruby Hash with a Variable as the Key
Gmaps4Rails:Setting Map Width and Height
Calling Ruby Method Without Instantiating Class
Determining Method's Visibility on the Fly
Defined' and 'Unless' Not Working as Expected
Why Can't We Override '||' and '&&'
How to Override the System Timezone in Ruby
Expressing Conditional Haml Possibly with Ternary Operator
Validates Presense VS Null False in Rails Models/Tables
Execute Code Once Sinatra Server Is Running
Ruby on Rails Workflow Engine (Like Ibm Workflow)
Testing HTML5 File Upload with Capybara/Selenium Webdriver - Ruby
How to Use Ruby Date Constants Gregorian, Julian, England and Even Italy