Click here to Skip to main content
15,885,839 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
DateTime dt = DateTime.Now.AddDays(6);
                    if (dt.DayOfWeek == DayOfWeek.Saturday)
                    {
                        
                    }
                    else if (dt.DayOfWeek == DayOfWeek.Sunday)
                    {

                    }


                 
                    string day = dt.DayOfWeek.ToString();


how could i not add if satuday or sunday, i only want to display from monday to friday?


UPDATE
===========
C#
DateTime dt = DateTime.Now.AddDays(6);

                    if (dt.DayOfWeek == DayOfWeek.Saturday)
                    {
                        dt.AddDays(-2);
                    }
                    else if (dt.DayOfWeek == DayOfWeek.Sunday)
                    {
                        dt.AddDays(-1);
                    }


                    string day = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(DateTimeFormatInfo.CurrentInfo.GetDayName(dt.DayOfWeek));
Posted
Updated 17-Mar-15 22:44pm
v2
Comments
Kurac1 18-Mar-15 4:12am    
I am adding for example right not 6 days from today so it will be tuesday but it should be Thursday because i dont want to use saturday and sunday?

1 solution

C#
public static class DateTimeExtensions
{
    public static bool IsBusinessDay(this DateTime instance)
    {
        return instance.DayOfWeek != DayOfWeek.Saturday && instance.DayOfWeek != DayOfWeek.Sunday;
    }
    public static DateTime AddBusinessDays(this DateTime instance, int days)
    {
        var newDate = instance;
        while (days > 0)
        {
            newDate = newDate.AddDays(-1);
            if (newDate.IsBusinessDay())
                --days;
        }
        return newDate;
    }
}
 
Share this answer
 
v2
Comments
Kurac1 18-Mar-15 4:22am    
Can u pls post an example of my question
Paw Jershauge 18-Mar-15 4:25am    
public static class DateTimeExtensions
{
public static bool IsBusinessDay(this DateTime instance)
{
return instance.DayOfWeek != DayOfWeek.Saturday && instance.DayOfWeek != DayOfWeek.Sunday;
}
public static DateTime AddBusinessDays(this DateTime instance, int days)
{
var newDate = instance;
while (days > 0)
{
newDate = newDate.AddDays(-1);
if (newDate.IsBusinessDay())
--days;
}
return newDate;
}
}


static class Program
{
[STAThread]
static void Main()
{
DateTime dt = DateTime.Now.AddBusinessDays(6);
}
}
Kurac1 18-Mar-15 4:37am    
if (newDate.IsBusinessDay()) get error on this line System.DateTime does not contain a definition for IsBusinessDay
Paw Jershauge 18-Mar-15 4:42am    
Ok i guess your pretty new to coding., did you add the Extension class to your code or???

Or just simple call it like this:
static class Program
{
public static bool IsBusinessDay(this DateTime instance)
{
return instance.DayOfWeek != DayOfWeek.Saturday && instance.DayOfWeek != DayOfWeek.Sunday;
}
public static DateTime AddBusinessDays(this DateTime instance, int days)
{
var newDate = instance;
while (days > 0)
{
newDate = newDate.AddDays(-1);
if (newDate.IsBusinessDay())
--days;
}
return newDate;
}
[STAThread]
static void Main()
{
DateTime dt = AddBusinessDays(DateTime.Now);
}
}
Kurac1 18-Mar-15 4:44am    
Hmm i want to use my code, i will update

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