Calculating Bank holidays between two given dates and showing difference in minutes - c#

I am trying to calculate the no of bank holidays in between two given dates for example 31/10/2019 10:00:00 and 01/11/2019 08:00:00 assuming 01/11/2019 is a bank holiday. I should get the bank holiday difference in minutes as 480 minutes in total. I am using the following code where if the one of given date falls on same date as bank holiday then my code is returning 1440 which is wrong, Can someone please suggest how to achieve this. Thanks
DateTime firstDay = new DateTime(2019, 10, 31, 10, 0, 0);
DateTime lastDay = new DateTime(2019, 11, 1, 8, 0, 0);
public static int CountOfBusinessClosures(DateTime firstDay, DateTime lastDay, IOrganizationService service)
{
// firstDay = firstDay.Date;
// lastDay = lastDay.Date;
var count = 0;
var businessClosuresdate = new DateTime(2019, 11, 1, 0, 0, 0);//GetBusinessClosureCalendarRules(service);
// Count the number of bank holidays in between the given dates
if (firstDay <= businessClosuresdate && businessClosuresdate <= lastDay)
{
count++;
}
return count;
}
OR
public static double CountOfBusinessClosures2(DateTime firstDay, DateTime lastDay, IOrganizationService service)
{
var firstDay1 = firstDay.Date;
var lastDay1 = lastDay.Date;
var count = 0;
double t = 0;
// var businessClosures = GetBusinessClosureCalendarRules(service);
// Count the number of bank holidays during the time interval
// foreach (var closure in businessClosures)
// {
var startDate = 01/11/2019 00:00:00 //(DateTime)closure["effectiveintervalstart"];
var endDate = 02/11/2019 00:00:00 // (DateTime)closure["effectiveintervalend"];
DateTime bh1 = startDate.Date;
if (firstDay1 <= bh1 && bh1 <= lastDay1)
{
// count++;
if (firstDay.Date < startDate.Date && startDate.Date != endDate.Date)
{
t += 1440; // 24h * 60 min
}
else
{
double difference;
difference = (endDate - startDate).TotalMinutes;
difference = difference - (firstDay - startDate).TotalMinutes;
t += difference;
}
}
// }
return t;
}
private static IEnumerable<Entity> GetBusinessClosureCalendarRules(IOrganizationService service)
{
// Get Organization Business Closure Calendar Id
var organization = service.Retrieve("organization", _orgId, new ColumnSet("businessclosurecalendarid"));
var query = new QueryExpression("calendar")
{
ColumnSet = new ColumnSet(true),
Criteria = new FilterExpression()
};
// Add condition to get Get Calander where CalanderId is equal to Organization's businessclosurecalendarid
query.Criteria.AddCondition(new ConditionExpression("calendarid", ConditionOperator.Equal, organization["businessclosurecalendarid"].ToString()));
// Get Calendar
var businessClosureCalendar = service.RetrieveMultiple(query).Entities[0];
return businessClosureCalendar == null || businessClosureCalendar.GetAttributeValue<EntityCollection>("calendarrules") == null ? null : businessClosureCalendar.GetAttributeValue<EntityCollection>("calendarrules").Entities;
}

#Bellam Try this and see if it suits your needs:
// Unit Test Class
[TestClass]
public class UnitTest1
{
[TestMethod]
public void C58840237T01()
{
DateTime firstDay = new DateTime(2019, 10, 31, 0, 0, 0);
DateTime lastDay = new DateTime(2019, 11, 1, 0, 0, 0);
double totalMinutesBusinessIsClosed = C58840237.MinutesBusinessIsClosed(firstDay, lastDay);
Assert.IsTrue(totalMinutesBusinessIsClosed == 480);
}
}
// Sample Class
public class C58840237
{
public static double MinutesBusinessIsClosed(DateTime firstDay, DateTime lastDay)
{
double t = 0;
// Holiday 01
var h01Start = Convert.ToDateTime("11/01/2019 08:00:00");
var h01End = Convert.ToDateTime("11/01/2019 16:00:00");
// Holiday is in range
if (firstDay <= h01Start && h01Start >= lastDay)
{
return (h01End - h01Start).TotalMinutes;
}
return t;
}
}
EDIT: I realize our date time formats are different, but that's easily adjustable.

Related

Calculate biggest positive sequnces in numbers

I am trying to find the biggest positive changes in the account transactions. It starts with opening balance and keep changing based on spending and deposits.
We need to find in which two dates the account contains the biggest or highest positive cash flow. I am not able to calculate and my code fails. I think my logic is wrong it gives a wrong output. Because between 8 Dec - 10 Dec account seen highest deposit or positive changes
12/11/2015 12:00:00 AM, 12/12/2015 12:00:00 AM, 23000
Instead
12/8/2015 12:00:00 AM, 12/10/2015 12:00:00 AM, 10000
dotnet fiddle
Below is the code
using System;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
var transactions = new List<Transaction>()
{
new Transaction() {Date = new DateTime(2015, 12, 04), Balance = -4000}, // Open with negative 4000
new Transaction() {Date = new DateTime(2015, 12, 05), Balance = 2000}, // Settled 6000, so balance 2000
new Transaction() {Date = new DateTime(2015, 12, 06), Balance = 0}, // Spent 2000
new Transaction() {Date = new DateTime(2015, 12, 07), Balance = 2000}, // Deposited 2000
new Transaction() {Date = new DateTime(2015, 12, 08), Balance = -5000}, // Spent 7000
new Transaction() {Date = new DateTime(2015, 12, 09), Balance = 0}, // Deposited 5000
new Transaction() {Date = new DateTime(2015, 12, 10), Balance = 5000}, // Deposited 5000
new Transaction() {Date = new DateTime(2015, 12, 11), Balance = 1000}, // Spent 4000
new Transaction() {Date = new DateTime(2015, 12, 12), Balance = 6000}, // Deposited 5000
};
var (start, end, biggestAmountChangePositive) = GetBiggestBalanceChangeInPositive(transactions);
Console.WriteLine(start); //2015, 12, 08
Console.WriteLine(end); //2015, 12, 10
Console.WriteLine(biggestAmountChangePositive); //10000
}
public static (DateTime? start, DateTime? end, decimal biggestAmountChangePositive) GetBiggestBalanceChangeInPositive(List<Transaction> transactions)
{
decimal biggestAmountChangePositive = 0;
DateTime? startDate = null;
DateTime? endDate = null;
for (var i = 1; i < transactions.Count; i++)
{
if (transactions[i].Balance > transactions[i - 1].Balance)
{
var change = Math.Abs(transactions[i - 1].Balance - transactions[i].Balance);
biggestAmountChangePositive = biggestAmountChangePositive + change;
startDate = transactions[i - 1].Date;
endDate = transactions[i].Date;
}
}
return (startDate, endDate, biggestAmountChangePositive);
}
}
public class Transaction
{
public DateTime Date { get; set; }
public decimal Balance { get; set; }
}
I made what you asked.
I created one method and one class to remove some code.
public static (DateTime? start, DateTime? end, decimal highestPositiveBalanceChange) GetBiggestBalanceChangeInPositive(List<Transaction> transactions)
{
DateTime? startDate = null;
DateTime? endDate = null;
var highestPositiveBalanceChange = decimal.MinValue;
DateTime? tempStart = transactions[0].Date;
DateTime? tempEnd = transactions[0].Date;
var tempLast = transactions[0].Balance;
decimal tempHighestPositiveBalanceChange = 0;
for (var index = 1; index < transactions.Count; index++)
{
var transaction = transactions[index];
if (transaction.Balance >= tempLast)
{
tempHighestPositiveBalanceChange += transaction.Balance - tempLast;
tempLast = transaction.Balance;
tempEnd = transaction.Date;
}
else
{
if (tempHighestPositiveBalanceChange > highestPositiveBalanceChange)
{
highestPositiveBalanceChange = tempHighestPositiveBalanceChange;
startDate = tempStart;
endDate = tempEnd;
}
tempStart = transaction.Date;
tempEnd = transaction.Date;
tempLast = transaction.Balance;
tempHighestPositiveBalanceChange = 0;
}
}
if (tempHighestPositiveBalanceChange > highestPositiveBalanceChange)
{
highestPositiveBalanceChange = tempHighestPositiveBalanceChange;
startDate = tempStart;
endDate = tempEnd;
}
return highestPositiveBalanceChange == 0 ? (null, null, 0) : (startDate, endDate, highestPositiveBalanceChange);
}
With this, you will get the 10000 that you wanted

Generate a list of weekly business dates between two dates excluding weekends and holidays

I want to generate a list of weekly business dates between two dates excluding weekends and holidays. I have managed to exclude the Weekends and created a routine for the holidays. Now I need to exclude the holidays. Below is my code.
using System;
using System.Collections.Generic;
using System.Linq;
namespace SeriesTest
{
class Program
{
public class BusinessWeekDays
{
public DateTime Monday;
public DateTime Sunday;
}
private static List<DateTime> Holidays = new List<DateTime>()
{
new DateTime(1, 1, 1), //New Year Day
new DateTime(1, 5, 1), //Labour Day
new DateTime(1, 7, 4), //Independence Day
new DateTime(1, 3, 1), //Martin Luther King Jr. Day
new DateTime(1, 3, 2), //Presidents Day
new DateTime(1, 12, 25), //Christmas
new DateTime(1, 5, 5), //Memorial Day
new DateTime(1, 9, 1), //Labor Day
new DateTime(1, 10, 2), //Columbus Day
new DateTime(1, 11, 4), //Columbus Day
};
private static bool IsHoliday(DateTime value, List<DateTime> holidays = null)
{
if (null == holidays)
holidays = Holidays;
return (value.DayOfWeek == DayOfWeek.Sunday) ||
(value.DayOfWeek == DayOfWeek.Saturday) ||
holidays.Any(holiday => holiday.Day == value.Day &&
holiday.Month == value.Month);
}
public static int BusinessDays(DateTime fromDate, DateTime toDate, List<DateTime> holidays = null)
{
int result = 0;
for (var date = fromDate;
date < toDate.Date;
date = date.AddDays(1))
if (!IsHoliday(date, holidays))
result += 1;
return result;
}
static void Main(string[] args)
{
var StartDate = DateTime.Parse("02/12/2019");
var SeriesEndDate = DateTime.Parse("12/31/2025");
var holidays = new List<DateTime>();
var firstMonday = Enumerable.Range(0, 7)
.SkipWhile(x => StartDate.AddDays(x).DayOfWeek != DayOfWeek.Monday)
.Select(x => StartDate.AddDays(x))
.First();
var ts = (SeriesEndDate - firstMonday);
var dates = new List<BusinessWeekDays>();
for (var i = 0; i < ts.Days; i += 7)
{
//Remove holidays. Weekend already removed here
if (BusinessDays(StartDate, SeriesEndDate, holidays) != 0)
{
dates.Add(new BusinessWeekDays { Monday = firstMonday.AddDays(i), Sunday = firstMonday.AddDays(i + 9) });
}
}
Console.WriteLine(dates);
}
}
}
It looks like you already have everything you need, but holidays is never set to your Holidays. Change the line below so that holidays is set to null. Then it will hit the null check in IsHoliday and be set properly.
var holidays = new List<DateTime>();
Should be:
var holidays = null;

Convert difference between start time and end time to hours. minutes and seconds

I'm trying to find the time duration between time range and convert it to hour, minutes and seconds. Hours should added should not get converted to 24 hour. if there is 50 hours 30 min and 30 seconds it should display as 50:30:30, If the minute duration exceeds 59 it should add to hours same in case of seconds.
Please see the complete code.Both methods are not giving the correct answer. Method GetSumOfDuration is considering 0.30 as 0.3 so its not giving the correct answer. method GetSumOfDurationFromSecond fails when there is more than 24 hours difference.
using System;
using System.Collections.Generic;
using System.Linq;
namespace TimeCalculationApp
{
public class ProcessDetail
{
public DateTime? StartTime { get; set; }
public DateTime? EndTime { get; set; }
public double? DurationInSeconds
{
get
{
if (StartTime != null && EndTime != null)
return (EndTime.Value - StartTime.Value).TotalSeconds;
return null;
}
}
public double? Duration
{
get
{
if (StartTime != null && EndTime != null)
return double.Parse(DateTimeHelper.GetTimeDuration(StartTime, EndTime));
return null;
}
}
}
class DateTimeHelper
{
public static string GetTimeDuration(DateTime? sDateTime, DateTime? eDateTime)
{
if (sDateTime != null && eDateTime != null)
{
if (sDateTime.Value <= eDateTime.Value)
{
var tsdiff = eDateTime.Value - sDateTime.Value;
var ts = TimeSpan.FromSeconds(tsdiff.TotalSeconds);
return string.Format("{0}.{1}", ts.Hours.ToString("0"), ts.Minutes.ToString("D2"));
}
return "0.00";
}
return "0.00";
}
public static string GetSumOfDuration(List<ProcessDetail> processDetails)
{
TimeSpan? sumDuration = null;
var totalhours = 0;
var totalminutes = 0;
foreach (var processDetail in processDetails)
{
if (processDetail.Duration != null)
{
var splitString = processDetail.Duration.ToString().Split('.');
var hour = splitString.Length > 0 ? splitString[0] : "0";
var minutes = splitString.Length > 1 ? splitString[1] : "0";
totalhours += int.Parse(hour);
totalminutes += int.Parse(minutes);
sumDuration = new TimeSpan(totalhours, totalminutes, 0);
}
}
return sumDuration == null
? null
: Math.Floor(sumDuration.Value.TotalHours).ToString("0") + ":" +
sumDuration.Value.Minutes.ToString("D2");
}
public static string GetSumOfDurationFromSecond(List<ProcessDetail> processDetails, bool includeSeconds = false)
{
var seconds = processDetails.Where(x => x.DurationInSeconds != null).Sum(x => x.DurationInSeconds);
if (seconds == null) return null;
var ts = TimeSpan.FromSeconds(seconds.Value);
return includeSeconds ? string.Format("{0}:{1}:{2}", ts.Hours.ToString("0"), ts.Minutes.ToString("D2"), ts.Seconds.ToString("D2")) :
string.Format("{0}:{1}", ts.Hours.ToString("0"), ts.Minutes.ToString("D2"));
}
}
class Program
{
static void Main(string[] args)
{
var processDetails = GetProcessDetails();
var duration = DateTimeHelper.GetSumOfDuration(processDetails);
var durationFromSeconds = DateTimeHelper.GetSumOfDurationFromSecond(processDetails);
Console.WriteLine(duration);
Console.WriteLine(durationFromSeconds);
processDetails = GetProcessDetailsTestData();
duration = DateTimeHelper.GetSumOfDuration(processDetails);
durationFromSeconds = DateTimeHelper.GetSumOfDurationFromSecond(processDetails);
Console.WriteLine(duration);
Console.WriteLine(durationFromSeconds);
Console.ReadKey();
}
private static List<ProcessDetail> GetProcessDetails()
{
return new List<ProcessDetail>
{
new ProcessDetail
{
StartTime = new DateTime(2017, 01, 01, 1, 0, 0),
EndTime = new DateTime(2017, 01, 01, 7, 59, 0)
},
new ProcessDetail
{
StartTime = new DateTime(2017, 01, 01, 1, 0, 0),
EndTime = new DateTime(2017, 01, 01, 17, 3, 45)
},
new ProcessDetail
{
StartTime = new DateTime(2017, 01, 01, 1, 0, 0),
EndTime = new DateTime(2017, 01, 01, 10, 0, 20)
},
new ProcessDetail
{
StartTime = new DateTime(2017, 01, 01, 1, 0, 0),
EndTime = new DateTime(2017, 01, 01, 15, 1, 12)
}
};
}
private static List<ProcessDetail> GetProcessDetailsTestData()
{
return new List<ProcessDetail>
{
new ProcessDetail
{
StartTime = DateTime.Parse("11/01/2017 06:36:28"),
EndTime = DateTime.Parse("11/01/2017 06:53:51")
},
new ProcessDetail
{
StartTime = DateTime.Parse("11/01/2017 09:12:46"),
EndTime = DateTime.Parse("11/01/2017 09:43:00")
},
new ProcessDetail
{
StartTime =DateTime.Parse("11/01/2017 15:29:25"),
EndTime = DateTime.Parse("11/01/2017 15:37:26")
},
new ProcessDetail
{
StartTime = DateTime.Parse("11/01/2017 15:19:19"),
EndTime = DateTime.Parse("11/01/2017 15:27:52")
},
new ProcessDetail
{
StartTime = DateTime.Parse("12/01/2017 01:05:43"),
EndTime = DateTime.Parse("12/01/2017 01:08:37")
}
};
}
}
}
This should give the total hours between the dates.
DateTime dt1 = new DateTime(2016, 1, 1);
DateTime dt2 = new DateTime(2016, 1, 5);
TimeSpan difference = dt2 - dt1;
int totalDaysToHours = difference.Days * 24;
Console.WriteLine((difference.Hours + totalDaysToHours) + " hours " + difference.Minutes + " Minutes " + difference.Seconds + " seconds" );
Console.ReadKey();
You can fix a time span of the difference by subtract actions.
TimeSpan time1 = TimeSpan.Parse(date1);
TimeSpan time2 = TimeSpan.Parse(date2);
TimeSpan difference = time1 - time2;
int hours = difference.Hours;
int minutes = difference.Minutes;
You can just substract the starttime from the endtime.
The result will be a Timespan object.
// Set startime example to one hour ten minutes and 30 seconds ago
DateTime startTime = DateTime.Now.AddSeconds(-4230);
DateTime endTime = DateTime.Now;
TimeSpan elapsedTime = endTime-startTime;
// Print out the elapsed time in hour:minutes:seconds to string
string result = elapsedTime.TotalHours + ":" + elapsedTime.Minutes + ":" + elapsedTime.Seconds
Be aware that TimeSpan provides Properties for e.g Hours and TotalHours. Hours can have a value between 0-23 (when the value would exceed 23 the property Days is increased instead) whereas TotalHours provides the total number of full hours
I agree with Thomas Voß that you should use Timespan. However TotalHours should be casted to int to get proper number of hours.
string result = (int)elapsedTime.TotalHours + ":" + elapsedTime.Minutes + ":" + elapsedTime.Seconds
If you can consider displaying the result in days format you could utilize TimeSpan Formatting :
elapsedTime.ToString("dd\.hh\:mm\:ss")
You can read more about custom TimeSpan Formatting here

Datediff without Weekends [duplicate]

In C#, how can I calculate the number of business (or weekdays) days between two dates?
I've had such a task before and I've got the solution.
I would avoid enumerating all days in between when it's avoidable, which is the case here. I don't even mention creating a bunch of DateTime instances, as I saw in one of the answers above. This is really waste of processing power. Especially in the real world situation, when you have to examine time intervals of several months.
See my code, with comments, below.
/// <summary>
/// Calculates number of business days, taking into account:
/// - weekends (Saturdays and Sundays)
/// - bank holidays in the middle of the week
/// </summary>
/// <param name="firstDay">First day in the time interval</param>
/// <param name="lastDay">Last day in the time interval</param>
/// <param name="bankHolidays">List of bank holidays excluding weekends</param>
/// <returns>Number of business days during the 'span'</returns>
public static int BusinessDaysUntil(this DateTime firstDay, DateTime lastDay, params DateTime[] bankHolidays)
{
firstDay = firstDay.Date;
lastDay = lastDay.Date;
if (firstDay > lastDay)
throw new ArgumentException("Incorrect last day " + lastDay);
TimeSpan span = lastDay - firstDay;
int businessDays = span.Days + 1;
int fullWeekCount = businessDays / 7;
// find out if there are weekends during the time exceedng the full weeks
if (businessDays > fullWeekCount*7)
{
// we are here to find out if there is a 1-day or 2-days weekend
// in the time interval remaining after subtracting the complete weeks
int firstDayOfWeek = (int) firstDay.DayOfWeek;
int lastDayOfWeek = (int) lastDay.DayOfWeek;
if (lastDayOfWeek < firstDayOfWeek)
lastDayOfWeek += 7;
if (firstDayOfWeek <= 6)
{
if (lastDayOfWeek >= 7)// Both Saturday and Sunday are in the remaining time interval
businessDays -= 2;
else if (lastDayOfWeek >= 6)// Only Saturday is in the remaining time interval
businessDays -= 1;
}
else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7)// Only Sunday is in the remaining time interval
businessDays -= 1;
}
// subtract the weekends during the full weeks in the interval
businessDays -= fullWeekCount + fullWeekCount;
// subtract the number of bank holidays during the time interval
foreach (DateTime bankHoliday in bankHolidays)
{
DateTime bh = bankHoliday.Date;
if (firstDay <= bh && bh <= lastDay)
--businessDays;
}
return businessDays;
}
Edit by Slauma, August 2011
Great answer! There is little bug though. I take the freedom to edit this answer since the answerer is absent since 2009.
The code above assumes that DayOfWeek.Sunday has the value 7 which is not the case. The value is actually 0. It leads to a wrong calculation if for example firstDay and lastDay are both the same Sunday. The method returns 1 in this case but it should be 0.
Easiest fix for this bug: Replace in the code above the lines where firstDayOfWeek and lastDayOfWeek are declared by the following:
int firstDayOfWeek = firstDay.DayOfWeek == DayOfWeek.Sunday
? 7 : (int)firstDay.DayOfWeek;
int lastDayOfWeek = lastDay.DayOfWeek == DayOfWeek.Sunday
? 7 : (int)lastDay.DayOfWeek;
Now the result is:
Friday to Friday -> 1
Saturday to Saturday -> 0
Sunday to Sunday -> 0
Friday to Saturday -> 1
Friday to Sunday -> 1
Friday to Monday -> 2
Saturday to Monday -> 1
Sunday to Monday -> 1
Monday to Monday -> 1
Ok. I think it's time to post the right answer:
public static double GetBusinessDays(DateTime startD, DateTime endD)
{
double calcBusinessDays =
1 + ((endD - startD).TotalDays * 5 -
(startD.DayOfWeek - endD.DayOfWeek) * 2) / 7;
if (endD.DayOfWeek == DayOfWeek.Saturday) calcBusinessDays--;
if (startD.DayOfWeek == DayOfWeek.Sunday) calcBusinessDays--;
return calcBusinessDays;
}
Original Source:
http://alecpojidaev.wordpress.com/2009/10/29/work-days-calculation-with-c/
I know this question is already solved, but I thought I could provide a more straightforward-looking answer that may help other visitors in the future.
Here's my take at it:
public int GetWorkingDays(DateTime from, DateTime to)
{
var dayDifference = (int)to.Subtract(from).TotalDays;
return Enumerable
.Range(1, dayDifference)
.Select(x => from.AddDays(x))
.Count(x => x.DayOfWeek != DayOfWeek.Saturday && x.DayOfWeek != DayOfWeek.Sunday);
}
This was my original submission:
public int GetWorkingDays(DateTime from, DateTime to)
{
var totalDays = 0;
for (var date = from; date < to; date = date.AddDays(1))
{
if (date.DayOfWeek != DayOfWeek.Saturday
&& date.DayOfWeek != DayOfWeek.Sunday)
totalDays++;
}
return totalDays;
}
Define an Extension Method on DateTime like so:
public static class DateTimeExtensions
{
public static bool IsWorkingDay(this DateTime date)
{
return date.DayOfWeek != DayOfWeek.Saturday
&& date.DayOfWeek != DayOfWeek.Sunday;
}
}
Then, use is within a Where clause to filter a broader list of dates:
var allDates = GetDates(); // method which returns a list of dates
// filter dates by working day's
var countOfWorkDays = allDates
.Where(day => day.IsWorkingDay())
.Count() ;
I used the following code to also take in to account bank holidays:
public class WorkingDays
{
public List<DateTime> GetHolidays()
{
var client = new WebClient();
var json = client.DownloadString("https://www.gov.uk/bank-holidays.json");
var js = new JavaScriptSerializer();
var holidays = js.Deserialize <Dictionary<string, Holidays>>(json);
return holidays["england-and-wales"].events.Select(d => d.date).ToList();
}
public int GetWorkingDays(DateTime from, DateTime to)
{
var totalDays = 0;
var holidays = GetHolidays();
for (var date = from.AddDays(1); date <= to; date = date.AddDays(1))
{
if (date.DayOfWeek != DayOfWeek.Saturday
&& date.DayOfWeek != DayOfWeek.Sunday
&& !holidays.Contains(date))
totalDays++;
}
return totalDays;
}
}
public class Holidays
{
public string division { get; set; }
public List<Event> events { get; set; }
}
public class Event
{
public DateTime date { get; set; }
public string notes { get; set; }
public string title { get; set; }
}
And Unit Tests:
[TestClass]
public class WorkingDays
{
[TestMethod]
public void SameDayIsZero()
{
var service = new WorkingDays();
var from = new DateTime(2013, 8, 12);
Assert.AreEqual(0, service.GetWorkingDays(from, from));
}
[TestMethod]
public void CalculateDaysInWorkingWeek()
{
var service = new WorkingDays();
var from = new DateTime(2013, 8, 12);
var to = new DateTime(2013, 8, 16);
Assert.AreEqual(4, service.GetWorkingDays(from, to), "Mon - Fri = 4");
Assert.AreEqual(1, service.GetWorkingDays(from, new DateTime(2013, 8, 13)), "Mon - Tues = 1");
}
[TestMethod]
public void NotIncludeWeekends()
{
var service = new WorkingDays();
var from = new DateTime(2013, 8, 9);
var to = new DateTime(2013, 8, 16);
Assert.AreEqual(5, service.GetWorkingDays(from, to), "Fri - Fri = 5");
Assert.AreEqual(2, service.GetWorkingDays(from, new DateTime(2013, 8, 13)), "Fri - Tues = 2");
Assert.AreEqual(1, service.GetWorkingDays(from, new DateTime(2013, 8, 12)), "Fri - Mon = 1");
}
[TestMethod]
public void AccountForHolidays()
{
var service = new WorkingDays();
var from = new DateTime(2013, 8, 23);
Assert.AreEqual(0, service.GetWorkingDays(from, new DateTime(2013, 8, 26)), "Fri - Mon = 0");
Assert.AreEqual(1, service.GetWorkingDays(from, new DateTime(2013, 8, 27)), "Fri - Tues = 1");
}
}
I searched a lot for a, easy to digest, algorithm to calculate the working days between 2 dates, and also to exclude the national holidays, and finally I decide to go with this approach:
public static int NumberOfWorkingDaysBetween2Dates(DateTime start,DateTime due,IEnumerable<DateTime> holidays)
{
var dic = new Dictionary<DateTime, DayOfWeek>();
var totalDays = (due - start).Days;
for (int i = 0; i < totalDays + 1; i++)
{
if (!holidays.Any(x => x == start.AddDays(i)))
dic.Add(start.AddDays(i), start.AddDays(i).DayOfWeek);
}
return dic.Where(x => x.Value != DayOfWeek.Saturday && x.Value != DayOfWeek.Sunday).Count();
}
Basically I wanted to go with each date and evaluate my conditions:
Is not Saturday
Is not Sunday
Is not national holiday
but also I wanted to avoid iterating dates.
By running and measuring the time need it to evaluate 1 full year, I go the following result:
static void Main(string[] args)
{
var start = new DateTime(2017, 1, 1);
var due = new DateTime(2017, 12, 31);
var sw = Stopwatch.StartNew();
var days = NumberOfWorkingDaysBetween2Dates(start, due,NationalHolidays());
sw.Stop();
Console.WriteLine($"Total working days = {days} --- time: {sw.Elapsed}");
Console.ReadLine();
// result is:
// Total working days = 249-- - time: 00:00:00.0269087
}
Edit: a new method more simple:
public static int ToBusinessWorkingDays(this DateTime start, DateTime due, DateTime[] holidays)
{
return Enumerable.Range(0, (due - start).Days)
.Select(a => start.AddDays(a))
.Where(a => a.DayOfWeek != DayOfWeek.Sunday)
.Where(a => a.DayOfWeek != DayOfWeek.Saturday)
.Count(a => !holidays.Any(x => x == a));
}
This solution avoids iteration, works for both +ve and -ve weekday differences and includes a unit test suite to regression against the slower method of counting weekdays. I've also include a concise method to add weekdays also works in the same non-iterative way.
Unit tests cover a few thousand date combinations in order to exhaustively test all start/end weekday combinations with both small and large date ranges.
Important: We make the assumption that we are counting days by excluding the start date and including the end date. This is important when counting weekdays as the specific start/end days that you include/exclude affect the result. This also ensures that the difference between two equal days is always zero and that we only include full working days as typically you want the answer to be correct for any time on the current start date (often today) and include the full end date (e.g. a due date).
NOTE: This code needs an additional adjustment for holidays but in keeping with the above assumption, this code must exclude holidays on the start date.
Add weekdays:
private static readonly int[,] _addOffset =
{
// 0 1 2 3 4
{0, 1, 2, 3, 4}, // Su 0
{0, 1, 2, 3, 4}, // M 1
{0, 1, 2, 3, 6}, // Tu 2
{0, 1, 4, 5, 6}, // W 3
{0, 1, 4, 5, 6}, // Th 4
{0, 3, 4, 5, 6}, // F 5
{0, 2, 3, 4, 5}, // Sa 6
};
public static DateTime AddWeekdays(this DateTime date, int weekdays)
{
int extraDays = weekdays % 5;
int addDays = weekdays >= 0
? (weekdays / 5) * 7 + _addOffset[(int)date.DayOfWeek, extraDays]
: (weekdays / 5) * 7 - _addOffset[6 - (int)date.DayOfWeek, -extraDays];
return date.AddDays(addDays);
}
Compute weekday difference:
static readonly int[,] _diffOffset =
{
// Su M Tu W Th F Sa
{0, 1, 2, 3, 4, 5, 5}, // Su
{4, 0, 1, 2, 3, 4, 4}, // M
{3, 4, 0, 1, 2, 3, 3}, // Tu
{2, 3, 4, 0, 1, 2, 2}, // W
{1, 2, 3, 4, 0, 1, 1}, // Th
{0, 1, 2, 3, 4, 0, 0}, // F
{0, 1, 2, 3, 4, 5, 0}, // Sa
};
public static int GetWeekdaysDiff(this DateTime dtStart, DateTime dtEnd)
{
int daysDiff = (int)(dtEnd - dtStart).TotalDays;
return daysDiff >= 0
? 5 * (daysDiff / 7) + _diffOffset[(int) dtStart.DayOfWeek, (int) dtEnd.DayOfWeek]
: 5 * (daysDiff / 7) - _diffOffset[6 - (int) dtStart.DayOfWeek, 6 - (int) dtEnd.DayOfWeek];
}
I found that most other solutions on stack overflow were either slow (iterative) or overly complex and many were just plain incorrect. Moral of the story is ... Don't trust it unless you've exhaustively tested it!!
Unit tests based on NUnit Combinatorial testing and ShouldBe NUnit extension.
[TestFixture]
public class DateTimeExtensionsTests
{
/// <summary>
/// Exclude start date, Include end date
/// </summary>
/// <param name="dtStart"></param>
/// <param name="dtEnd"></param>
/// <returns></returns>
private IEnumerable<DateTime> GetDateRange(DateTime dtStart, DateTime dtEnd)
{
Console.WriteLine(#"dtStart={0:yy-MMM-dd ddd}, dtEnd={1:yy-MMM-dd ddd}", dtStart, dtEnd);
TimeSpan diff = dtEnd - dtStart;
Console.WriteLine(diff);
if (dtStart <= dtEnd)
{
for (DateTime dt = dtStart.AddDays(1); dt <= dtEnd; dt = dt.AddDays(1))
{
Console.WriteLine(#"dt={0:yy-MMM-dd ddd}", dt);
yield return dt;
}
}
else
{
for (DateTime dt = dtStart.AddDays(-1); dt >= dtEnd; dt = dt.AddDays(-1))
{
Console.WriteLine(#"dt={0:yy-MMM-dd ddd}", dt);
yield return dt;
}
}
}
[Test, Combinatorial]
public void TestGetWeekdaysDiff(
[Values(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 30)]
int startDay,
[Values(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 30)]
int endDay,
[Values(7)]
int startMonth,
[Values(7)]
int endMonth)
{
// Arrange
DateTime dtStart = new DateTime(2016, startMonth, startDay);
DateTime dtEnd = new DateTime(2016, endMonth, endDay);
int nDays = GetDateRange(dtStart, dtEnd)
.Count(dt => dt.DayOfWeek != DayOfWeek.Saturday && dt.DayOfWeek != DayOfWeek.Sunday);
if (dtEnd < dtStart) nDays = -nDays;
Console.WriteLine(#"countBusDays={0}", nDays);
// Act / Assert
dtStart.GetWeekdaysDiff(dtEnd).ShouldBe(nDays);
}
[Test, Combinatorial]
public void TestAddWeekdays(
[Values(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 30)]
int startDay,
[Values(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 20, 30)]
int weekdays)
{
DateTime dtStart = new DateTime(2016, 7, startDay);
DateTime dtEnd1 = dtStart.AddWeekdays(weekdays); // ADD
dtStart.GetWeekdaysDiff(dtEnd1).ShouldBe(weekdays);
DateTime dtEnd2 = dtStart.AddWeekdays(-weekdays); // SUBTRACT
dtStart.GetWeekdaysDiff(dtEnd2).ShouldBe(-weekdays);
}
}
Well this has been beaten to death. :) However I'm still going to provide another answer because I needed something a bit different. This solution is different in that it returns a Business TimeSpan between the start and end, and you can set the business hours of the day, and add holidays. So you can use it to calculate if it happens within a day, across days, over weekends, and even holidays. And you can get just the business days or not by just getting what you need from the returned TimeSpan object. And the way it uses lists of days, you can see how very easy it would be to add the list of non-work days if it's not the typical Sat and Sun.
And I tested for a year, and it seems super fast.
I just hope the pasting of the code is accurate. But I know it works.
public static TimeSpan GetBusinessTimespanBetween(
DateTime start, DateTime end,
TimeSpan workdayStartTime, TimeSpan workdayEndTime,
List<DateTime> holidays = null)
{
if (end < start)
throw new ArgumentException("start datetime must be before end datetime.");
// Just create an empty list for easier coding.
if (holidays == null) holidays = new List<DateTime>();
if (holidays.Where(x => x.TimeOfDay.Ticks > 0).Any())
throw new ArgumentException("holidays can not have a TimeOfDay, only the Date.");
var nonWorkDays = new List<DayOfWeek>() { DayOfWeek.Saturday, DayOfWeek.Sunday };
var startTime = start.TimeOfDay;
// If the start time is before the starting hours, set it to the starting hour.
if (startTime < workdayStartTime) startTime = workdayStartTime;
var timeBeforeEndOfWorkDay = workdayEndTime - startTime;
// If it's after the end of the day, then this time lapse doesn't count.
if (timeBeforeEndOfWorkDay.TotalSeconds < 0) timeBeforeEndOfWorkDay = new TimeSpan();
// If start is during a non work day, it doesn't count.
if (nonWorkDays.Contains(start.DayOfWeek)) timeBeforeEndOfWorkDay = new TimeSpan();
else if (holidays.Contains(start.Date)) timeBeforeEndOfWorkDay = new TimeSpan();
var endTime = end.TimeOfDay;
// If the end time is after the ending hours, set it to the ending hour.
if (endTime > workdayEndTime) endTime = workdayEndTime;
var timeAfterStartOfWorkDay = endTime - workdayStartTime;
// If it's before the start of the day, then this time lapse doesn't count.
if (timeAfterStartOfWorkDay.TotalSeconds < 0) timeAfterStartOfWorkDay = new TimeSpan();
// If end is during a non work day, it doesn't count.
if (nonWorkDays.Contains(end.DayOfWeek)) timeAfterStartOfWorkDay = new TimeSpan();
else if (holidays.Contains(end.Date)) timeAfterStartOfWorkDay = new TimeSpan();
// Easy scenario if the times are during the day day.
if (start.Date.CompareTo(end.Date) == 0)
{
if (nonWorkDays.Contains(start.DayOfWeek)) return new TimeSpan();
else if (holidays.Contains(start.Date)) return new TimeSpan();
return endTime - startTime;
}
else
{
var timeBetween = end - start;
var daysBetween = (int)Math.Floor(timeBetween.TotalDays);
var dailyWorkSeconds = (int)Math.Floor((workdayEndTime - workdayStartTime).TotalSeconds);
var businessDaysBetween = 0;
// Now the fun begins with calculating the actual Business days.
if (daysBetween > 0)
{
var nextStartDay = start.AddDays(1).Date;
var dayBeforeEnd = end.AddDays(-1).Date;
for (DateTime d = nextStartDay; d <= dayBeforeEnd; d = d.AddDays(1))
{
if (nonWorkDays.Contains(d.DayOfWeek)) continue;
else if (holidays.Contains(d.Date)) continue;
businessDaysBetween++;
}
}
var dailyWorkSecondsToAdd = dailyWorkSeconds * businessDaysBetween;
var output = timeBeforeEndOfWorkDay + timeAfterStartOfWorkDay;
output = output + new TimeSpan(0, 0, dailyWorkSecondsToAdd);
return output;
}
}
And here is test code: Note that you just have to put this function in a class called DateHelper for the test code to work.
[TestMethod]
public void TestGetBusinessTimespanBetween()
{
var workdayStart = new TimeSpan(8, 0, 0);
var workdayEnd = new TimeSpan(17, 0, 0);
var holidays = new List<DateTime>()
{
new DateTime(2018, 1, 15), // a Monday
new DateTime(2018, 2, 15) // a Thursday
};
var testdata = new[]
{
new
{
expectedMinutes = 0,
start = new DateTime(2016, 10, 19, 9, 50, 0),
end = new DateTime(2016, 10, 19, 9, 50, 0)
},
new
{
expectedMinutes = 10,
start = new DateTime(2016, 10, 19, 9, 50, 0),
end = new DateTime(2016, 10, 19, 10, 0, 0)
},
new
{
expectedMinutes = 5,
start = new DateTime(2016, 10, 19, 7, 50, 0),
end = new DateTime(2016, 10, 19, 8, 5, 0)
},
new
{
expectedMinutes = 5,
start = new DateTime(2016, 10, 19, 16, 55, 0),
end = new DateTime(2016, 10, 19, 17, 5, 0)
},
new
{
expectedMinutes = 15,
start = new DateTime(2016, 10, 19, 16, 50, 0),
end = new DateTime(2016, 10, 20, 8, 5, 0)
},
new
{
expectedMinutes = 10,
start = new DateTime(2016, 10, 19, 16, 50, 0),
end = new DateTime(2016, 10, 20, 7, 55, 0)
},
new
{
expectedMinutes = 5,
start = new DateTime(2016, 10, 19, 17, 10, 0),
end = new DateTime(2016, 10, 20, 8, 5, 0)
},
new
{
expectedMinutes = 0,
start = new DateTime(2016, 10, 19, 17, 10, 0),
end = new DateTime(2016, 10, 20, 7, 5, 0)
},
new
{
expectedMinutes = 545,
start = new DateTime(2016, 10, 19, 12, 10, 0),
end = new DateTime(2016, 10, 20, 12, 15, 0)
},
// Spanning multiple weekdays
new
{
expectedMinutes = 835,
start = new DateTime(2016, 10, 19, 12, 10, 0),
end = new DateTime(2016, 10, 21, 8, 5, 0)
},
// Spanning multiple weekdays
new
{
expectedMinutes = 1375,
start = new DateTime(2016, 10, 18, 12, 10, 0),
end = new DateTime(2016, 10, 21, 8, 5, 0)
},
// Spanning from a Thursday to a Tuesday, 5 mins short of complete day.
new
{
expectedMinutes = 1615,
start = new DateTime(2016, 10, 20, 12, 10, 0),
end = new DateTime(2016, 10, 25, 12, 5, 0)
},
// Spanning from a Thursday to a Tuesday, 5 mins beyond complete day.
new
{
expectedMinutes = 1625,
start = new DateTime(2016, 10, 20, 12, 10, 0),
end = new DateTime(2016, 10, 25, 12, 15, 0)
},
// Spanning from a Friday to a Monday, 5 mins beyond complete day.
new
{
expectedMinutes = 545,
start = new DateTime(2016, 10, 21, 12, 10, 0),
end = new DateTime(2016, 10, 24, 12, 15, 0)
},
// Spanning from a Friday to a Monday, 5 mins short complete day.
new
{
expectedMinutes = 535,
start = new DateTime(2016, 10, 21, 12, 10, 0),
end = new DateTime(2016, 10, 24, 12, 5, 0)
},
// Spanning from a Saturday to a Monday, 5 mins short complete day.
new
{
expectedMinutes = 245,
start = new DateTime(2016, 10, 22, 12, 10, 0),
end = new DateTime(2016, 10, 24, 12, 5, 0)
},
// Spanning from a Saturday to a Sunday, 5 mins beyond complete day.
new
{
expectedMinutes = 0,
start = new DateTime(2016, 10, 22, 12, 10, 0),
end = new DateTime(2016, 10, 23, 12, 15, 0)
},
// Times within the same Saturday.
new
{
expectedMinutes = 0,
start = new DateTime(2016, 10, 22, 12, 10, 0),
end = new DateTime(2016, 10, 23, 12, 15, 0)
},
// Spanning from a Saturday to the Sunday next week.
new
{
expectedMinutes = 2700,
start = new DateTime(2016, 10, 22, 12, 10, 0),
end = new DateTime(2016, 10, 30, 12, 15, 0)
},
// Spanning a year.
new
{
expectedMinutes = 143355,
start = new DateTime(2016, 10, 22, 12, 10, 0),
end = new DateTime(2017, 10, 30, 12, 15, 0)
},
// Spanning a year with 2 holidays.
new
{
expectedMinutes = 142815,
start = new DateTime(2017, 10, 22, 12, 10, 0),
end = new DateTime(2018, 10, 30, 12, 15, 0)
},
};
foreach (var item in testdata)
{
Assert.AreEqual(item.expectedMinutes,
DateHelper.GetBusinessTimespanBetween(
item.start, item.end,
workdayStart, workdayEnd,
holidays)
.TotalMinutes);
}
}
Here's some code for that purpose, with swedish holidays but you can adapt what holidays to count. Note that I added a limit you might want to remove, but it was for a web-based system and I didnt want anyone to enter some huge date to hog the process
public static int GetWorkdays(DateTime from ,DateTime to)
{
int limit = 9999;
int counter = 0;
DateTime current = from;
int result = 0;
if (from > to)
{
DateTime temp = from;
from = to;
to = temp;
}
if (from >= to)
{
return 0;
}
while (current <= to && counter < limit)
{
if (IsSwedishWorkday(current))
{
result++;
}
current = current.AddDays(1);
counter++;
}
return result;
}
public static bool IsSwedishWorkday(DateTime date)
{
return (!IsSwedishHoliday(date) && date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday);
}
public static bool IsSwedishHoliday(DateTime date)
{
return (
IsSameDay(GetEpiphanyDay(date.Year), date) ||
IsSameDay(GetMayDay(date.Year), date) ||
IsSameDay(GetSwedishNationalDay(date.Year), date) ||
IsSameDay(GetChristmasDay(date.Year), date) ||
IsSameDay(GetBoxingDay(date.Year), date) ||
IsSameDay(GetGoodFriday(date.Year), date) ||
IsSameDay(GetAscensionDay(date.Year), date) ||
IsSameDay(GetAllSaintsDay(date.Year), date) ||
IsSameDay(GetMidsummersDay(date.Year), date) ||
IsSameDay(GetPentecostDay(date.Year), date) ||
IsSameDay(GetEasterMonday(date.Year), date) ||
IsSameDay(GetNewYearsDay(date.Year), date) ||
IsSameDay(GetEasterDay(date.Year), date)
);
}
// Trettondagen
public static DateTime GetEpiphanyDay(int year)
{
return new DateTime(year, 1, 6);
}
// Första maj
public static DateTime GetMayDay(int year)
{
return new DateTime(year,5,1);
}
// Juldagen
public static DateTime GetSwedishNationalDay(int year)
{
return new DateTime(year, 6, 6);
}
// Juldagen
public static DateTime GetNewYearsDay(int year)
{
return new DateTime(year,1,1);
}
// Juldagen
public static DateTime GetChristmasDay(int year)
{
return new DateTime(year,12,25);
}
// Annandag jul
public static DateTime GetBoxingDay(int year)
{
return new DateTime(year, 12, 26);
}
// Långfredagen
public static DateTime GetGoodFriday(int year)
{
return GetEasterDay(year).AddDays(-3);
}
// Kristi himmelsfärdsdag
public static DateTime GetAscensionDay(int year)
{
return GetEasterDay(year).AddDays(5*7+4);
}
// Midsommar
public static DateTime GetAllSaintsDay(int year)
{
DateTime result = new DateTime(year,10,31);
while (result.DayOfWeek != DayOfWeek.Saturday)
{
result = result.AddDays(1);
}
return result;
}
// Midsommar
public static DateTime GetMidsummersDay(int year)
{
DateTime result = new DateTime(year, 6, 20);
while (result.DayOfWeek != DayOfWeek.Saturday)
{
result = result.AddDays(1);
}
return result;
}
// Pingstdagen
public static DateTime GetPentecostDay(int year)
{
return GetEasterDay(year).AddDays(7 * 7);
}
// Annandag påsk
public static DateTime GetEasterMonday(int year)
{
return GetEasterDay(year).AddDays(1);
}
public static DateTime GetEasterDay(int y)
{
double c;
double n;
double k;
double i;
double j;
double l;
double m;
double d;
c = System.Math.Floor(y / 100.0);
n = y - 19 * System.Math.Floor(y / 19.0);
k = System.Math.Floor((c - 17) / 25.0);
i = c - System.Math.Floor(c / 4) - System.Math.Floor((c - k) / 3) + 19 * n + 15;
i = i - 30 * System.Math.Floor(i / 30);
i = i - System.Math.Floor(i / 28) * (1 - System.Math.Floor(i / 28) * System.Math.Floor(29 / (i + 1)) * System.Math.Floor((21 - n) / 11));
j = y + System.Math.Floor(y / 4.0) + i + 2 - c + System.Math.Floor(c / 4);
j = j - 7 * System.Math.Floor(j / 7);
l = i - j;
m = 3 + System.Math.Floor((l + 40) / 44);// month
d = l + 28 - 31 * System.Math.Floor(m / 4);// day
double days = ((m == 3) ? d : d + 31);
DateTime result = new DateTime(y, 3, 1).AddDays(days-1);
return result;
}
Works and without loops
This method doesn't use any loops and is actually quite simple. It expands the date range to full weeks since we know that each week has 5 business days. It then uses a lookup table to find the number of business days to subtract from the start and end to get the right result. I've expanded out the calculation to help show what's going on, but the whole thing could be condensed into a single line if needed.
Anyway, this works for me and so I thought I'd post it here in case it might help others. Happy coding.
Calculation
t : Total number of days between dates (1 if min = max)
a + b : Extra days needed to expand total to full weeks
k : 1.4 is number of weekdays per week, i.e., (t / 7) * 5
c : Number of weekdays to subtract from the total
m : A lookup table used to find the value of "c" for each day of the week
Culture
Code assumes a Monday to Friday work week. For other cultures, such as Sunday to Thursday, you'll need to offset the dates prior to calculation.
Method
public int Weekdays(DateTime min, DateTime max)
{
if (min.Date > max.Date) throw new Exception("Invalid date span");
var t = (max.AddDays(1).Date - min.Date).TotalDays;
var a = (int) min.DayOfWeek;
var b = 6 - (int) max.DayOfWeek;
var k = 1.4;
var m = new int[]{0, 0, 1, 2, 3, 4, 5};
var c = m[a] + m[b];
return (int)((t + a + b) / k) - c;
}
Here's a quick sample code. It's a class method, so will only work inside of your class. If you want it to be static, change the signature to private static (or public static).
private IEnumerable<DateTime> GetWorkingDays(DateTime sd, DateTime ed)
{
for (var d = sd; d <= ed; d = d.AddDays(1))
if (d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday)
yield return d;
}
This method creates a loop variable d, initializes it to the start day, sd, then increments by one day each iteration (d = d.AddDays(1)).
It returns the desired values using yield, which creates an iterator. The cool thing about iterators is that they don't hold all of the values of the IEnumerable in memory, only calling each one sequentially. This means that you can call this method from the dawn of time to now without having to worry about running out of memory.
This method returns the number of business days between two dates:
Here I use the DayOfWeek enum for checking weekends.
private static int BusinessDaysLeft(DateTime first, DateTime last)
{
var count = 0;
while (first.Date != last.Date)
{
if(first.DayOfWeek != DayOfWeek.Saturday && first.DayOfWeek != DayOfWeek.Sunday)
count++;
first = first.AddDays(1);
}
return count;
}
I think none of the above answers are actually correct. None of them solves all the special cases such as when the dates starts and ends on the middle of a weekend, when the date starts on a Friday and ends on next Monday, etc. On top of that, they all round the calculations to whole days, so if the start date is in the middle of a saturday for example, it will substract a whole day from the working days, giving wrong results...
Anyway, here is my solution that is quite efficient and simple and works for all cases. The trick is just to find the previous Monday for start and end dates, and then do a small compensation when start and end happens during the weekend:
public double WorkDays(DateTime startDate, DateTime endDate){
double weekendDays;
double days = endDate.Subtract(startDate).TotalDays;
if(days<0) return 0;
DateTime startMonday = startDate.AddDays(DayOfWeek.Monday - startDate.DayOfWeek).Date;
DateTime endMonday = endDate.AddDays(DayOfWeek.Monday - endDate.DayOfWeek).Date;
weekendDays = ((endMonday.Subtract(startMonday).TotalDays) / 7) * 2;
// compute fractionary part of weekend days
double diffStart = startDate.Subtract(startMonday).TotalDays - 5;
double diffEnd = endDate.Subtract(endMonday).TotalDays - 5;
// compensate weekenddays
if(diffStart>0) weekendDays -= diffStart;
if(diffEnd>0) weekendDays += diffEnd;
return days - weekendDays;
}
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DateTime start = new DateTime(2014, 1, 1);
DateTime stop = new DateTime(2014, 12, 31);
int totalWorkingDays = GetNumberOfWorkingDays(start, stop);
Console.WriteLine("There are {0} working days.", totalWorkingDays);
}
private static int GetNumberOfWorkingDays(DateTime start, DateTime stop)
{
TimeSpan interval = stop - start;
int totalWeek = interval.Days / 7;
int totalWorkingDays = 5 * totalWeek;
int remainingDays = interval.Days % 7;
for (int i = 0; i <= remainingDays; i++)
{
DayOfWeek test = (DayOfWeek)(((int)start.DayOfWeek + i) % 7);
if (test >= DayOfWeek.Monday && test <= DayOfWeek.Friday)
totalWorkingDays++;
}
return totalWorkingDays;
}
}
}
Here is the function which we can use to calculate business days between two date. I'm not using holiday list as it can vary accross country/region.
If we want to use it anyway we can take third argument as list of holiday and before incrementing count we should check that list does not contains d
public static int GetBussinessDaysBetweenTwoDates(DateTime StartDate, DateTime EndDate)
{
if (StartDate > EndDate)
return -1;
int bd = 0;
for (DateTime d = StartDate; d < EndDate; d = d.AddDays(1))
{
if (d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday)
bd++;
}
return bd;
}
By using marinjw library,
And solution provided by Alec Pojidaev i created below, this will skip public (you need to specify country) holidays:
Please Note that if you use actual DateTime and have hours present then you have to add .Date and best to reformat FromDate and ToDate as FromDate = FromDate.Date and ToDate = ToDate.Date
public static int GetProcessingTime(this DateTime? FromDate, DateTime? ToDate)
{
if (FromDate == null) return 0;
if (ToDate == null) return 0;
List<DateTime> Holidays = new List<DateTime>();
int startyear = FromDate.Value.Year;
int endyear = ToDate.Value.Year;
//loop years to extract all holidays
for(int i = startyear; i <= endyear; i++)
{
IList<DateTime> Temp = new PolandPublicHoliday().PublicHolidays(i);
Holidays.AddRange(Temp.ToList());
}
//exclude holidays outside of range
Holidays = Holidays.Where(x => x >= FromDate && x <= ToDate).ToList();
//exclude holidays that are set to be on sunday/saturday
Holidays = Holidays.Where(x => x.DayOfWeek != DayOfWeek.Sunday && x.DayOfWeek != DayOfWeek.Saturday).ToList();
//calculate date difference without sundays and saturdays Value need to be added as I am using nullable DateTime
double calcBusinessDays = 1 + ((FromDate.Value - ToDate.Value).TotalDays * 5 - (FromDate.Value.DayOfWeek - ToDate.Value.DayOfWeek) * 2) / 7;
if (FromDate.Value.DayOfWeek == DayOfWeek.Saturday) calcBusinessDays--;
if (ToDate.Value.DayOfWeek == DayOfWeek.Sunday) calcBusinessDays--;
//remove left holidays
if (Holidays!=null)
calcBusinessDays -= Holidays.Count;
return (int)calcBusinessDays;
}
I'll just share my solution. It worked for me, maybe I just don't notice/know that theres a bug.
I started by getting the first incomplete week if there's any.
a complete week was from sunday for saturday, so if the (int)_now.DayOfWeek was not 0(Sunday), the first week was incomplete.
I just subtract 1 to first weeks count for the first week's saturday then add it to new count;
Then I get the last incomplete week, then subtract 1 for it's sunday then add to new count.
Then finally, the number of complete weeks multiply by 5(weekdays) was added to new count.
public int RemoveNonWorkingDays(int numberOfDays){
int workingDays = 0;
int firstWeek = 7 - (int)_now.DayOfWeek;
if(firstWeek < 7){
if(firstWeek > numberOfDays)
return numberOfDays;
workingDays += firstWeek-1;
numberOfDays -= firstWeek;
}
int lastWeek = numberOfDays % 7;
if(lastWeek > 0){
numberOfDays -= lastWeek;
workingDays += lastWeek - 1;
}
workingDays += (numberOfDays/7)*5;
return workingDays;
}
I was having trouble finding a solid TSQL version of this code. Below is essentially a conversion of the C# code here with addition of the Holiday table which should be used to pre-calculate holidays.
CREATE TABLE dbo.Holiday
(
HolidayDt DATE NOT NULL,
Name NVARCHAR(50) NOT NULL,
IsWeekday BIT NOT NULL,
CONSTRAINT PK_Holiday PRIMARY KEY (HolidayDt)
)
GO
CREATE INDEX IDX_Holiday ON Holiday (HolidayDt, IsWeekday)
GO
CREATE function dbo.GetBusinessDays
(
#FirstDay datetime,
#LastDay datetime
)
RETURNS INT
AS
BEGIN
DECLARE #BusinessDays INT, #FullWeekCount INT
SELECT #FirstDay = CONVERT(DATETIME,CONVERT(DATE,#FirstDay))
, #LastDay = CONVERT(DATETIME,CONVERT(DATE,#LastDay))
IF #FirstDay > #LastDay
RETURN NULL;
SELECT #BusinessDays = DATEDIFF(DAY, #FirstDay, #LastDay) + 1
SELECT #FullWeekCount = #BusinessDays / 7;
-- find out if there are weekends during the time exceedng the full weeks
IF #BusinessDays > (#FullWeekCount * 7)
BEGIN
-- we are here to find out if there is a 1-day or 2-days weekend
-- in the time interval remaining after subtracting the complete weeks
DECLARE #firstDayOfWeek INT, #lastDayOfWeek INT;
SELECT #firstDayOfWeek = DATEPART(DW, #FirstDay), #lastDayOfWeek = DATEPART(DW, #LastDay);
IF #lastDayOfWeek < #firstDayOfWeek
SELECT #lastDayOfWeek = #lastDayOfWeek + 7;
IF #firstDayOfWeek <= 6
BEGIN
IF (#lastDayOfWeek >= 7) --Both Saturday and Sunday are in the remaining time interval
BEGIN
SELECT #BusinessDays = #BusinessDays - 2
END
ELSE IF #lastDayOfWeek>=6 --Only Saturday is in the remaining time interval
BEGIN
SELECT #BusinessDays = #BusinessDays - 1
END
END
ELSE IF #firstDayOfWeek <= 7 AND #lastDayOfWeek >=7 -- Only Sunday is in the remaining time interval
BEGIN
SELECT #BusinessDays = #BusinessDays - 1
END
END
-- subtract the weekends during the full weeks in the interval
DECLARE #Holidays INT;
SELECT #Holidays = COUNT(*)
FROM Holiday
WHERE HolidayDt BETWEEN #FirstDay AND #LastDay
AND IsWeekday = CAST(1 AS BIT)
SELECT #BusinessDays = #BusinessDays - (#FullWeekCount + #FullWeekCount) -- - #Holidays
RETURN #BusinessDays
END
int BusinessDayDifference(DateTime Date1, DateTime Date2)
{
int Sign = 1;
if (Date2 > Date1)
{
Sign = -1;
DateTime TempDate = Date1;
Date1 = Date2;
Date2 = TempDate;
}
int BusDayDiff = (int)(Date1.Date - Date2.Date).TotalDays;
if (Date1.DayOfWeek == DayOfWeek.Saturday)
BusDayDiff -= 1;
if (Date2.DayOfWeek == DayOfWeek.Sunday)
BusDayDiff -= 1;
int Week1 = GetWeekNum(Date1);
int Week2 = GetWeekNum(Date2);
int WeekDiff = Week1 - Week2;
BusDayDiff -= WeekDiff * 2;
foreach (DateTime Holiday in Holidays)
if (Date1 >= Holiday && Date2 <= Holiday)
BusDayDiff--;
BusDayDiff *= Sign;
return BusDayDiff;
}
private int GetWeekNum(DateTime Date)
{
return (int)(Date.AddDays(-(int)Date.DayOfWeek).Ticks / TimeSpan.TicksPerDay / 7);
}
Here is one very simple solution for this problem. We have starting date, end date and "for loop" for encreasing the day and calculating to see if it's a workday or a weekend by converting to string DayOfWeek.
class Program
{
static void Main(string[] args)
{
DateTime day = new DateTime();
Console.Write("Inser your end date (example: 01/30/2015): ");
DateTime endDate = DateTime.Parse(Console.ReadLine());
int numberOfDays = 0;
for (day = DateTime.Now.Date; day.Date < endDate.Date; day = day.Date.AddDays(1))
{
string dayToString = Convert.ToString(day.DayOfWeek);
if (dayToString != "Saturday" && dayToString != "Sunday") numberOfDays++;
}
Console.WriteLine("Number of working days (not including local holidays) between two dates is "+numberOfDays);
}
}
Based on the comment marked as answer and patch recommended , as well as -> This version wants to convert the Days to Business-Hours ... Considers Same day hours as well.
/// <summary>
/// Calculates number of business days, taking into account:
/// - weekends (Saturdays and Sundays)
/// - bank holidays in the middle of the week
/// </summary>
/// <param name="firstDay">First day in the time interval</param>
/// <param name="lastDay">Last day in the time interval</param>
/// <param name="bankHolidays">List of bank holidays excluding weekends</param>
/// <returns>Number of business hours during the 'span'</returns>
public static int BusinessHoursUntil(DateTime firstDay, DateTime lastDay, params DateTime[] bankHolidays)
{
var original_firstDay = firstDay;
var original_lastDay = lastDay;
firstDay = firstDay.Date;
lastDay = lastDay.Date;
if (firstDay > lastDay)
return -1; //// throw new ArgumentException("Incorrect last day " + lastDay);
TimeSpan span = lastDay - firstDay;
int businessDays = span.Days + 1;
int fullWeekCount = businessDays / 7;
// find out if there are weekends during the time exceedng the full weeks
if (businessDays > fullWeekCount * 7)
{
// we are here to find out if there is a 1-day or 2-days weekend
// in the time interval remaining after subtracting the complete weeks
int firstDayOfWeek = firstDay.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)firstDay.DayOfWeek;
int lastDayOfWeek = lastDay.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)lastDay.DayOfWeek;
if (lastDayOfWeek < firstDayOfWeek)
lastDayOfWeek += 7;
if (firstDayOfWeek <= 6)
{
if (lastDayOfWeek >= 7)// Both Saturday and Sunday are in the remaining time interval
businessDays -= 2;
else if (lastDayOfWeek >= 6)// Only Saturday is in the remaining time interval
businessDays -= 1;
}
else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7)// Only Sunday is in the remaining time interval
businessDays -= 1;
}
// subtract the weekends during the full weeks in the interval
businessDays -= fullWeekCount + fullWeekCount;
if (bankHolidays != null && bankHolidays.Any())
{
// subtract the number of bank holidays during the time interval
foreach (DateTime bankHoliday in bankHolidays)
{
DateTime bh = bankHoliday.Date;
if (firstDay <= bh && bh <= lastDay)
--businessDays;
}
}
int total_business_hours = 0;
if (firstDay.Date == lastDay.Date)
{//If on the same day, go granular with Hours from the Orginial_*Day values
total_business_hours = (int)(original_lastDay - original_firstDay).TotalHours;
}
else
{//Convert Business-Days to TotalHours
total_business_hours = (int)(firstDay.AddDays(businessDays).AddHours(firstDay.Hour) - firstDay).TotalHours;
}
return total_business_hours;
}
I just improved #Alexander and #Slauma answer to support a business week as a parameter, for cases where saturday is a business day, or even cases where there is just a couple of days of the week that are considered business days:
/// <summary>
/// Calculate the number of business days between two dates, considering:
/// - Days of the week that are not considered business days.
/// - Holidays between these two dates.
/// </summary>
/// <param name="fDay">First day of the desired 'span'.</param>
/// <param name="lDay">Last day of the desired 'span'.</param>
/// <param name="BusinessDaysOfWeek">Days of the week that are considered to be business days, if NULL considers monday, tuesday, wednesday, thursday and friday as business days of the week.</param>
/// <param name="Holidays">Holidays, if NULL, considers no holiday.</param>
/// <returns>Number of business days during the 'span'</returns>
public static int BusinessDaysUntil(this DateTime fDay, DateTime lDay, DayOfWeek[] BusinessDaysOfWeek = null, DateTime[] Holidays = null)
{
if (BusinessDaysOfWeek == null)
BusinessDaysOfWeek = new DayOfWeek[] { DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday };
if (Holidays == null)
Holidays = new DateTime[] { };
fDay = fDay.Date;
lDay = lDay.Date;
if (fDay > lDay)
throw new ArgumentException("Incorrect last day " + lDay);
int bDays = (lDay - fDay).Days + 1;
int fullWeekCount = bDays / 7;
int fullWeekCountMult = 7 - WeekDays.Length;
// Find out if there are weekends during the time exceedng the full weeks
if (bDays > (fullWeekCount * 7))
{
int fDayOfWeek = (int)fDay.DayOfWeek;
int lDayOfWeek = (int)lDay.DayOfWeek;
if (fDayOfWeek > lDayOfWeek)
lDayOfWeek += 7;
// If they are the same, we already covered it right before the Holiday subtraction
if (lDayOfWeek != fDayOfWeek)
{
// Here we need to see if any of the days between are considered business days
for (int i = fDayOfWeek; i <= lDayOfWeek; i++)
if (!WeekDays.Contains((DayOfWeek)(i > 6 ? i - 7 : i)))
bDays -= 1;
}
}
// Subtract the days that are not in WeekDays[] during the full weeks in the interval
bDays -= (fullWeekCount * fullWeekCountMult);
// Subtract the number of bank holidays during the time interval
bDays = bDays - Holidays.Select(x => x.Date).Count(x => fDay <= x && x <= lDay);
return bDays;
}
I believe this could be a simpler way:
public int BusinessDaysUntil(DateTime start, DateTime end, params DateTime[] bankHolidays)
{
int tld = (int)((end - start).TotalDays) + 1; //including end day
int not_buss_day = 2 * (tld / 7); //Saturday and Sunday
int rest = tld % 7; //rest.
if (rest > 0)
{
int tmp = (int)start.DayOfWeek - 1 + rest;
if (tmp == 6 || start.DayOfWeek == DayOfWeek.Sunday) not_buss_day++; else if (tmp > 6) not_buss_day += 2;
}
foreach (DateTime bankHoliday in bankHolidays)
{
DateTime bh = bankHoliday.Date;
if (!(bh.DayOfWeek == DayOfWeek.Saturday || bh.DayOfWeek == DayOfWeek.Sunday) && (start <= bh && bh <= end))
{
not_buss_day++;
}
}
return tld - not_buss_day;
}
Here's yet another idea - this method allows to specify any working week and holidays.
The idea here is that we find the core of the date range from the first first working day of the week to the last weekend day of the week. This enables us to calculate the whole weeks easily (without iterating over all of the dates). All we need to do then is to add the working days that fall before the start and end of this core range.
public static int CalculateWorkingDays(
DateTime startDate,
DateTime endDate,
IList<DateTime> holidays,
DayOfWeek firstDayOfWeek,
DayOfWeek lastDayOfWeek)
{
// Make sure the defined working days run contiguously
if (lastDayOfWeek < firstDayOfWeek)
{
throw new Exception("Last day of week cannot fall before first day of week!");
}
// Create a list of the days of the week that make-up the weekend by working back
// from the firstDayOfWeek and forward from lastDayOfWeek to get the start and end
// the weekend
var weekendStart = lastDayOfWeek == DayOfWeek.Saturday ? DayOfWeek.Sunday : lastDayOfWeek + 1;
var weekendEnd = firstDayOfWeek == DayOfWeek.Sunday ? DayOfWeek.Saturday : firstDayOfWeek - 1;
var weekendDays = new List<DayOfWeek>();
var w = weekendStart;
do {
weekendDays.Add(w);
if (w == weekendEnd) break;
w = (w == DayOfWeek.Saturday) ? DayOfWeek.Sunday : w + 1;
} while (true);
// Force simple dates - no time
startDate = startDate.Date;
endDate = endDate.Date;
// Ensure a progessive date range
if (endDate < startDate)
{
var t = startDate;
startDate = endDate;
endDate = t;
}
// setup some working variables and constants
const int daysInWeek = 7; // yeah - really!
var actualStartDate = startDate; // this will end up on startOfWeek boundary
var actualEndDate = endDate; // this will end up on weekendEnd boundary
int workingDaysInWeek = daysInWeek - weekendDays.Count;
int workingDays = 0; // the result we are trying to find
int leadingDays = 0; // the number of working days leading up to the firstDayOfWeek boundary
int trailingDays = 0; // the number of working days counting back to the weekendEnd boundary
// Calculate leading working days
// if we aren't on the firstDayOfWeek we need to step forward to the nearest
if (startDate.DayOfWeek != firstDayOfWeek)
{
var d = startDate;
do {
if (d.DayOfWeek == firstDayOfWeek || d >= endDate)
{
actualStartDate = d;
break;
}
if (!weekendDays.Contains(d.DayOfWeek))
{
leadingDays++;
}
d = d.AddDays(1);
} while(true);
}
// Calculate trailing working days
// if we aren't on the weekendEnd we step back to the nearest
if (endDate >= actualStartDate && endDate.DayOfWeek != weekendEnd)
{
var d = endDate;
do {
if (d.DayOfWeek == weekendEnd || d < actualStartDate)
{
actualEndDate = d;
break;
}
if (!weekendDays.Contains(d.DayOfWeek))
{
trailingDays++;
}
d = d.AddDays(-1);
} while(true);
}
// Calculate the inclusive number of days between the actualStartDate and the actualEndDate
var coreDays = (actualEndDate - actualStartDate).Days + 1;
var noWeeks = coreDays / daysInWeek;
// add together leading, core and trailing days
workingDays += noWeeks * workingDaysInWeek;
workingDays += leadingDays;
workingDays += trailingDays;
// Finally remove any holidays that fall within the range.
if (holidays != null)
{
workingDays -= holidays.Count(h => h >= startDate && (h <= endDate));
}
return workingDays;
}
Since I can't comment. There is one more issue with the accepted solution where bank holidays are subtracted even when they are situated in the weekend. Seeing how other input is checked, it is only fitting that this is as well.
The foreach should therefore be:
// subtract the number of bank holidays during the time interval
foreach (DateTime bankHoliday in bankHolidays)
{
DateTime bh = bankHoliday.Date;
// Do not subtract bank holidays when they fall in the weekend to avoid double subtraction
if (bh.DayOfWeek == DayOfWeek.Saturday || bh.DayOfWeek == DayOfWeek.Sunday)
continue;
if (firstDay <= bh && bh <= lastDay)
--businessDays;
}
Here is an approach if you are using MVC.
I have also calculated national holidays or any festive days to be excluded by fetching it from holidayscalendar which you will need to make one.
foreach (DateTime day in EachDay(model))
{
bool key = false;
foreach (LeaveModel ln in holidaycalendar)
{
if (day.Date == ln.Date && day.DayOfWeek != DayOfWeek.Saturday && day.DayOfWeek != DayOfWeek.Sunday)
{
key = true; break;
}
}
if (day.DayOfWeek == DayOfWeek.Saturday || day.DayOfWeek == DayOfWeek.Sunday)
{
key = true;
}
if (key != true)
{
leavecount++;
}
}
Leavemodel is a list here
Here is an helper function I wrote for that task. it also returns the count of weekends via the out parameter. if you wish you can customize the "weekend" days in run time for countries that use different weekend days or to include holidays trough the weekendDays[] optional parameter :
public static int GetNetworkDays(DateTime startDate, DateTime endDate,out int totalWeekenDays, DayOfWeek[] weekendDays = null)
{
if (startDate >= endDate)
{
throw new Exception("start date can not be greater then or equel to end date");
}
DayOfWeek[] weekends = new DayOfWeek[] { DayOfWeek.Sunday, DayOfWeek.Saturday };
if (weekendDays != null)
{
weekends = weekendDays;
}
var totaldays = (endDate - startDate).TotalDays + 1; // add one to include the first day to
var counter = 0;
var workdaysCounter = 0;
var weekendsCounter = 0;
for (int i = 0; i < totaldays; i++)
{
if (weekends.Contains(startDate.AddDays(counter).DayOfWeek))
{
weekendsCounter++;
}
else
{
workdaysCounter++;
}
counter++;
}
totalWeekenDays = weekendsCounter;
return workdaysCounter;
}
I came up with the following solution
var dateStart = new DateTime(2019,01,10);
var dateEnd = new DateTime(2019,01,31);
var timeBetween = (dateEnd - dateStart).TotalDays + 1;
int numberOf7DayWeeks = (int)(timeBetween / 7);
int numberOfWeekendDays = numberOf7DayWeeks * 2;
int workingDays =(int)( timeBetween - numberOfWeekendDays);
if(dateStart.DayOfWeek == DayOfWeek.Saturday || dateEnd.DayOfWeek == DayOfWeek.Sunday){
workingDays -=2;
}
if(dateStart.DayOfWeek == DayOfWeek.Sunday || dateEnd.DayOfWeek == DayOfWeek.Saturday){
workingDays -=1;
}
You just have to iterate through each day in the time range and subtract a day from the counter if its a Saturday or a Sunday.
private float SubtractWeekend(DateTime start, DateTime end) {
float totaldays = (end.Date - start.Date).Days;
var iterationVal = totalDays;
for (int i = 0; i <= iterationVal; i++) {
int dayVal = (int)start.Date.AddDays(i).DayOfWeek;
if(dayVal == 6 || dayVal == 0) {
// saturday or sunday
totalDays--;
}
}
return totalDays;
}
public static int CalculateBusinessDaysInRange(this DateTime startDate, DateTime endDate, params DateTime[] holidayDates)
{
endDate = endDate.Date;
if(startDate > endDate)
throw new ArgumentException("The end date can not be before the start date!", nameof(endDate));
int accumulator = 0;
DateTime itterator = startDate.Date;
do
{
if(itterator.DayOfWeek != DayOfWeek.Saturday && itterator.DayOfWeek != DayOfWeek.Sunday && !holidayDates.Any(hol => hol.Date == itterator))
{ accumulator++; }
}
while((itterator = itterator.AddDays(1)).Date <= endDate);
return accumulator
}
I'm only posting this because despite all of the excellent answers that have been given, none of the math made sense to me. This is definitely a KISS method that should work and be fairly maintainable. Granted if you are calculating ranges that are greater than 2-3 months this will not be the most effective way. We simply determine if it is a Saturday or Sunday or the date is a given holiday date. If it's not we add a business day. If it is then everything is fine.
I'm sure this could be even more so simplified with LINQ, but this way is much easier to understand.

How to find out how many quarters there are beween one quarter and the other in c#

I've created a class called Kwartal (which translates into Quarter) for use in my program:
public sealed class Kwartal {
private DateTime _poczatekKwartalu;
private DateTime _koniecKwartalu;
private int _numerKwartalu;
private int _rok;
public Kwartal(int numer, DateTime dataod, DateTime datado) {
_numerKwartalu = numer;
_koniecKwartalu = datado;
_poczatekKwartalu = dataod;
_rok = dataod.Year;
}
public Kwartal() { }
public int Numer {
get { return _numerKwartalu; }
set { _numerKwartalu = value; }
}
public DateTime DataPoczatkowa {
get { return _poczatekKwartalu; }
set { _poczatekKwartalu = value; }
}
public DateTime DataKoncowa {
get { return _koniecKwartalu; }
set { _koniecKwartalu = value; }
}
public int Rok {
get { return _rok; }
set { _rok = value; }
}
}
It's basically definition for Quarter. Usually i define it like this:
Kwartal kwartal1 = new Kwartal(1, new DateTime(year, 1, 1), new DateTime(year, 3, 31));
Kwartal kwartal2 = new Kwartal(2, new DateTime(year, 4, 1), new DateTime(year, 6, 30));
Now i was wondering how I can do math on those. For example I've got Quarter1 in 2011 and i then have Quarter3 in 2012. I would like to find out how many quarters are there between Quarter1 and
Quarter3.
Like kwartal2 - kwartal1 = 5
Nowhere do you define the number of quarters in the year - without there being some property/constant set on the Kwartal object how can you know what the base value should be for your subtraction operation?
Once you've set that base value, the operation would be reasonably easy, you can create an absolute quarter count, e.g.
k.QuarterCount = (k1.year * kwartal.base) + k1.quarter
then you have an integer to substract from the other year.
From your answer, this is what I would do:
public static int zwrocRozniceMiedzyKwartalami(Kwartal kwartal1, Kwartal kwartal2) {
var quartersPerYear = 4;
var yearDifference = kwartal2.Rok - kwartal1.Rok;
var differenceQuarters = (yearDifference * quartersPerYear) + (kwartal2.Numer - kwartal1.Numer);
return differenceQuarters;
}
I think this would give you the following answers:
(Year1, Quarter1) - (Year2, Quarter2) = Difference
(2012, 2) - (2011, 1) = (2011 - 2012)*4 + (1 - 2) = -4 + (-1) = -5 => (2011, 1) is 5 quarters before (2012, 2)
(2014, 1) - (2018,3) = (2018 - 2014)*4 + (3 - 1) = 16 + 2 = 18 => (2018,3) is 18 months after (2014,1)
You can use the DateDiff class of the Time Period Library for .NET, respecting the calendar culture:
// ----------------------------------------------------------------------
public void DateDiffSample()
{
DateTime date1 = new DateTime( 2009, 11, 8, 7, 13, 59 );
Console.WriteLine( "Date1: {0}", date1 );
// > Date1: 08.11.2009 07:13:59
DateTime date2 = new DateTime( 2011, 3, 20, 19, 55, 28 );
Console.WriteLine( "Date2: {0}", date2 );
// > Date2: 20.03.2011 19:55:28
DateDiff dateDiff = new DateDiff( date1, date2 );
Console.WriteLine( "DateDiff.Quarters: {0}", dateDiff.Quarters );
// > DateDiff.Quarters: 5
} // DateDiffSample
First, you should define what the result of the calculation should be. Create a matrix/table with some data, and a lot of edge cases, and calculate by hand what the result should be.
Then, you could figure out an algorithm to use to calculate it. And basicly implement this algorithm. And test on your data you made up.
Oh, and then you start thinking about time-zones, daylight saving, etc. And you should read this blog entry from Jon Skeet.
I am not sure whether this is what you want to achieve
DateTime quarter1 = new DateTime(2010, 3, 31); //end date of Q1 as you capture DataKoncowa
DateTime quarter2 = new DateTime(2011, 3, 31);//end date of Q2 as you capture DataKoncowa
TimeSpan ts = quarter2 - quarter1; //kwartal2.DataKoncowa - kwartal1.DataKoncowa
int actualQuarters = ts.Days / (30 *3); //assuming quarter is 90 days
This returns 4 .. something like what you expect
If you convert the year / quarters to decimal, and do a little math, you can calculate the difference.
class DateTimeQuarter
{
public DateTimeQuarter(DateTime date)
{
Date = date;
Quarter = date.Month / 4 + 1;
}
public static int operator -(DateTimeQuarter lhs, DateTimeQuarter rhs)
{
double value = Convert.ToDouble(
(rhs.Date.Year - lhs.Date.Year)) + (rhs.Quarter / 10.0) - (rhs.Quarter / 10.0);
int result =
(Convert.ToInt32(value) * 4) + Convert.ToInt32(value - Math.Floor(value));
return result;
}
public DateTime Date { get; set; }
public int Quarter { get; set; }
}
static void Main(string[] args)
{
DateTimeQuarter q1 = new DateTimeQuarter(new DateTime(2006, 04, 20));
DateTimeQuarter q2 = new DateTimeQuarter(new DateTime(2007, 12, 25));
int quarters = q1 - q2;
}
This is how i ended up doing this. Seems to be working fine. If you've got a better way lemme know :-)
public static int zwrocRozniceMiedzyKwartalami(Kwartal kwartal1, Kwartal kwartal2) {
int quartersPerYear = 4;
int differenceQuarters;
int yearDifference = kwartal2.Rok - kwartal1.Rok;
if (yearDifference == 0) {
differenceQuarters = kwartal2.Numer - kwartal1.Numer;
return differenceQuarters;
} else if (yearDifference > 0) {
differenceQuarters = (yearDifference * quartersPerYear) + (kwartal2.Numer - kwartal1.Numer);
return differenceQuarters;
} else if (yearDifference < 0) {
return -1;
}
return -1;
}

Categories

Resources