Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
SQL
Can anyone please tell me how to get all days and date of selected month from calendar in .net.This is to create attendance of employees.

I need each days and date of the selected month to be displayed in label box.Like sun,mon,tue etc ...and 1,2,3....28 or 29 or 30 or 31  based on the month and year I selected.
Thanks in advance.
Posted
Comments
Thanks7872 11-May-15 4:45am    
This is list of requirement. Not a question.

A simple loop it is just enough, e.g.
C#
const int MONTH = 5; // write out all the days of May 2015
DateTime dt = new DateTime(2015, MONTH, 1);
while (dt.Month == MONTH)
{
  Console.WriteLine(dt.ToString("ddd dd"));
  dt = dt.AddDays(1);
}
 
Share this answer
 
Comments
JinzNaaz 11-May-15 9:30am    
@CPallini...Everything is working fine.But its showing month,year and time also.I just want dates only like 1,2,...31.Instead its coming 5/11/2015 12:00:00 AM.Also first date is not adding to the list..It starts with 2 only
You can use this method

C#
public List<datetime> GetDates(int year, int month)
{
   return Enumerable.Range(1, DateTime.DaysInMonth(year, month))
                      // Days: 1, 2 ... 31 etc.
                    .Select(day => new DateTime(year, month, day)) 
                     // Map each day to a date
                    .ToList(); // Load dates into a list
}
 
Share this answer
 
v2
Comments
JinzNaaz 11-May-15 5:12am    
@Praveen_Kumar Gupta
Can you please tell me how to use this method.what is datetime in the function (List<datetime>).Here its showing error.
[no name] 11-May-15 5:22am    
datetime typo , now check
solution improved


var mothdates=GetDates(2015,4);// this is a list which have all dates for 4th month , and 2015 year

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