Click here to Skip to main content
13,146,739 members (80,790 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

12.5K views
1 bookmarked
Posted 9 Nov 2011

Find the count of a weekday between two dates without iterating/looping

, 11 Nov 2011
Rate this:
Please Sign up or sign in to vote.
Single line in Linq This gets you the number of weekdays, i.e., Monday to Friday between given two dates.[VB]Dim dt1 As New Date(2011, 1, 1)Dim dt2 As New Date(2011, 1, 15)Dim days As Integer = (From d As Date In _ (Enumerable.Range(0, 1 +...
Single line in Linq

This gets you the number of weekdays, i.e., Monday to Friday between given two dates.
[VB]
Dim dt1 As New Date(2011, 1, 1)
Dim dt2 As New Date(2011, 1, 15)
Dim days As Integer = (From d As Date In _
  (Enumerable.Range(0, 1 + dt2.Subtract(dt1).Days).Select(Function(offset) dt1.AddDays(offset)).ToArray()) _
  Where d.DayOfWeek >= DayOfWeek.Monday And d.DayOfWeek <= DayOfWeek.Friday Select 1).Sum

[C#]
DateTime dt1 = new DateTime(2011, 1, 1);
DateTime dt2 = new DateTime(2011, 1, 15);
int days = (from d in (Enumerable.Range(0, 1 + dt2.Subtract(dt1).Days).Select(offset => dt1.AddDays(offset)).ToArray())where d.DayOfWeek >= DayOfWeek.Monday & d.DayOfWeek <= DayOfWeek.Friday1).Sum;


You can easily modify it to get the count of a particular day as,
[VB]
Dim dt1 As New Date(2011, 1, 1)
Dim dt2 As New Date(2011, 1, 15)
Dim days As Integer = (From d As Date In _
  (Enumerable.Range(0, 1 + dt2.Subtract(dt1).Days).Select(Function(offset) dt1.AddDays(offset)).ToArray()) _
  Where d.DayOfWeek = DayOfWeek.Monday Select 1).Sum

[C#]
DateTime dt1 = new DateTime(2011, 1, 1);
DateTime dt2 = new DateTime(2011, 1, 15);
int days = (from d in (Enumerable.Range(0, 1 + dt2.Subtract(dt1).Days).Select(offset => dt1.AddDays(offset)).ToArray())where d.DayOfWeek == DayOfWeek.Monday).Sum;


See here, Enumerable.Range gets you the array of dates between given dates. You can apply your conditions and get different results using Linq on it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Prerak Patel
Software Developer
India India
Working since 2006 on VBA, VB6, VB.Net, C#, ASP.Net, MSSQL




  • Courage is not the absence of fear, but rather the judgement that something is more important than fear.

  • The fear of suffering is worse than the suffering itself.

  • People need not fear the unknown if they are capable of achieving what they need and want.

  • Every blessing ignored becomes a curse.

  • Sometimes what's in your head isn't as crazy as you think.

  • We never really grow up, we only learn how to act in public.

  • You can make very bad teams with very good individuals.

  • Admitting mistakes means you have a sense of responsibility in your actions and that shows you are more matured than almost anyone. -Nithin


You may also be interested in...

Pro

Comments and Discussions

 
GeneralReason for my vote of 1 This iterates, which is clearly to b... Pin
AspDotNetDev11-Nov-11 6:50
protectorAspDotNetDev11-Nov-11 6:50 
GeneralRe: Sorry for those wrong variable names. Corrected it. About it... Pin
Prerak Patel11-Nov-11 16:19
mvpPrerak Patel11-Nov-11 16:19 
GeneralWhy did you provide an alternate using VB when the original ... Pin
John Simmons / outlaw programmer10-Nov-11 2:18
mvpJohn Simmons / outlaw programmer10-Nov-11 2:18 
GeneralRe: As I am basically a VB developer, I just made it and posted.... Pin
Prerak Patel10-Nov-11 3:27
mvpPrerak Patel10-Nov-11 3:27 
GeneralDont you think LINQ is internally again a loop though with m... Pin
zenwalker19859-Nov-11 18:58
memberzenwalker19859-Nov-11 18:58 
GeneralRe: That's good example of maths in code, still, with this code ... Pin
Prerak Patel9-Nov-11 20:08
mvpPrerak Patel9-Nov-11 20:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 11 Nov 2011
Article Copyright 2011 by Prerak Patel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid