Pratik
Pratik

Reputation: 1512

How to generate series with month span in C#

I have a webservice which accepts the date range and due to huge data import i am passing the date range parameters for 1 month only.

Acceptable start date format : 1-apr-2012

Acceptable end date format : 30-apr-2012

I want programatically send each month's start and end dates for last 2 years like 1-Jan-2012 till 31-aug-2012

Below is sample piece of code which i started to test but its giving me span of 30 days when end date plus 1 month is added.

static void Main(string[] args)
    {
        string startDt = "1-apr-2011";
        string endDt = "30-apr-2012";
        DateTime dt = Convert.ToDateTime(startDt);
        DateTime dt2 = Convert.ToDateTime(endDt);
        CultureInfo culture = CultureInfo.GetCultureInfo("en-GB");

        //Console.WriteLine(value.ToString("D", culture)); 

        while (dt < dt2)
        {
            Console.WriteLine(dt.ToString("D", culture));
           // Console.WriteLine(String.Format("{0:dd-MM-yyyy}", dt));
            dt = dt.AddMonths(1);                
        }
        Console.ReadLine();
    }

Corrected Code:

 static void Main(string[] args)
    {
        string startDt = "1-apr-2011";
        string endDt = "30-apr-2012";
        DateTime dt = Convert.ToDateTime(startDt);
        DateTime dt2 = Convert.ToDateTime(endDt);
        CultureInfo culture = CultureInfo.GetCultureInfo("en-GB"); 
        while (dt < dt2)
        {
            DateTime dtend = new DateTime(dt.Year, dt.Month, DateTime.DaysInMonth(dt.Year, dt.Month)); 
            Console.WriteLine(dt.ToString("D", culture));
            Console.WriteLine(dtend.ToString("D", culture));               
            dt = dt.AddMonths(1);                
        }
        Console.ReadLine();
    }

Upvotes: 1

Views: 2379

Answers (3)

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

Every month first date will be 1 but for last date you have to use

int DateTime.DaysInMonth(int year, int month);

This takes month because month can be 28, 29, 30 or 31 days and takes year for leap year consideration.

Adding AddMonth(1) to last date of a month does not necessary means it will give last date of next month.

Picture this scenario

DateTime dtt = new DateTime(2012, 09, 30);

dtt = dtt.AddMonths(1);

Now 30-Sept is last date of September but when we AddMonth(1) it gives us 30-October. But last date of October is 31st not 30th.

so if for January first date will be

DateTime startdateofamonth = new DateTime(2011, 1, 1);

while last date will be

DateTime lastdateofamonth = new DateTime(2011, 1, DateTime.DaysInMonth(2011,1));

Upvotes: 6

Habib
Habib

Reputation: 223267

You can do:

var firstDateOfMonth = new DateTime(dt.Year, dt.Month, 1);
var last = new DateTime(dt.Year, dt.Month, 1).AddMonths(1).AddDays(-1);

So your code should be:

  static void Main(string[] args)
        {
            string startDt = "1-apr-2011";
            string endDt = "30-apr-2012";
            DateTime dt = Convert.ToDateTime(startDt);
            DateTime dt2 = Convert.ToDateTime(endDt);
            while (dt < dt2)
            {
                var first = new DateTime(dt.Year, dt.Month, 1);
                var last = new DateTime(dt.Year, dt.Month, 1).AddMonths(1).AddDays(-1);
                Console.Write(String.Format("{0:dd-MMM-yyyy}", first));
                Console.Write("== To == ");
                Console.WriteLine(String.Format("{0:dd-MMM-yyyy}", last));
                dt = dt.AddMonths(1);
            }
            Console.Read();
        }

Output would be:

01-Apr-2011== To == 30-Apr-2011
01-May-2011== To == 31-May-2011
01-Jun-2011== To == 30-Jun-2011
01-Jul-2011== To == 31-Jul-2011
01-Aug-2011== To == 31-Aug-2011
01-Sep-2011== To == 30-Sep-2011
01-Oct-2011== To == 31-Oct-2011
01-Nov-2011== To == 30-Nov-2011
01-Dec-2011== To == 31-Dec-2011
01-Jan-2012== To == 31-Jan-2012
01-Feb-2012== To == 29-Feb-2012
01-Mar-2012== To == 31-Mar-2012
01-Apr-2012== To == 30-Apr-2012

Upvotes: 2

V4Vendetta
V4Vendetta

Reputation: 38210

I am not sure if this is what you mean,

The start date is first so you should be able to get it via new DateTime(2011,[month],1) you need to increment the months (don't go beyond 12 ! )

Further you can get the Days in the month using DateTime.DaysInMonth which could be added up to your start date (via AddDays()) and arrive at the end date.

Upvotes: 1

Related Questions