Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
haii..

I want to get the upcoming date corresponding to a week day. Eg: The date(dd/MM/yyyy) corresponding to coming Friday. So I need a function (C#)..plz help..its urgent
Posted
Updated 2-Dec-12 20:15pm
v2

If you looking to retrieve the day of week from date you can try the below approach

C#
DateTime date = DateTime.Now;
Console.WriteLine(date.ToString("dddd"));


Refer How to: Extract the Day of the Week from a Specific Date[^]

[Update]
Create an enum called days
C#
enum Days
{
    sunday,
    monday,
    tuesday,
    wednesday,
    thrusday,
    friday,
    saturday,
};


You can then use this enum value as a input parameter and retrieve the date of week day using the below logic.
C#
Days weekday = Days.sunday;
DateTime Today = DateTime.Today;
DateTime finalDate;

if ((int)Today.DayOfWeek < (int)weekday)
{
    finalDate = Today.Date.AddDays((int)weekday - (int)Today.DayOfWeek);
}
else
{
    finalDate = Today.Date.AddDays((int)weekday - (int)Today.DayOfWeek + 7);
}

[/Update]
 
Share this answer
 
v3
Comments
aravindnass 3-Dec-12 2:14am    
thankkss...

Actually my query is :
I have week day as parameter, now I want upcoming date corresponding to that week day
__TR__ 3-Dec-12 2:48am    
I have updated my solution. Let me know if you have any doubts.
Hi dear,

I think you need 'Friday - December - 2012'.

Example :

dateTimePicker1.CustomFormat = System.DateTime.Today.ToString("dddd-MMMM-yyyy");

If you like this answer plz vote me
 
Share this answer
 
v2
Comments
aravindnass 3-Dec-12 2:13am    
I have week day as parameter, now I want upcoming date corresponding to that week day
hi dear,

C#
string Days = "Friday";  // Friday is week day
string dayName = dateTimePicker1.Value.DayOfWeek.ToString();

if (Days == dayName)
{
   // week day
}
else
{
  // Not week day
}
 
Share this answer
 

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