Click here to Skip to main content
15,915,324 members
Please Sign up or sign in to vote.
1.83/5 (3 votes)
See more:
i am entering dayname and i want to store all dates of entered day from current month plz help me?suppose i choose monday then i want to store all dates(5-05-2014,etc) of monday from may month
Posted
Updated 2-May-14 19:56pm
v3
Comments
Bh@gyesh 3-May-14 1:50am    
Elaborate more your question with example.
Abhinav S 3-May-14 1:50am    
What have you tried so far?

1 solution

C#
private List<datetime> GetDays(DayOfWeek givenDay)
{
    DateTime now = DateTime.Now;
    List<datetime> days = new List<datetime>();
    DateTime basedt = new DateTime(now.Year, now.Month, 1);
    while ((basedt.Month == now.Month) && (basedt.Year == now.Year))
    {
        if (basedt.DayOfWeek == givenDay)
        {
            days.Add(new DateTime(basedt.Year, basedt.Month, basedt.Day));
        }
        basedt = basedt.AddDays(1);
    }
    return days;
}

usage
C#
var days = GetDays(DayOfWeek.Sunday);

Quote:
i am entering dayname

then you may need to convert string day name to a DayOfWeek. You can do as below, assume you have "Wednesday" as input
C#
string strDay = "Wednesday";
DayOfWeek day;
if (Enum.TryParse<DayOfWeek>(strDay, out day))
{
    var days = GetDays(day);
}

hope this helps :)
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900