C# .NET - How to calculate num of weekdays between 2 dates
Asked By jez enefer-mills
14-Nov-05 05:34 AM
Hi everyone,
Sorrry to trouble you all again.
I have 2 dates which I subtract from each other to find out the number of days between them.
As follows: -
this.txttotdays.Text = (DateTime.Parse(this.txtDate4.Text) - DateTime.Parse(this.txtDate1.Text)).Days.ToString();
Can someone show me how to modify this line of code to get a count of weekdays only
Thanks
try
DateTime asof,bday;
TimeSpan span = asof - bday;
Console.Writeline(span.Days());
Weekdays only
I used you suggestion is this code: -
TimeSpan span = (DateTime.Parse(this.txtDate4.Text) - DateTime.Parse(this.txtDate1.Text));
this.txtTotDays2.Text = span.Days.ToString();
but got an answer including weekends
Can you help further
Something like this
You need to itereate over the DateTimes
and find the DayOfWeek of every dateTime. If this is Monday through Friday then Increment your counter by1.
Below link elaborates this
http://www.dotnet247.com/247reference/msgs/32/162879.aspx
Here's a formula based approach
I saw examples using a loop and counter, but it could cause slow performance in some circumstances, so I set about finding a formula based approach. I was unable to find such a formula after digging deep into Google, so I wrote my own. Let me just say it was a lot more challenging than I thought it would be.
int Weekdays(DateTime dtmStart, DateTime dtmEnd)
{
// This function includes the start and end date in the count if they fall on a weekday
int dowStart = ((int)dtmStart.DayOfWeek == 0 ? 7 : (int)dtmStart.DayOfWeek);
int dowEnd = ((int)dtmEnd.DayOfWeek == 0 ? 7 : (int)dtmEnd.DayOfWeek);
TimeSpan tSpan = dtmEnd - dtmStart;
if (dowStart <= dowEnd)
{
return (((tSpan.Days / 7) * 5) + Math.Max((Math.Min((dowEnd + 1), 6) - dowStart), 0));
}
else
{
return (((tSpan.Days / 7) * 5) + Math.Min((dowEnd + 6) - Math.Min(dowStart, 6), 5));
}
}
Bulldog replied to Bernal Schooley
I found this a little har to follow, so i made this one.
public static int Weekdays(DateTime startDate, DateTime endDate)
{
if (startDate > endDate)
{
Swap<DateTime>(ref startDate, ref endDate);
}
int weekDaysInFirstWeek = Math.Min(DayOfWeek.Saturday - startDate.DayOfWeek, 5);
int weekDaysInLastWeek = Math.Min((int)endDate.DayOfWeek, 5);
//set startdate on the saterday of the week
startDate = startDate.AddDays(DayOfWeek.Saturday - startDate.DayOfWeek).Date;
//set enddate on the sunday of the week
endDate = endDate.AddDays(-1 * (int)endDate.DayOfWeek).Date;
//number of full weeks between the dates
int fullWeeksbetween = (endDate - startDate).Days / 7;
return (fullWeeksbetween * 5) + weekDaysInFirstWeek + weekDaysInLastWeek;
}
Bulldog replied to Bulldog
Oops small mistake there
public static int Weekdays(DateTime startDate, DateTime endDate)
{
if (startDate > endDate)
{
Swap<DateTime>(ref startDate, ref endDate);
}
int weekDaysInFirstWeek = Math.Min(DayOfWeek.Saturday - startDate.DayOfWeek, 5);
int weekDaysInLastWeek = Math.Min((int)endDate.DayOfWeek, 5);
//set startdate on the next sunday
startDate = startDate.AddDays(DayOfWeek.Saturday - startDate.DayOfWeek + 1).Date;
//set enddate on the saterday of the week
endDate = endDate.AddDays(-1 * (int)endDate.DayOfWeek).Date;
//number of full weeks between the dates
int fullWeeksbetween = (endDate - startDate).Days / 7;
return (fullWeeksbetween * 5) + weekDaysInFirstWeek + weekDaysInLastWeek;
}

set day according to month hi, people. I've got a date that needs to be set to a specific day for each month. But if the 0))) day = 29; return day; } [ / code] [code] int day = CheckDays(start.Year, start.Month, validDay); DateTime validation = new DateTime(start.Year, start.Month, day); / / display result foreach (DateTime date in GetDates(validation, end)) { dt.Rows.Add((date.ToShortDateString())); } [ / code> this code outputs: Valuation Date 30 / 06 / 2009 30 / 07 / 2009 30 / 08 / 2009 30 / 09 / 2009 30 / 10 / 2009 you try to assign invalid day (for e.g. day 31 to April 2010) on datetime variable, you will get error something like: ArgumentOutOfRangeException (Year, Month, and Day parameters describe an
How can I get this to work? Brian VB.NET Discussions ToShortDateString (1) ToShortTimeString (1) TimeSpan (1) DateTime (1) MyPointInTime (1) Console (1) Part (1) ShiftStartTime (1) Brian, Maybe it is more simple at least for me, not somebody from an English culture), to use New Datetime. dim a (as DateTime for older versions) = new DateTime(now.Year, now.Month, Now, Day, 8, 0, 0) If a < Now then it is o'clcok Cor not really sure what you mean there. . I have dtmTime defined as datetime and when I do immed on it. . is evaluating to > than midnight Isn't everything failed because no accessible '> ' can be called with these arguments: 'Public Shared Operator > (t1 As Date, t2 As Date) As Boolean': Value of type 'System.TimeSpan' cannot be converted to 'Date'. 'Public Shared Operator
for loop??? Hallo can someone please help me, I've got this table: Valuation Date Construction 30 / 03 / 2009 30, 625.26 30 / 04 / 2009 269, 825.04 30 / 05 2011 146, 909, 564.95 Calculated from this formula: [code] public string Calculation(double b, DateTime validation, DateTime start, DateTime end, double alpha, double beta) { / / start date(t1) System.TimeSpan sp_start = validation.Subtract(start); string sp_start_str = sp_start.TotalDays.ToString("#"); double t1 = double.Parse(sp_start_str); / / change if (validation.Month = = 12) t1 = t1 - 14; if (validation.Month = = 01) t1 = t1 - 28; / / end date(t2) System.TimeSpan sp_end = end.Subtract(start); string sp_end_str = sp_end.TotalDays.ToString("#"); double t2 = double.Parse(sp_end_str); t2
date format hi. . . How can I find start date and end date of a month in c#? eg 1 / 4 / 2011 to30 / 4 / 2011 pl send me Collections.Generic; using System.Text; namespace Console_DateTime { class Program { static void Main( string [] args) { System. DateTime dtTodayNoon = new System. DateTime (2006, 9, 13, 12, 0, 0); System. DateTime dtYestMidnight = new System. DateTime (2006, 9, 12, 0, 0, 0); System. TimeSpan diffResult = dtTodayNoon - dtYestMidnight; Console .WriteLine( "Yesterday Midnight - Today Noon = " + diffResult.Days); Console .WriteLine( "Yesterday Midnight
How to subtract the current date and date in database. Example, I have a test which falls on 15 / 4 / 2011.I want to I edit the current codes given below. protected void Timer1_Tick(object sender, EventArgs e) { DateTime d1 = DateTime.Now; / / DateTime d2 = Convert.ToDateTime(""); DateTime d2 = Convert.ToDateTime("GetSQLDate"); d2.AddDays(-1); TimeSpan t = d1 - d2; int timediff = t.Days; if (timediff = = 1) { Response.Redirect("example.aspx"); } } public static DateTime GetSQLDate(string id) { string query = "SELECT date FROM examtimetable WHERE subject_id = '" + id + "'"; DateTime d = new