1

Please help me with the code i.e how to get Last day of Last month.

i.e. if today date is 06-11-2016 (dd-mm-yyyy) => output: 31-10-2016

potashin
  • 44,205
  • 11
  • 83
  • 107
Ravi
  • 211
  • 3
  • 11

1 Answers1

4

Anything like this?

require 'date'

t = Date.today
Date.new(t.year, t.month) - 1
 => #<Date: 2016-10-31 ((2457693j,0s,0n),+0s,2299161j)> 
Ursus
  • 29,643
  • 3
  • 33
  • 50
  • 3
    what about `t - t.day` ?) It should return the same result, but without creating new instance of `Date`) – Oleksandr Holubenko Nov 06 '16 at 18:53
  • It works, cool :) – Ursus Nov 06 '16 at 19:12
  • Hello Alex and Ursus, I have tried to get a weekday from a given date. and the below code worked for me. Just want to know, Is there a better way to do this `require 'date' d = Date.today weekday = Date.parse( '#{d.day} #{d.month} #{d.year}' ) puts weekday.strftime('%A') Output: **Monday** ` – Ravi Nov 09 '16 at 05:13
  • Well, it's a matter of tastes. You could do just `Date.today.strftime('%A')` – Ursus Nov 09 '16 at 10:03
  • Thank you very much – Ravi Nov 09 '16 at 19:18