Home > Net >  Calculate the hours in 2 different days excluding the sunday
Calculate the hours in 2 different days excluding the sunday

Time:11-01

 mydt1 = Convert.ToDateTime(dt.Rows[0].ItemArray[7].ToString());

 DateTime dt2 = DateTime.Now;

 var hours = (dt2 - mydt1).TotalHours;

 if (hours >= 0) {
   txtElapse.Text = hours.ToString("0");
   var totals = hours * 2;

   txtFine.Text = totals.ToString("0");
 } else {
   txtFine.Text = "0";
   txtElapse.Text = "0";
 }

I'm creating a library system, and I'm working on the fine. However, I want to exclude every Sunday that passes on the timespan. I don't know how could I do it or what logic I can use.

CodePudding user response:

So you want to get difference (TimeSpan) between 2 DateTime, but ignoring Sunday.

Here is my minimum reproducible example:

using System;

public class Program
{
        public static void Main(string[] args)
        {
                DateTime dt1 = Convert.ToDateTime(args[0]);
                DateTime dt2 = Convert.ToDateTime(args[1]);
                TimeSpan x = dt2-dt1;
                double hours = x.TotalHours;

                int numOfWeek = (int)Math.Floor(x.TotalDays/7);
                hours = hours - (numOfWeek * 24);
                // if difference is more than 1 week, then subtract hours with week difference

                DayOfWeek dt1_dow = dt1.DayOfWeek; // System.DayOfWeek is enum, 0=Sunday, 1=Monday, etc
                DayOfWeek dt2_dow = dt2.DayOfWeek; // System.DayOfWeek is enum, 0=Sunday, 1=Monday, etc
                if( (int)dt1_dow > (int)dt2_dow )
                {
                        hours = hours - 24;
                }
                // if dt1's day of week is higher than dt2's day of week, then we can be sure that it go over one Sunday

                Console.WriteLine(numOfWeek);
                Console.WriteLine(hours);
        }
}

You can use https://dotnetfiddle.net/ or compile it yourself as console application (in visual studio) or save it as file.cs text file then compile with mono in linux with rm file.exe ; csc file.cs ; mono file.exe '2021-11-05 03:00:00' '2021-11-10 03:00:00'

Test:

$ cal
    November 2021   
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

Test cases:

$ mono a.exe '2021-11-05 03:00:00' '2021-11-10 03:00:00'
0
96
  • date 5 to 10: pass sunday, not more than 1 week, resulting in 96 hours (correct, since it's 4 days not counting sunday)
$ mono a.exe '2021-11-03 03:00:00' '2021-11-05 03:00:00'
0
48
  • date 3 to 5: does not pass sunday, not more than 1 week, resulting in 48 hours (correct, since it's 2 days)
$ mono a.exe '2021-11-03 03:00:00' '2021-11-15 03:00:00'
1
240
  • date 3 to 15: does not pass sunday, more than 1 week, resulting in 240 hours (correct, since it's 10 days not counting sunday)

So in your code:

 mydt1 = Convert.ToDateTime(dt.Rows[0].ItemArray[7].ToString());

 DateTime dt2 = DateTime.Now;

 var hours = (dt2 - mydt1).TotalHours;

// start adding code
int numOfWeek = (int)Math.Floor(x.TotalDays/7);
hours = hours - (numOfWeek * 24);
DayOfWeek dt1_dow = dt1.DayOfWeek;
DayOfWeek dt2_dow = dt2.DayOfWeek;
if( (int)dt1_dow > (int)dt2_dow )
{
    hours = hours - 24;
}
// end adding code

 if (hours >= 0) {
   txtElapse.Text = hours.ToString("0");
   var totals = hours * 2;

   txtFine.Text = totals.ToString("0");
 } else {
   txtFine.Text = "0";
   txtElapse.Text = "0";
 }

  • Related