0
votes

I am trying to create a c# mvc application for displaying timsheets of each employee. I want to create a table dynamically and the table header should contain the values as:

Mon 20-04-2020 |Tue 21-04-2020|Wed 22-04-2020|Thu 23-04-2020|Fri 24-04-2020|Sat 25-04-2020|Sun 26-04-2020

I am a beginner so i don't know much about it and stuck on it from a while. Thanks in advance.

1
Show us what you have tried so far, so we can get you help were you are stuck. - Shane_Yo
Is formatting the date string your problem? Or a ui control? I am voting to close this question, since it's to vague - nilsK

1 Answers

0
votes

If you want to list the current week days, starting from Monday you can do this :

var today = DateTime.Now;       
var startDate = today.AddDays(-(((today.DayOfWeek - DayOfWeek.Monday) + 7) % 7));
var endDate = startDate.AddDays(7);
var currentWeekDays = Enumerable.Range(0, 7).Select(x => startDate.AddDays(x));

And if you want to format a date to get Mon 20-04-2020, you can use the format "ddd dd-MM-yyyy".

To conclude, you can obtain Mon 20-04-2020|Tue 21-04-2020|Wed 22-04-2020|Thu 23-04-2020|Fri 24-04-2020|Sat 25-04-2020|Sun 26-04-2020 by doing this :

var formattedCurrentWeekDays = currentWeekDays.Select(d => d.ToString("ddd dd-MM-yyyy")).ToList();
var result = string.Join("|", formattedCurrentWeekDays);
Console.WriteLine(result);