Proper way to get next year from now - ruby-on-rails

How I can get the next year in Rails?
I need to get only the year, without the date.
When I do 1.year.from_now it returns a full date, not just the year.

There are several options to do this. But using your format, we can do it like this:
1.year.from_now.year

Try these
Time.now.year + 1
output will be
2019
As suggest by #sschmeck you can try these also to get only next year
Time.now.year.succ

today's date:
require 'date'
Date.today
#=> #<Date: 2018-01-02 ...>
This day next_year:
Date.today.next_year
#=> #<Date: 2019-01-02 ...>
And finally its year:
Date.today.next_year.year
#=> 2019

You can use this,
d = Time.now.year.next

Related

Ruby - Convert Day Of Week To Integer

How would you convert a day string (i.e. "Monday" or "Wednesday"), into the corresponding wday integer (1 or 3)?
I've come up with this convoluted way
Date.today.beginning_of_week("Monday".downcase.to_sym).wday
You can parse it using strptime:
Date.strptime('Monday', '%A').wday
#=> 1
Date.strptime('Wednesday', '%A').wday
#=> 3
The intermediate date object refers to the weekday in the current week:
Date.today
#=> #<Date: 2018-11-20 ...>
Date.strptime('Monday', '%A')
#=> #<Date: 2018-11-19 ...>
You can also use _strptime (prefixed with an underscore) to extract the date elements which happen to be :wday for a single weekday:
Date._strptime('Monday', '%A')
#=> {:wday=>1}
You can use the CHRONIC gem.
Chronic is a Ruby natural language date/time parser written by Tom
Preston-Werner (#mojombo) which takes surprisingly human readable text
and converts it to dates. (http://robdodson.me/playing-with-ruby-dates/)
You can use like this:
require 'chronic'
Chronic.parse('Monday').wday
=> 1
I hope help you!

Rails cant save correct date into database

I have a date format like this "18/12/15" as input (it is a string)
18 => day
12 => month
15 => year (2015)
I am trying to store it in my db in a date column but it doesnt save the proper date (for the previous exemple I get Sat, 15 Dec 2018 whereas I should get something like day, 18 Dec 2015.)
How can I fix that
You could use strptime:
require 'date'
Date.strptime('18/12/15', '%d/%m/%y')
#<Date: 2015-12-18 ((2457375j,0s,0n),+0s,2299161j)>
You want to define a date with the order of day, month, year, while, due to the locale your intention is interpreted as year, month, day.
You can do one of the following:
pass the parameters in inverted order
implement a method which gets the parameters in the correct order and inverts them
set the locale to the one you intend to use
use strptime, as spickermann described

Rails, day of year to date

I have day of the year. For example 15th day of 2014, or 210th day of 2014. I need to get the Date on the particular day. Is there any library function in Rails/Ruby that I can use, or any other elegant way?
Something like:
15th day of 2014 = 15-Jan-2014
210th day of 2014 = 29-Jul-2014
You can use Date.ordinal for that:
require 'date'
Date.ordinal(2014, 210)
# => #<Date: 2014-07-29 ((2456868j,0s,0n),+0s,2299161j)>
Date.new(2014, 1, 1) + 210
=> Wed, 30 Jul 2014
Edit - you'd need to subtract 1 day. I prefer #wonderb0lt's suggestion.

Ruby On Rails, week number is not the correct one (-1)

I need the current week number, if I'm not totally mistaken it's week 51 right now? However when testing it in the console I get this.
Time.now
=> 2013-12-19 11:08:25 +0100
Time.now.strftime('%U')
=> "50"
Date.today
=> Thu, 19 Dec 2013
Date.today.strftime("%U").to_i
=> 50
Why is that?
Time.now.strftime('%V') will give you the week number according to ISO 8601.
why is that?
according to %U or %W, The days in the year before the first week are in week 0 (00..53).
with %V (as #Graeme McLean wrote), The days in the year before the first week are in the last week of
the previous year (01..53).
From here.
Hmm, I'm unsure as to why it is that way, but to get the correct one using Ruby, I use this:
require 'Date'
week_number = Date.today.cweek #=> 51

Why do I get an "invalid date" error for %-m%d%Y"?

Can someone explain why I'm getting an invalid date error with the following line?
Date.strptime("3082013",'%-m%d%Y')
From my understanding, %-m should look for a month with no leading 0. However, executing this code returns
ArgumentError: invalid date
Assuming I can't change the format of the incoming date, what is the best way to deal with this situation?
Add a zero to the left :
Date.strptime("3082013".rjust(8,'0'),'%-m%d%Y')
The problem is that strptime does not suppor the minus (-) flag.
You need to add some sort of separator between your month/day/year. Otherwise how will Ruby know whether "3082013" is 3/08/2013 or 30/8/2013?
Sometimes you have to get a bit lower-level, and do what Date.strptime would do for you:
"3082013".scan(/(\d+) (\d{2}) (\d{4})/x).first
=> ["3", "08", "2013"]
If "308213" is in MMDDYYYY format:
mon, day, year = "03082013".scan(/(\d+) (\d{2}) (\d{4})/x).first.map(&:to_i)
=> [3, 8, 2013]
Or, if you know that it's in DDMMYYYY format, switch mon and day:
day, mon, year = "03082013".scan(/(\d+) (\d{2}) (\d{4})/x).first.map(&:to_i)
=> [3, 8, 2013]
Then you can create a Date object:
Date.new(year, mon, day)
=> #<Date: 2013-03-08 ((2456360j,0s,0n),+0s,2299161j)>

Resources