Generate date range in 1 year slot

Niladri.Biswas
Posted by Niladri.Biswas under C# category on | Points: 40 | Views : 1067
Given a StartDate in MM/DD/YYYY format , the challenge is to generate the date range in 1 year slot such that the program will stop at the current date.

class Program
{
static void Main(string[] args)
{
var startDate = "04/10/2011";
var endDate = DateTime.Now.AddDays(-1).ToString("MM/dd/yyyy");
int year = Convert.ToDateTime(startDate).Year;
var res = (DateTime.Now.Year - Convert.ToDateTime(startDate).Year) + 1;

if (res > 0)
{
res = res - 1;
if (Convert.ToDateTime(startDate).Year != DateTime.Now.Year)
{
endDate = "12/31/" + year.ToString();
}
}

for (int i = res; i >= 0; i--)
{

Console.WriteLine("StartDate = {0} , EndDate = {1}", startDate, endDate);
if (res > 0)
{
res = res - 1;
year = (year + 1);
startDate = "01/01/" + year;
endDate = "12/31/" + year;
if (res == 1)
{
endDate = Convert.ToDateTime("12/31/" + year).ToString();
}
}
}

Console.ReadKey();
}

}

Comments or Responses

Login to post response