How to add months in date with rails [closed] - ruby-on-rails

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
For example: I am inserting date 2011-06-01 (format mm/dd/yyyy) and now I want to add 8 months to this date. I want the result to be 2012-02-01.
So when adding months, the year may also increase.

Rails provides for + n.days, n.months, etc.
my_date + 8.months
This is an ActiveRecord, not a Ruby thing, though. So make sure it's loading thru Rails.

You can use the '>>' operator over a Date object to easily achieve just that, it returns a date object N (N being a number) months after the original.
In your case:
Date.new(2011,6,1) >> 8
That will return the date of 2012-02-01.
Likewise you can use '<<' in order to 'travel back in time'. ;)

Related

Ruby and Rail - How to combine date and time to get data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
Currently, I have 3 columns : booking_date, booking_time_from, booking_time_to.
Problem is how can i get all data have booking time 2 hours ago using Active Support ?
Booking.where([booking_time], 2.hours.ago.to_datetime)
I'm not sure about what's the difference between booking_time_from and booking_time_to, and which column that the booking time you're referring here. Let's assume it's booking_time_to, you can
Booking.where("booking_time_to < ?", 2.hours.ago)

How do I separate out fields in a CKRecords results Xcode objective-c [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have saved data to fields in CloudKit and I have queried for the posts by date in decending order and by getting the one at index(0) I have the last post. My question is how do I separate out the various fields and use them to populate the text in various labels in my view. Here is what I have for the latest post query.
<CKRecord: 0x104e5b600; recordID=157F08E2-FF00-42DD-8B89-7CB10FAC81B6:(_defaultZone:__defaultOwner__), recordChangeTag=kk6d3s69, values={ temperature=54, trip=50, level=53, battery=64, date=1-20-2021 20:57:09 }, recordType=MyPoolInfo>
If someone could help me out I would appreciate it. Thanks
I figured this out. This is a dictionary. So I just called the following:
NSLog(#"temperature:%#",[dictionary objectForKey:#"temperature"]) ;
NSLog(#"date:%#",[dictionary objectForKey:#"date"]) ;
NSLog(#"trip:%#",[dictionary objectForKey:#"trip"]) ;
NSLog(#"battery:%#",[dictionary objectForKey:#"battery"]) ;
NSLog(#"level:%#",[dictionary objectForKey:#"level"]) ;
I could then fill in the label.text like this:
self.DateLabel.text =[dictionary objectForKey:#"date"];
in case anyone needs to know.

Get value of most recent date [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to get the value of col B for the most recent date. The number of rows may change... i.e. oct isnt always the most recent date.
On dates u can use the MAX function. If you wrap this inside a VLOOKUP the MAX will get you the searchkey and the returns the value next to it.
=VLOOKUP(MAX(A1:A10),A1:B10,2,0)
With data in cols A and B, use:
=index(B:B,match(max(A:A),A:A,0))
As you see from the example, we don't even have to sort the data.
proper way:
=MAXIFS(B:B, A:A, MAX(A:A)
or:
=FILTER(B:B, A:A=MAX(A:A))

Find the number of months between two dates in decimal [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to calculate the number of months between two dates. I want the result in decimal point. For example, if my date range is "2017-02-01" to "2017-03-1", then I am expecting the number of months to be something like 1.033 in decimal.
Please help.
You should be able to do something like this
#Mondel.time_diff_in_months("2017-03-01", "2017-02-01")
def time_diff_in_months(grater_date, lesser_date)
Time.at(grater_date.to_time - lesser_date.to_time).month # 1
end
I hope that this helps

How to add a number to a current value in rails? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Sorry for my English. I have a model with some field.
How to add a number to a current value in field?
I want to add the number several times
i generated scaffold.I use this to create modal. But i want to add number to a current value when i clicked submit
#foo = Foo.first
#foo.bar += 10
#which is a shorter way of saying #foo.bar = #foo.bar + 10
#foo.save
If this isn't helpful, please add some more details to your question, eg your example code you have so far.

Resources