Converting a string into a date Ruby on Rails - ruby-on-rails

I'm attempting to convert a string date into a date that can be stored in the database. These are my attempts:
params[:event][:start_date] = '03-21-2016'
DateTime.strptime(params[:event][:start_date], '%Y-%m-%dT%H:%M:%S%z')
or
DateTime.strptime(params[:event][:start_date], '%m-%d-%Y')
However I keep getting an invalid date error. I'm not sure what I'm doing wrong.

One way to do it would be this:
date_string = '03-21-2016'
month, day, year = date_string.split('-').map(&:to_i)
DateTime.new(year, month, day)

You need to understand what each fragment (eg %Y) in the format string ('%Y-%m-%dT%H:%M:%S%z') means: read this.
http://apidock.com/rails/ActiveSupport/TimeWithZone/strftime
Once you know this, you can tailor a format string to the date string you have, or expect to get: in this case, "%m-%d-%Y".

When debugging create a new, basic and simple, version of the code and test that.
require 'date'
params = '03-21-2016'
DateTime.strptime(params, '%m-%d-%Y') # => #<DateTime: 2016-03-21T00:00:00+00:00 ((2457469j,0s,0n),+0s,2299161j)>
Note the order for the format: '%m-%d-%Y', which works. That's the problem with your first attempt, where you tried to use%Y-%m-%d. There is NO month21or day2016`.
Your second attempt is valid but your question makes it appear it doesn't work. You need to be more careful with your testing:
params = {event:{start_date:'03-21-2016'}}
DateTime.strptime(params[:event][:start_date], '%m-%d-%Y') # => #<DateTime: 2016-03-21T00:00:00+00:00 ((2457469j,0s,0n),+0s,2299161j)>

Related

Using group_by_hour_of_day: rails using groupdate gem

I wanted to group the total checkin in a day using the groupdate gem with this code.
from = Time.zone.now.beginning_of_day
to = Time.zone.now.end_of_day
customer_checkins = CustomerCheckin.where(created_at: from..to, account_id: 139)
hours = customer_checkins.group_by_hour_of_day(:created_at).count
The result is weird because all counts are tagged into hour 0 instead of being tagged to its specific hour of day. The result is shown below.
{0=>677, 1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>0, 7=>0, 8=>0,
9=>4, 10=>0, 11=>0, 12=>0, 13=>0, 14=>0, 15=>0, 16=>0, 17=>0,
18=>0, 19=>0, 20=>0, 21=>0, 22=>0, 23=>0}
What could be the problem with this one?
You can run your code in rails c.The console will pop up your sql query,so it may help you to find out the problem.
I guess group_by_hour_of_day is your CustomerCheckin model function.I don't now whether this function has trim your :created_at format. By default the :created_at will contain the second like "%Y-%m-%d %H:%M:%S",so the sql will group by base on the second not the hour. You may try to trim the :created_at format. Like
DateTime.parse("2015-05-08 02:30:01 +0800").strftime("%Y-%m-%d %H")
=> "2015-05-08 02"
Hope it will help you.

Rails string to DOB year

I'm currently storing strings formatted like "01/01/1989" (client side validation) for the bday field.
I want to parse out the year and store it as a variable, like this:
#bdayyear = current_user.bday.year
I want that to bring back "1989"
How would I go about doing that?
Just do using #strftime and ::strptime :
s = "01/01/1989"
current_user.bday.strptime(s, "%d/%m/%Y").strftime("%Y")
# => "1989"
Convert it into a DateTime object and you can call year on it
>> DateTime.parse("01/01/1989").year
=> 1989
If the format is always consistent the why not just use the string
"01/01/1989".slice(-4..-1)
"01/01/1989"[-4..-1]
Or
"01/01/1989".split(/\W/).pop
These will all return "1989" without converting it to a date

Merge Time into a DateTime to update Datetime

I need to update time info in a DateTime.
I get a string in the format "14" or "14:30" (for example), so I need to give it to Time parser to get the right hour. Then I need to update self.start_at which is a datetime which already has a time, but I need to update it.
self.start_at_hours = Time.parse(self.start_at_hours) # example 14:30:00
# NEED TO UPDATE self.start_at which is a datetime
I was using the change method on self.start_at but it only takes hour and minutes separated and I'm not sure what should I do.
Have you thought about doing somethings like this?
time_to_merge = Time.new
date_to_merge = Date.today
merged_datetime = DateTime.new(date_to_merge.year, date_to_merge.month,
date_to_merge.day, time_to_merge.hour,
time_to_merge.min, time_to_merge.sec)
For replacing time, the .change() method should work, like this:
my_datetime = my_datetime.change(hour: my_time.hour, min: my_time.min, sec: my_time.sec)
For adding time, try converting to seconds and then adding them:
my_datetime += my_time.seconds_since_midnight.seconds

Rails: My code causing "can't convert nil into String"

This code from my developer below... does it expect something like "1 - January" and try to parse it into something else?
I changed the code on my form so now the value being passed to this controller is just "1" instead of "1 - January"
How can I fix this so I don't get the "can't convert nil into string" error?
def get_expiry_month_number(monty_det, expiry_year)
if MONTH_NAMES.include? monty_det
month_number = MONTH_NAMES.index(monty_det)
month_number = MONTH_NUMBERS[month_number]
expiry_year = month_number.to_s + expiry_year[expiry_year.length-3..expiry_year.length-1]
return expiry_year
end
end
In your example you are running into the problem that one of your strings in your calculation for year is nil. Most likely this one,
expiry_year[expiry_year.length-3..expiry_year.length-1]
You can try and fix this by calling to_s on the culprit variable, thus converting the nil into '' and then concatenated.
Like so: expiry_year = month_number.to_s + expiry_year[expiry_year.length-3..expiry_year.length-1].to_s
However, this may have other seen consequences and isn't recommended. A better approach would be to find out why something you expect is a string is turning out to be empty, and perhaps displaying an appropriate error.
Could you explain a little bit more about what the function is supposed to do? And perhaps the value of expiry_year and monty_det when the error occurs? puts statements can be a big help here.
I guess that get_expiry_month_number('january', 2011) would return: 1011.
Indeed:
month_number should return 1 for january
expiry_year[expiry_year.length-3..expiry_year.length-1] returns the 3 last characters of 2011 => 011

Saving and Querying a Date in GORM Grails

I have a database table TableA, which has a column 'theDate' for which the datatype in the database is DATE.
When I save a java.util.Date to 'theDate' through GORM it appears to save just the date value when I look at the data in the table by just executing select * from TableA.
However, when I run a query such as:
select * from TableA where theDate = :myDate
No results are found, but if I run something like;
select * from TableA where theDate <= :myDate
I do get results.
So it's like the Time is relevant.
My question is how do I save a Date and query for a Date ignoring the Time completely and just matching on an exact Date only?
Thanks.
note: I have also tried using sql.Date and util.Calendar but to no success.
clearTime()
You can use clearTime() before saving and before comparing to zero out the time fields:
// zero the time when saving
new MyDomain(theDate: new Date().clearTime()).save()
// zero the target time before comparing
def now = new Date().clearTime()
MyDomain.findAll('SELECT * FROM MyDomain WHERE theDate = :myDate', [myDate: now])
joda-time plugin
An alternative would be to install the joda-time plugin and use the LocalDate type (which only holds date information, no times) instead of Date. For what it's worth, I don't think I've worked on a project with dates without using the Joda plugin. It's completely worth it.
If you have date saved without clearing you could retrieve it using range, as Jordan H. wrote but in more simple way.
def getResults(Date date) {
def from = date.clearTime()
def to = from + 1
def results = MyDomain.findAll("from MyDomain where dateCreated between :start and :stop" ,[start:from,stop:to])
}
Your question may be a duplicate. See Convert datetime in to date. But if anyone has more recent information, that would be great.
If that doesn't help, you can hack it the way I might, with a BETWEEN restriction, e.g.
def today = new Date()
def ymdFmt = new java.text.SimpleDateFormat("yyyy-MM-dd")
def dateYmd = ymdFmt.format(today)
def dateTimeFormat = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
def startDate = dateTimeFormat.parse("${dateYmd} 00:00:00");
def endDate = dateTimeFormat.parse("${dateYmd} 23:59:59");
MyDomain.findAll("from MyDomain where dateCreated between ? and ?", [startDate, endDate])
It's definitely not pretty, but it may get you where you're going.
I figured it out.
I used DateGroovyMethods.clearTime to clear the time value before saving.
You can use the DB type date not datetime , in the filed type

Resources