How can I get DateTime.DaysInMonth without weekends? - c#

How do I get in C# the number of days in a month without Friday and Saturday?

Obligatory LINQ solution:
int days = Enumerable.Range( 1, DateTime.DaysInMonth( year, month ) )
.Select( day => new DateTime( year, month, day ) )
.Count( d => d.DayOfWeek != DayOfWeek.Saturday &&
d.DayOfWeek != DayOfWeek.Friday );

Here, quick and dirty:
class Program
{
static void Main(string[] args)
{
int month = DateTime.Today.Month;
int year = DateTime.Today.Year;
int daysInMonthMinusFridayAndSaturday = 0;
for (int i = 1; i <= DateTime.DaysInMonth(year,month); i++)
{
DateTime thisDay = new DateTime(year,month,i);
if(thisDay.DayOfWeek != DayOfWeek.Friday && thisDay.DayOfWeek != DayOfWeek.Saturday)
{
daysInMonthMinusFridayAndSaturday += 1;
}
}
Console.WriteLine(daysInMonthMinusFridayAndSaturday);
Console.ReadLine();
}
}

I would avoid loops or iterations and do it like this:
int GetWorkDays(int year, int month)
{
var firstDayOfMonth = new DateTime(year, month, 1).DayOfWeek;
var daysInMonth = DateTime.DaysInMonth(year, month);
// count whole weeks first
var wholeWeeks = daysInMonth / 7;
var extraDays = daysInMonth % 7;
// calculate the overlap of the "remainder days" with the weekend.
var lastDayOfMonth = (int) (firstDayOfMonth + extraDays - 1);
var overlapStart = Math.Max((int) firstDayOfMonth, (int) DayOfWeek.Friday);
var overlapEnd = Math.Min(lastDayOfMonth, (int) DayOfWeek.Saturday);
var weekendOverlap = Math.Max(0, overlapEnd - overlapStart + 1);
// substract weekend days
return daysInMonth - wholeWeeks * 2 - weekendOverlap;
}

Related

How to get the list of week start date (Monday) and end date (Sunday) for the year in C#

I want to get only weeks for the whole year where I want to get the start date (Monday) and end date (Friday) in C#.
For example: 1/52 = 02 Jan (Monday) - 09 Jan (Sunday) 2/52 = 10 Jan (Monday) - 17 Jan (Sunday)
and so on.
I can get current week dates but no idea how to get for the year.
// We Set the Monday as the first day of the week.
DayOfWeek day = datetime.DayOfWeek;
int days = day - DayOfWeek.Monday;
if (days == -1)
{
days = 6; // this is when we have sunday as a DayOfWeek day
}
DateTime start = datetime.AddDays(-days);
DateTime end = start.AddDays(6);
Without making it complicated you can simply use while like below.
while (datetime.DayOfWeek != DayOfWeek.Monday)
{
datetime= datetime.AddDays(1);
}
DateTime start = datetime;
DateTime end = start.AddDays(6);
Or you want to find week from the week index 1/52 for any year then write function like below. Use it like GetWeek(1, 2020) to get 06.01.2020 - 12.01.2020. Format it as per your requirement.
public DateTime GetNextMonday(DateTime datetime)
{
return datetime.AddDays((7 - (int)datetime.DayOfWeek + (int)DayOfWeek.Monday) % 7);
}
public string GetWeek(int week, int year)
{
var start = GetNextMonday(new DateTime(year, 1, 1).AddDays((week-1)*7));
var end = start.AddDays(6);
return start.ToShortDateString() + " - " + end.ToShortDateString();
}
As far as I have understood, probably this will help, I tried the below and it displayed for me the start and end dates for the specified years:
DateTime starting = new DateTime(2020, 1, 1);
DateTime ending = new DateTime(2020, 12, 1);
DateTime currentDay = starting;
DateTime start = currentDay;
DateTime end = currentDay;
while (ending.Year >= currentDay.Year)
{
if (currentDay.DayOfWeek == DayOfWeek.Monday)
{
start = currentDay;
end = start.AddDays(6);
currentDay = end;
Console.WriteLine(start + "(" + start.DayOfWeek + ")");
Console.WriteLine(end + "(" + end.DayOfWeek + ")");
}
else
{
currentDay = currentDay.AddDays(1);
}
}
You can use methods below to calculate start day of any week of any year
public static DateTime StartOfNthWeekOfYear(int year, int weekNumber, DayOfWeek firstDayOfWeek)
{
if(weekNumber < 1)
{
throw new ArgumentOutOfRangeException(nameof(weekNumber));
}
DateTime startOfWeek = StartOfFirstWeekOfYear(year, firstDayOfWeek).AddDays((weekNumber - 1) * 7);
DateTime endOfWeek = startOfWeek.AddDays(6);
if(endOfWeek.Year != year || startOfWeek.Year != year)
{
throw new ArgumentOutOfRangeException(nameof(weekNumber));
}
return startOfWeek;
}
public static DateTime StartOfFirstWeekOfYear(int year, DayOfWeek firstDayOfWeek)
{
DateTime startOfYear = new DateTime(year, 1, 1);
if (startOfYear.DayOfWeek != firstDayOfWeek)
{
return StartOfWeek(startOfYear, firstDayOfWeek).AddDays(7);
}
return startOfYear;
}
public static DateTime StartOfWeek(DateTime value, DayOfWeek firstDayOfWeek)
{
if (value.DayOfWeek != firstDayOfWeek)
{
return value.AddDays(-((7 + (int)value.DayOfWeek - (int)firstDayOfWeek) % 7));
}
return value;
}
I think this should work for Gregorian calendars and takes into account different cultures:
public static IList<DateTime> GetFirstDayOfWeekDates(CultureInfo cultureInfo, int year)
{
var lastDateOfYear = new DateTime(year, 12, 31);
var firstDate = new DateTime(year, 1, 1);
var dayOfWeek = cultureInfo.DateTimeFormat.FirstDayOfWeek;
while (firstDate.DayOfWeek != dayOfWeek)
{
firstDate = firstDate.AddDays(1);
}
var numberOfWeeksInYear = cultureInfo.Calendar.GetWeekOfYear(lastDateOfYear, cultureInfo.DateTimeFormat.CalendarWeekRule, dayOfWeek);
var firstDayOfWeekDates = new List<DateTime>();
firstDayOfWeekDates.Add(firstDate);
var currentDate = firstDate;
for (int i = 0; i < numberOfWeeksInYear; i++)
{
var weekLater = currentDate.AddDays(7);
if (weekLater.Year == year)
{
currentDate = weekLater;
firstDayOfWeekDates.Add(currentDate);
}
}
return firstDayOfWeekDates;
}
You can test this with a console app like this (make the method static):
static void Main(string[] args)
{
var ci = new CultureInfo("en-GB");
var dates = GetFirstDayOfWeekDates(ci, DateTime.Now.Year);
foreach (var dt in dates)
{
Console.WriteLine("Date: " + dt.ToShortDateString());
}
Console.ReadLine();
}
It brings back the following:
If you want to include the end date of the week as well then you can tweak this slightly by adding a new class called WeekDate:
public class WeekDate
{
public DateTime StartOfWeek { get; set; }
public DateTime EndOfWeek { get; set; }
}
GetFirstDayOfWeekDates then becomes:
public static IList<WeekDate> GetFirstDayOfWeekDates(CultureInfo cultureInfo, int year)
{
var lastDateOfYear = new DateTime(year, 12, 31);
var firstDate = new DateTime(year, 1, 1);
var dayOfWeek = cultureInfo.DateTimeFormat.FirstDayOfWeek;
while (firstDate.DayOfWeek != dayOfWeek)
{
firstDate = firstDate.AddDays(1);
}
var numberOfWeeksInYear = cultureInfo.Calendar.GetWeekOfYear(lastDateOfYear, cultureInfo.DateTimeFormat.CalendarWeekRule, dayOfWeek);
var firstDayOfWeekDates = new List<WeekDate>();
firstDayOfWeekDates.Add(new WeekDate { StartOfWeek = firstDate, EndOfWeek = firstDate.AddDays(6) });
var currentDate = firstDate;
for (int i = 0; i < numberOfWeeksInYear; i++)
{
var weekLater = currentDate.AddDays(7);
if (weekLater.Year == year)
{
currentDate = currentDate.AddDays(7);
firstDayOfWeekDates.Add(new WeekDate { StartOfWeek = currentDate, EndOfWeek = currentDate.AddDays(6) });
}
}
return firstDayOfWeekDates;
}
Which returns:

Getting last 7 days data from days name like sunday, monday, tuesday etc in linq asp.net mvc

I want to fetch data by last week days like last Sunday, last Monday and so on 7 days. I wrote this query but I returns null.
var dateCriteria = DateTime.Now.Date.AddDays(-7);
var one = _context.Sale.Where(m => m.Date >= dateCriteria && m.Date.DayOfWeek.ToString() ==
"Sunday");
DayOfWeek is enum. So just use it without conversion:
var dateCriteria = DateTime.Now.Date.AddDays(-7);
var one = _context.Sale.Where(m => m.Date >= dateCriteria && m.Date.DayOfWeek ==
DayOfWeek.Sunday);
I am not sure if I understood your question correctly but here is what I would do to get the last Sunday's sales.
var one = _context.Sale.Where(m => m.Date == GetLast(DayOfWeek.Sunday));
private DateTime GetLast(DayOfWeek dayOfWeek) {
var currentDate = DateTime.Now.Date;
var currentDayOfWeek = (int)currentDate.DayOfWeek;
if (currentDayOfWeek <= (int)dayOfWeek) {
currentDayOfWeek = currentDayOfWeek + 7;
}
int daysToExtract = currentDayOfWeek - (int)dayOfWeek;
return currentDate.AddDays(-daysToExtract);
}

Is there an optimized way of getting business weeks between two dates?

Just wondering if there is a more optimized and/or neater way (using LINQ for example) of writing what I have below to get a list of business week date ranges between two dates?
This is what I have currently ..
// Some storage
public class Bucket
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
// Other code removed for brevity ...
DateTime start = new DateTime(2015, 7, 1);
DateTime end = new DateTime(2015, 9, 1);
DayOfWeek firstDayOfWeek = DayOfWeek.Monday;
DayOfWeek lastDayOfWeek = DayOfWeek.Friday;
var buckets = new List<Bucket>();
var currentDate = start;
DateTime startOfBucket = currentDate;
DateTime endOfBucket = currentDate;
while (currentDate <= end)
{
var currentDayOfWeek = currentDate.DayOfWeek;
// Skip days outside the business week
if (currentDayOfWeek >= firstDayOfWeek && currentDayOfWeek <= lastDayOfWeek)
{
if (currentDayOfWeek == firstDayOfWeek)
{
// Start a new bucket
startOfBucket = currentDate;
}
if ((currentDayOfWeek == lastDayOfWeek) || (currentDate == end))
{
// End of bucket
endOfBucket = currentDate;
// Create bucket
buckets.Add(new Bucket()
{
StartDate = startOfBucket,
EndDate = endOfBucket
});
}
}
currentDate = currentDate.AddDays(1);
}
And this will give me the following date ranges ...
Start: 01/Jul/2015 End: 03/Jul/2015
Start: 06/Jul/2015 End: 10/Jul/2015
Start: 13/Jul/2015 End: 17/Jul/2015
Start: 20/Jul/2015 End: 24/Jul/2015
Start: 27/Jul/2015 End: 31/Jul/2015
Start: 03/Aug/2015 End: 07/Aug/2015
Start: 10/Aug/2015 End: 14/Aug/2015
Start: 17/Aug/2015 End: 21/Aug/2015
Start: 24/Aug/2015 End: 28/Aug/2015
Start: 31/Aug/2015 End: 01/Sep/2015
N.B. The first and last weeks are purposefully not full weeks (they abide to the date range given).
Edit
The solution provided here gives the number of days between the two dates but I am interested in getting the collection of date ranges.
Also, I don't need to account for any holidays.
Thanks,
It's quite handy using linq
var startDate = new DateTime(2015, 7, 1);
var endDate = new DateTime(2015, 9, 1);
var workDates = Enumerable.Range(0, (int)(endDate - startDate).TotalDays + 1)
.Select(i => startDate.AddDays(i))
.Where(date => (date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday))
.Select(i => i);
var display = workDates
.GroupAdjacentBy((x, y) => x.AddDays(1) == y)
.Select(g => string.Format("Start: {0:dd/MMM/yyyy} End: {1:dd/MMM/yyyy}", g.First(), g.Last()));
With the extension method GroupAdjacentBy<T>
public static class IEnumerableExtension
{
public static IEnumerable<IEnumerable<T>> GroupAdjacentBy<T>(
this IEnumerable<T> source, Func<T, T, bool> predicate)
{
using (var e = source.GetEnumerator())
{
if (e.MoveNext())
{
var list = new List<T> { e.Current };
var pred = e.Current;
while (e.MoveNext())
{
if (predicate(pred, e.Current))
{
list.Add(e.Current);
}
else
{
yield return list;
list = new List<T> { e.Current };
}
pred = e.Current;
}
yield return list;
}
}
}
}
Fiddle
This is based on Eric's accepted answer so please give him any upvote. I've just modified his solution to handle business weeks that could be 7 days long and also for one that could wrap a weekend.
var startDate = new DateTime(2015, 7, 1);
var endDate = new DateTime(2015, 9, 1);
DayOfWeek firstDayOfWeek = DayOfWeek.Monday;
DayOfWeek lastDayOfWeek = DayOfWeek.Friday;
var workDates = Enumerable.Range(0, (int)(endDate - startDate).TotalDays + 1)
.Select(i => startDate.AddDays(i))
.Where(date =>
// Normal work weeks where first day of week is before last (numerically) e.g. Monday -> Friday or Sunday -> Saturday
(firstDayOfWeek < lastDayOfWeek && date.DayOfWeek >= firstDayOfWeek && date.DayOfWeek <= lastDayOfWeek) ||
// Cater for business weeks whose start and end dates wrap over the weekend e.g. Thursday -> Tuesday
(lastDayOfWeek < firstDayOfWeek && (date.DayOfWeek >= firstDayOfWeek || date.DayOfWeek <= lastDayOfWeek)))
.Select(i => i);
var display = workDates
.GroupAdjacentBy((x, y) => x.AddDays(1) == y && !(x.DayOfWeek == lastDayOfWeek && y.DayOfWeek == firstDayOfWeek))
.Select(g => string.Format("Start: {0:dd/MMM/yyyy} End: {1:dd/MMM/yyyy}", g.First(), g.Last()));

Get weekday hours between two dates?

How would I get the number of weekday hours between two dates? (There's a lot of business days calculations, but doesn't seem to be much on weekday hours - not business/opening hours, just hours that aren't weekends).
This is my stab at it - is there a better way?
void Main()
{
// Works
DateTime start = new DateTime(2013,6,15,0,0,0); // Saturday
DateTime end = new DateTime(2013,6,17,10,0,0); // Monday
// Result = 10 (OK)
GetBusinessHours(start, end).Dump();
// Bugs
start = new DateTime(2013,6,14,0,0,0); // Friday
end = new DateTime(2013,6,15,0,0,0); // Saturday
// Result = 0 (Bug) - should be 24
GetBusinessHours(start, end).Dump();
}
public double GetBusinessHours(DateTime start, DateTime end)
{
double result = (end - start).TotalHours;
int weekendDays = Enumerable.Range(0, 1 + end.Subtract(start).Days).Select(offset => start.AddDays(offset)).Count(d => d.DayOfWeek == DayOfWeek.Sunday || d.DayOfWeek == DayOfWeek.Saturday);
double weekendDeltaHours = 0;
if (start.DayOfWeek == DayOfWeek.Saturday ||
start.DayOfWeek == DayOfWeek.Sunday)
{
weekendDays--;
weekendDeltaHours = start.Date.AddDays(1).Subtract(start).TotalHours;
}
else if (end.DayOfWeek == DayOfWeek.Saturday ||
end.DayOfWeek == DayOfWeek.Sunday)
{
weekendDeltaHours = (end - end.Date).TotalHours;
}
result = result - (weekendDays * 24) - weekendDeltaHours;
return result;
}
(Props to Ani for Enumerable.Range trick).
Not that this will be the most efficient method, it will work for what you require.
var end = DateTime.Now;
var start = end.AddDays(-100);
var weekend = new[] { DayOfWeek.Saturday, DayOfWeek.Sunday };
var count = Enumerable.Range(0, Convert.ToInt32(end.Subtract(start).TotalHours))
.Count(offset => !weekend.Contains(start.AddHours(offset).DayOfWeek));
It might be worth putting a check that the number of hours isn't too big for an int. However this would mean a date range of > 245,000 years!

Recurrence of events (LINQ query)

I have DateStart, DateEnd Periodicity, TypePeriodicity fields.
We have a query:
var result = Events.Where(e => e.DateStart <=today && e.DateEnd >= today).ToList();
I want that this query to check Periodicity.
For example:
name - record1
DateStart = 2012-02-02
DateEnd = 2012-03-31
Periodicity = 2
TypePeriodicity = 1 ( it's mean a week, may be also day = 0, month=2):
I want the following, if current date equals:
2,3,4,5 February - return `record1`
6,7,8..12 - not return, because TypePeriodicity = 1 and Periodicity = 2, which means every 2 weeks
13..19 - return `record1`
20..26 - not return
and so on until `DateEnd`
Thanks.
PS. Maybe not LINQ, but simple method that recieve result as parameter.
Here is something to get you started:
You could define a DateEvaluator delegate like so:
delegate bool DateEvaluator(DateTime startDate, DateTime endDate, DateTime dateToCheck, int periodicity);
The purpose of the delegate would be to evaluate for a given periodicity type if a date should be considered as within range. We would have hence 3 date evaluators.
One for each period type: Lets call them dayPeriodicityChecker, weekPeriodicityChecker and monthPeriodicityChecker
Our dayPeriodicityChecker is straightforward:
DateEvaluator dayPeriodicityChecker = (startDate, endDate, dateToCheck, periodicity) =>
{
if ((dateToCheck < startDate) || (dateToCheck > endDate))
return false;
TimeSpan dateDiff = dateToCheck - startDate;
return dateDiff.Days % periodicity == 0;
};
Our weekPeriodicityChecker needs to account for the start day of week, so the start date would need to be adjusted to the date in which the startDate week actually starts:
DateEvaluator weekPeriodicityChecker = (startDate, endDate, dateToCheck, periodicity) =>
{
if ((dateToCheck < startDate) || (dateToCheck > endDate))
return false;
DateTime adjustedStartDate = startDate.AddDays(-(int)startDate.DayOfWeek + 1);
TimeSpan dateDiff = dateToCheck - adjustedStartDate;
return (dateDiff.Days / 7) % periodicity == 0;
};
Our monthPeriodicityChecker needs to cater for months with a variable number of days:
DateEvaluator monthPeriodicityChecker dateToCheck, periodicity) =>
{
if ((dateToCheck < startDate) || (dateToCheck > endDate))
return false;
int monthDiff = 0;
while (startDate.AddMonths(1) < dateToCheck)
{
monthDiff++
// i'm sure there is a speedier way to calculate the month difference, but this should do for the purpose of this example
}
return (monthDiff - 1) % periodicity == 0;
};
Once you have all your date evaluators defined you could put them in an array like so:
DateEvaluator[] dateEvaluators = new DateEvaluator[]
{
dayPeriodicityChecker,
weekPeriodicityChecker,
monthPeriodicityChecker
};
This will allow you to do :
int periodicityType = 0; // or 1=week or 2=months
bool isDateIn = dateEvaluators[periodicityType ](startDate, endDate, dateTocheck, Periodicity)
So lets test this:
PeriodicityEvent pEvent = new PeriodicityEvent
{
Name = "record1",
DateStart = new DateTime(2012, 02, 02),
DateEnd = new DateTime(2012, 03, 31),
PeriodicityType = 1,
Periodicity = 2
};
DateTime baseDate = new DateTime(2012, 02, 01);
for (int i = 0; i < 30; i++)
{
DateTime testDate = baseDate.AddDays(i);
if (dateEvaluators[pEvent.PeriodicityType](pEvent.DateStart, pEvent.DateEnd, testDate, pEvent.Periodicity))
{
Console.WriteLine("{0} is in", testDate.ToString("dd MMM"));
}
else
{
Console.WriteLine("{0} is out", testDate.ToString("dd MMM"));
}
}
This will produce the desired output as below:
To use you would simply do:
Events.Where(e => dateEvaluators[e.PeriodType](e.DateStart, e.DateEnd, today, e.Periodicity).ToList();
Good luck!

Categories

Resources