Displaying specfic weekdates [Resolved]

Posted by Suraj038 under .NET Framework on 8/29/2016 | Points: 10 | Views : 265 | Status : [Member] | Replies : 1
I'm creating a website in Asp.net.

I need to display dates in labels(any control) in the below format.(only weekdays).

for example:

On 8/9/2016 'day display like "8/9/2016 to 8/15/2016 " ,
On 8/10/2016 'day display like 8/9/2016 to 8/15/2016
On 8/11/2016 'day display like 8/9/2016 to 8/15/2016
On 8/12/2016 'day display like 8/9/2016 to 8/15/2016
On 8/13/2016 'day display like 8/9/2016 to 8/15/2016
On 8/14/2016 'day display like 8/9/2016 to 8/15/2016
On 8/15/2016 'day display like 8/9/2016 to 8/15/2016


later on

On 8/16/2016 'day display like 8/16/2016 to 8/22/2016
On 8/17/2016 'day display like 8/16/2016 to 8/22/2016
On 8/18/2016 'day display like 8/16/2016 to 8/22/2016
On 8/19/2016 'day display like 8/16/2016 to 8/22/2016
On 8/20/2016 'day display like 8/16/2016 to 8/22/2016
On 8/21/2016 'day display like 8/16/2016 to 8/22/2016
On 8/22/2016 'day display like 8/16/2016 to 8/22/2016


Could anyone please help with any logic. Let me know if your not clear.




Responses

Posted by: Rajnilari2015 on: 9/22/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Try this

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var initialDate = Convert.ToDateTime("8/9/2016");
var dtRange = GetDateRange(initialDate);
var lastDt = FormateDate(initialDate.AddDays(6));

//display
dtRange.ForEach(dt => Console.WriteLine("On {0} day display like {1} to {2}", dt, dt, lastDt));
Console.ReadKey();
}

private static List<string> GetDateRange(DateTime startDt)
{
List<string> dtList = new List<string>();
Enumerable
.Range(0, 7)
.ToList()
.ForEach(i => dtList.Add(FormateDate(startDt.AddDays(i))));
return dtList;
}
private static string FormateDate(DateTime dt)
{
return dt.ToString("MM/dd/yyyy");
}

}
}


Result
---------
On 08/09/2016 day display like 08/09/2016 to 08/15/2016
On 08/10/2016 day display like 08/10/2016 to 08/15/2016
On 08/11/2016 day display like 08/11/2016 to 08/15/2016
On 08/12/2016 day display like 08/12/2016 to 08/15/2016
On 08/13/2016 day display like 08/13/2016 to 08/15/2016
On 08/14/2016 day display like 08/14/2016 to 08/15/2016
On 08/15/2016 day display like 08/15/2016 to 08/15/2016


Change the initialDate to 8/16/2016 like
var initialDate = Convert.ToDateTime("8/16/2016");


Result
---------
On 08/16/2016 day display like 08/16/2016 to 08/22/2016
On 08/17/2016 day display like 08/17/2016 to 08/22/2016
On 08/18/2016 day display like 08/18/2016 to 08/22/2016
On 08/19/2016 day display like 08/19/2016 to 08/22/2016
On 08/20/2016 day display like 08/20/2016 to 08/22/2016
On 08/21/2016 day display like 08/21/2016 to 08/22/2016
On 08/22/2016 day display like 08/22/2016 to 08/22/2016


Hope that helps

--
Thanks & Regards,
RNA Team

Suraj038, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response