How to get today's date in Ruby 1.9.3?
Did you require 'date'
?
> require 'date'
=> true
> Date.today
=> #<Date: 2013-03-12 ((2456364j,0s,0n),+0s,2299161j)>
Get today's date in Jekyll with Liquid markup
It didn't work for me either. It appears you've hit a current bug in the Ruby 1.9.3 support. There is a pull request that fixes the bug, but it's not incorporated yet. A workaround is listed, perhaps it will work for you:
{{ site.time | date: '%y' }}
Differences in date string parsing between Ruby 1.9.3 and Ruby 1.8.7
Would it be an option for you to use Time.strptime("01/28/2012", "%m/%d/%Y")
in place of Time.parse
? That way you have better control over how Ruby is going to parse the date.
If not there are gems: (e.g. ruby-american_date) to make the Ruby 1.9 Time.parse behave like Ruby 1.8.7, but only use it if it's absolutely necessary.
1.9.3-p0 :002 > Time.parse '01/28/2012'
ArgumentError: argument out of range
1.9.3-p0 :003 > require 'american_date'
1.9.3-p0 :004 > Time.parse '01/28/2012'
=> 2012-01-28 00:00:00 +0000
How do I get a range or array for every minute in a day in Ruby 1.9.3?
This might get you started:
0.upto((60 * 24) - 1).each { |m| puts "%02d:%02d:00" % [m / 60, m % 60] }
Ruby find next Thursday (or any day of the week)
DateTime.now.next_week.next_day(3)
As @Nils Riedemann pointed out:
Keep in mind that it will not get the next thursday, but the thursday
of the next week. eg. If tomorrow was thursday, it won't return
tomorrow, but next week's thursdays.
Here is an excerpt of some code I've written recently which handles the missing case.
require 'date'
module DateTimeMixin
def next_week
self + (7 - self.wday)
end
def next_wday (n)
n > self.wday ? self + (n - self.wday) : self.next_week.next_day(n)
end
end
# Example
ruby-1.9.3-p194 :001 > x = DateTime.now.extend(DateTimeMixin)
=> #<DateTime: 2012-10-19T15:46:57-05:00 ... >
ruby-1.9.3-p194 :002 > x.next_week
=> #<DateTime: 2012-10-21T15:46:57-05:00 ... >
ruby-1.9.3-p194 :003 > x.next_wday(4)
=> #<DateTime: 2012-10-25T15:46:57-05:00 ... >
ruby DateTime parsing from 'mm/dd/yyyy' format
require 'date'
my_date = Date.strptime("12/22/2011", "%m/%d/%Y")
How can I create a new Date instance in Ruby
According to Date documentation:
require 'date'
Date.new(2001,2,25) #=> #<Date: 2001-02-25
Date.jd(2451966) #=> #<Date: 2001-02-25
Date.ordinal(2001,56) #=> #<Date: 2001-02-25
Date.commercial(2001,8,7) #=> #<Date: 2001-02-25
Date.parse('2001-02-25') #=> #<Date: 2001-02-25
Date.strptime('25-02-2001', '%d-%m-%Y') #=> #<Date: 2001-02-25
Time.new(2001,2,25).to_date #=> #<Date: 2001-02-25
Name of this month (Date.today.month as name)
Date::MONTHNAMES[Date.today.month]
would give you "January". (You may need to require 'date'
first).
How to know if today's date is in a date range?
Use ===
Actually, there is an operator that will do this. Make a Range
and compare Time
objects to it using the ===
operator.
start = Time.now.to_i
range = start..(start + 2)
inside = start + 1
outside = start + 3 # ok, now...
range === inside # true
range === outside # false
Update post-comment-flood: This version works well everywhere. (In Rails, in Ruby 1, and in Ruby 2.) The earlier
irb
example also worked fine but the interactive example wasn't always reproduced correctly in some experiments. This one is easier to cut-and-paste.It's all straightened out now.
How can I use a Date object and a string with a day name to create a new Date object?
If you can manage to convert "Thursday" to week day 4 -- I know you can --, you can get another date like this:
1.9.3p125 :014 > d = Date.parse "Mon, 03 Dec 2012"
=> Mon, 03 Dec 2012
1.9.3p125 :015 > Date.commercial d.cwyear, d.cweek, 4
=> Thu, 06 Dec 2012
1.9.3p125 :016 >
BTW, you can store the map from day name to number in an Array or an Hash, or use I18n.
Related Topics
What Orm to Use in One Process Multiple Db Connections Sinatra Application
Best Way to Find_Or_Create_By_Id But Update the Attributes If the Record Is Found
How to Make a Ruby Script Using Trollop for Command Line Parsing
Rails Cancan and State MAChine - Authorizing States
Bundle Exec Jekyll Serve: Cannot Load Such File
Ruby: How to Call Function Before It Is Defined
Floating Point Precision in Ruby on Rails Model Validations
Issues with Installing Ruby 2.0.0 on MACos Catalina
Respond_With Redirect with Notice Flash Message Not Working
Accessing a Has_One Associations' Attributes
Ruby: Mass Initializing Instance Variables
How to Listen to Stdin Input Without Pausing My Script
Using Passphrase Callback in Ruby Gpgme
How to Include Ё in [А-Я] Regexp Char Interval