Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3230072
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:53:03+00:00 2026-05-17T16:53:03+00:00

I need to determine duration between two DateTimes in minutes. However, there is a

  • 0

I need to determine duration between two DateTimes in minutes.

However, there is a slight twist:

  • exclude weekends
  • only count minutes
    which are between 7:00AM and 7:00PM.
    for example: [09/30/2010 6:39:00
    PM] - [09/30/2010 7:39:00 PM] = 21
    Minutes

I’m just having a hard time coming up with a decent way to do it and would appreciate if anyone can suggest something.

Thanks.


Edit:

I ended up going with dtb’s solution. There is only one special case which needed to be taken care of: if end time is after 7:00PM, count the minutes from 7:00AM to the actual end time.

This is how I modified it:

var minutes = from day in start.DaysInRangeUntil(end)
                where !day.IsWeekendDay()
                let st = Helpers.Max(day.AddHours(7), start)
                let en = (day.DayOfYear == end.DayOfYear ? 
                            end :
                            Helpers.Min(day.AddHours(19), end)
                            )
                select (en - st).TotalMinutes;

Again, thanks for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:53:03+00:00Added an answer on May 17, 2026 at 4:53 pm

    You can, of course, use LINQ:

    DateTime a = new DateTime(2010, 10, 30, 21, 58, 29);
    DateTime b = a + new TimeSpan(12, 5, 54, 24, 623);
    
    var minutes = from day in a.DaysInRangeUntil(b)
                  where !day.IsWeekendDay()
                  let start = Max(day.AddHours( 7), a)
                  let end   = Min(day.AddHours(19), b)
                  select (end - start).TotalMinutes;
    
    var result = minutes.Sum();
    
    // result == 6292.89
    

    (Note: You probably need to check for a lot of corner cases which I completely ignored.)

    Helper methods:

    static IEnumerable<DateTime> DaysInRangeUntil(this DateTime start, DateTime end)
    {
        return Enumerable.Range(0, 1 + (int)(end.Date - start.Date).TotalDays)
                         .Select(dt => start.Date.AddDays(dt));
    }
    
    static bool IsWeekendDay(this DateTime dt)
    {
        return dt.DayOfWeek == DayOfWeek.Saturday
            || dt.DayOfWeek == DayOfWeek.Sunday;
    }
    
    static DateTime Max(DateTime a, DateTime b)
    {
        return new DateTime(Math.Max(a.Ticks, b.Ticks));
    }
    
    static DateTime Min(DateTime a, DateTime b)
    {
        return new DateTime(Math.Min(a.Ticks, b.Ticks));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to determine whether or not two sets contains exactly the same elements.
I need to determine if a given selection is in between a start line
I need to determine whether there is a resource file for the given CultureInfo
I need to determine if a string contains two or more consecutive alpha chars.
I need to determine if an InetSocketAddress is IPv6 or IPv4 efficiently. The only
My winform application is launched by another application (the parent), I need determine the
I need to determine in 80% if a file is binary or text, is
I need to determine file type (i.e., MimeType) of stored data in the SQL
I need to determine the clients .NET framework version in my web application. I'm
I need to determine whether a selected UIColor (picked by the user) is dark

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.