-2

I want to select and save the entry and exit process according to the time coming from the personnel card reader.

How can I get difference between 2 dates?

  1. Shift start: 07:00 end: 16:00
  2. Shift start: 16:00 end: 23:00
  3. Shift start: 23:00 end: 07:00
string entrystring = "23:00";
string exitstring = "07:00";
string deviceclockstring = "09:00";

TimeSpan entry = TimeSpan.Parse(entrystring);
TimeSpan exit = TimeSpan.Parse(exitstring);
TimeSpan deviceclock = TimeSpan.Parse(deviceclockstring);

double entryTimeDiff = (deviceclock - entry).TotalSeconds;
double exitTimeDiff = (exit - deviceclock).TotalSeconds;

if (entryTimeDiff < exitTimeDiff)
    Console.WriteLine("closer to check-in time");
else if (entryTimeDiff == exitTimeDiff)
    Console.WriteLine("midway between check-in and check-out");
else
    Console.WriteLine("closer to departure time");
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 1
    Welcome to StackOverflow! Please read [ask]. In particular, your question is very vague. Please narrow it down to a specific problem which we can help with. "Select and save" doesn't mean very much, for example. You're expected to attempt to solve your problem yourself, and ask for help on the specific bits you get stuck on. – canton7 Aug 03 '21 at 10:26
  • 1
    Please share your sourcecode and describe what exactly is your problem – Olli Aug 03 '21 at 10:26
  • 1
    OK. That's an interesting requirement. What's our involvement in all of this? – ProgrammingLlama Aug 03 '21 at 10:26
  • 1
    Don't do `.TotalSeconds` - keep it as a `TimeSpan`. It's then strongly-typed and cleaner to work with. – Enigmativity Aug 10 '21 at 04:55

2 Answers2

0

Try this:

TimeSpan entry = TimeSpan.Parse("23:00");
TimeSpan exit = TimeSpan.Parse("07:00");
TimeSpan deviceclock = TimeSpan.Parse("09:00");

TimeSpan GetDiff(TimeSpan start, TimeSpan end) =>
    end.Subtract(start).Add(TimeSpan.FromDays(end < start ? 1.0 : 0.0));
    
TimeSpan entryTimeDiff = GetDiff(entry, deviceclock);
TimeSpan exitTimeDiff = GetDiff(deviceclock, exit);
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
0

if you have to consider the difference in both direction (clockwise and anticlockwise). Subtract both ways and consider the minimum of both difference

TimeSpan entry = TimeSpan.Parse("16:00");
TimeSpan exit = TimeSpan.Parse("23:00");
TimeSpan deviceclock = TimeSpan.Parse("09:00");

TimeSpan GetDiff(TimeSpan start, TimeSpan end) =>
end.Subtract(start).Add(TimeSpan.FromDays(end < start ? 1.0 : 0.0));

var entryTimeDiffClockwise = GetDiff(entry,deviceclock);
var entryTimeDiffAntiClockwise = GetDiff(deviceclock,entry);
var entryTimeDiff = new TimeSpan(Math.Min(entryTimeDiffClockwise.Ticks, entryTimeDiffAntiClockwise.Ticks));

var exitTimeDiffClockwise = GetDiff(deviceclock,exit);
var exitTimeDiffAntiClockwise = GetDiff(exit,deviceclock);
var exitTimeDiff = new TimeSpan(Math.Min(exitTimeDiffClockwise.Ticks, exitTimeDiffAntiClockwise.Ticks));

if (entryTimeDiff < exitTimeDiff)
    Console.WriteLine("closer to check-in time");
else if (entryTimeDiff == exitTimeDiff)
    Console.WriteLine("midway between check-in and check-out");
else
    Console.WriteLine("closer to departure time");
Akshay G
  • 2,070
  • 1
  • 15
  • 33