-3

Question: Hi I am wondering if threre is a way to categorize time with a simple xor or somthing like that?

Explaination: I have a database table filled with DateTime fields PunchInTime And a PunchOut time and i am trying to get how many hours of a certain time-spans there are between those two fields, how many hours are there between 08:00 and 12:00 and how many between 12:30 and 16:30 and so on.

I am a Junior programmer so maybe im overlooking somthing here but please Advise or tell me if there is somthing i need to explain further.

Edit:

Yes sorry im trying to acomplish this in C# and there are multiple timespans i am trying to get out of a long time between 2 datetimes but they are never spannig a longer period than 24 hours.

I have tryed myself already with simple arithmetic but ive not managed to tackle this so it works.

This is a part of a program i am trying to make for work hours so im trying to get this right the first time

3 Answers3

1

This is how you can do in c#, Create two different DateTimes and use subtract, which will give a TimeSpan. you can get hours directly from that.

DateTime now = DateTime.Now;
DateTime firstTime = new DateTime(now.Year, now.Month, now.Day, 8,0,0);
DateTime secondTime = new DateTime(now.Year, now.Month, now.Day, 12, 0, 0);

int hours = secondTime.Subtract(firstTime).Hours;

if you need to do this in SQL, there are DATEDIFF() functions.

Pankaj
  • 2,618
  • 3
  • 25
  • 47
0

You can use DateTime.Subtract method. Which will give you a TimeSpan and from there you can use TimeSpan.TotalHours.

Look at the DateTime.Subtract Reference Here

TSungur
  • 396
  • 2
  • 9
0

You have to get instance of TimeSpan which has all information you want and you can do it like this:

var start = DateTime.Now;
var end = DateTime.Now.AddMinutes(15);

var diff = end - start;