Hello everyone. Please advice how to get todays month first day and previous month first day. If go on by this post date, it will need to give me now 01-11-2010 and 01-10-2010 or if today was like 3 Jan 2010 it would be 01-01-2010 and 01-12-2009 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jamey Cribbs
2010-Nov-03 13:59 UTC
Re: Todays month first day and previous month first day
Something like: Date.today.beginning_of_month Date.today.ago(1.month).beginning_of_month Probably an easier way, just off the top of my head. Jamey On Wed, Nov 3, 2010 at 9:43 AM, Vitaliy Yanchuk <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> Hello everyone. > > Please advice how to get todays month first day and previous month first > day. > > If go on by this post date, it will need to give me now > 01-11-2010 > and > 01-10-2010 > > or if today was like 3 Jan 2010 it would be > 01-01-2010 > and > 01-12-2009 > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker
2010-Nov-03 14:05 UTC
Re: Todays month first day and previous month first day
Vitaliy Yanchuk wrote in post #958983:> Hello everyone. > > Please advice how to get todays month first day and previous month first > day. > > If go on by this post date, it will need to give me now > 01-11-2010 > and > 01-10-2010 > > or if today was like 3 Jan 2010 it would be > 01-01-2010 > and > 01-12-2009ruby-1.9.2-p0 > today = Time.now => 2010-11-03 10:02:46 -0400 ruby-1.9.2-p0 > today.at_beginning_of_month => 2010-11-01 00:00:00 -0400 ruby-1.9.2-p0 > today.beginning_of_month => 2010-11-01 00:00:00 -0400 ruby-1.9.2-p0 > today.beginning_of_month - 1.month => 2010-10-01 00:00:00 -0400 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker
2010-Nov-03 14:09 UTC
Re: Todays month first day and previous month first day
Robert Walker wrote in post #958994:> ruby-1.9.2-p0 > today = Time.now > => 2010-11-03 10:02:46 -0400 > ruby-1.9.2-p0 > today.at_beginning_of_month > => 2010-11-01 00:00:00 -0400 > ruby-1.9.2-p0 > today.beginning_of_month > => 2010-11-01 00:00:00 -0400 > ruby-1.9.2-p0 > today.beginning_of_month - 1.month > => 2010-10-01 00:00:00 -0400Jamey Cribbs wrote> Date.today.beginning_of_monthOr yes, using Date rather than Time as Jamie showed. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rob Biedenharn
2010-Nov-03 14:56 UTC
Re: Re: Todays month first day and previous month first day
On Nov 3, 2010, at 10:09 AM, Robert Walker wrote:> Robert Walker wrote in post #958994: >> ruby-1.9.2-p0 > today = Time.now >> => 2010-11-03 10:02:46 -0400 >> ruby-1.9.2-p0 > today.at_beginning_of_month >> => 2010-11-01 00:00:00 -0400 >> ruby-1.9.2-p0 > today.beginning_of_month >> => 2010-11-01 00:00:00 -0400 >> ruby-1.9.2-p0 > today.beginning_of_month - 1.month >> => 2010-10-01 00:00:00 -0400 > > Jamey Cribbs wrote >> Date.today.beginning_of_month > > Or yes, using Date rather than Time as Jamie showed.Current versions of ActiveSupport handle this properly, but older versions that treat 1.month simply as 30 days of seconds would fail to do the expected thing for dates in March (or any dates in months that follow a 31 day month -- yeah, it''s not looking too good). However, you can get what you want with nothing more than the standard Ruby Date class: [ruby-1.9.2-p0] :Users/rab $ irb irb> require ''date'' => true irb> t = Date.today => #<Date: 2010-11-03 (4911007/2,0,2299161)> irb> puts t 2010-11-03 => nil irb> bom = t - t.mday + 1 => #<Date: 2010-11-01 (4911003/2,0,2299161)> irb> puts bom 2010-11-01 => nil irb> prev = bom << 1 => #<Date: 2010-10-01 (4910941/2,0,2299161)> irb> puts prev 2010-10-01 => nil -Rob Rob Biedenharn Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.com/ -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Vitaliy Yanchuk
2010-Nov-03 15:19 UTC
Re: Todays month first day and previous month first day
Thanks all, the method beginning_of_month is just what I was looking for. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2010-Nov-06 14:30 UTC
Re: Re: Todays month first day and previous month first day
On 3 November 2010 14:56, Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> wrote:> Current versions of ActiveSupport handle this properly, but older versions > that treat 1.month simply as 30 days of seconds would fail to do the > expected thing for dates in March (or any dates in months that follow a 31 > day month -- yeah, it''s not looking too good).ah... the old parsing "31st Feb" giving no error, but instead returning "3rd March" :-) You can also avoid errors given by "Date.today.beginning_of_month - 1.month" or "Date.today.beginning_of_month.ago(1.month)" by using "Date.today.beginning_of_month.ago(1.day).beginning_of_month" as another alternative option. Dates and times are horrible... -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fred Ballard
2010-Nov-06 14:53 UTC
Re: Re: Todays month first day and previous month first day
OT The problem is very old. TSO, Time Sharing Option, was IBM''s first commercial timesharing product. Its interactivity offered the first way for most enterprise customers off punched cards. When the date first went from February 28 to February 29 in 1972, the date on the session startup message read "March 0". On Sat, Nov 6, 2010 at 9:30 AM, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 3 November 2010 14:56, Rob Biedenharn <Rob-GBZH0y1GwQfnZcttdmLDtcI/UQi/AW5J@public.gmane.org> > wrote: > > Current versions of ActiveSupport handle this properly, but older > versions > > that treat 1.month simply as 30 days of seconds would fail to do the > > expected thing for dates in March (or any dates in months that follow a > 31 > > day month -- yeah, it''s not looking too good). > > ah... the old parsing "31st Feb" giving no error, but instead > returning "3rd March" :-) > > You can also avoid errors given by "Date.today.beginning_of_month - > 1.month" or "Date.today.beginning_of_month.ago(1.month)" by using > "Date.today.beginning_of_month.ago(1.day).beginning_of_month" as > another alternative. > > Dates and times are horrible... > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.