2

Here is my code:

DateTime startDate = Convert.ToDateTime( orderDate ).AddMonths(-1);
DateTime endDate = Convert.ToDateTime( orderDate );

orderDate is a valid date I pass in.

How do I always guarantee startDate is the first day of the previous month for orderDate? How do I always guarantee endDate is the last day of the month for orderDate?

Example:

orderDate = 5/4/2012

I want startDate to be 4/1/2012 (or 04/1/2012 whichever is default)
I want endDate to be 4/30/2012

How can I acheive this?

Anuroopa Shenoy
  • 383
  • 1
  • 4
  • 7
James Wilson
  • 5,074
  • 16
  • 63
  • 122
  • 1
    possible duplicate of [Get the previous month's first and last day dates in c#](http://stackoverflow.com/questions/591752/get-the-previous-months-first-and-last-day-dates-in-c-sharp) – DaveShaw May 04 '12 at 21:11
  • 1
    Why are you converting a DateTime to a DateTime? – Tim Schmelter May 04 '12 at 21:12
  • @TimeSchmelter Because orderDate is a string passed in from two parameters that can be changed via a text box or query string. – James Wilson May 04 '12 at 21:14
  • @JamesWilson: I've just asked because you've said that _"orderDate is a valid date I pass in"_ and there's really [an overload](http://msdn.microsoft.com/en-us/library/7ex62041.aspx) which takes a DateTime. – Tim Schmelter May 04 '12 at 21:15
  • There is no 4/31/2012 :) – D Stanley May 04 '12 at 21:15
  • @TimSchmelter well it is a valid date, perhaps I should have been more specific. I'm still new to asp.net. So orderDate is a string version of a specific date. Keeping me on my toes is always a good thing! :) – James Wilson May 04 '12 at 21:18

5 Answers5

2
DateTime startDate = Convert.ToDateTime( orderDate ).AddMonths(-1);
// set to first of month
startDate = startDate.AddDays(1-startDate.Day);

// Set end date to last of month, which is one day before first of next month
DateTime endDate = startDate.AddMonths(1).AddDays(-1);
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • I'm going with this method. Any chance this can break on December or January like one other answer in this thread? Just want to cover all my bases I can. – James Wilson May 04 '12 at 21:21
  • 1
    @James Wilson - as with any code you pull off SO there are no guarantees :) Unless it's baked into your ASP.NET project you should write unit tests for it. In any case you should definitely test as many fringe cases as you can think of (dates in January, December, 2000, etc.) – D Stanley May 04 '12 at 21:28
1
origDate = startDate.AddMonths(-1);
DateTime myDate = new DateTime(origDate.Year, origDate.Month, 1);

http://msdn.microsoft.com/en-us/library/xcfzdy4x.aspx

Something like this... We do it often enough that we've just created an extension method on date.

Matthew
  • 10,244
  • 5
  • 49
  • 104
1
DateTime startDate = new DateTime( orderDate.Month == 1 ? orderDate.Year - 1 : orderDate.Year, orderDate.Month - 1, 1);
DateTime endDate = new DateTime(orderDate.Year, orderDate.Month, DateTime.DaysInMonth(orderDate.Year, OrderDate.Month));
Icemanind
  • 47,519
  • 50
  • 171
  • 296
0

Use DateTime constructor that takes separate values for each year, month, day,... and adjust them accodringly (i.e. first of the month means day = 1).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0
var orderDate = DateTime.Now.Date;
var endDate = orderDate.AddDays(-orderDate.Day);
var startDate  = endDate.AddDays(-(endDate.Day - 1));
faester
  • 14,886
  • 5
  • 45
  • 56