I have been working on a massive project for awhile now to help do the scheduling at my work. One now I am working on a form design for entering in data to create a shift.

Part of this is the start time and the end time of the shift.

Now I like to challange myself, and enjoy creating new ideas. But I was wondering if there was something in C# that would allow me to find the total time between a start time and end time?

The form object has the two textboxes (the zeros are removed upon entering the textbox) and then a comboBox next to it for either AM or PM.

I hope I have given enough data to help, I feel the picture can say alot (also seeming to have trouble writting at the moment).

If you need more verification or help making sense of this don't hesistate to ask. Thanks in advance for any help possible

Edited 4 Years Ago by pritaeas: Removed URL.

you could use something like this..

DateTime d1 = new DateTime(2011, 05, 31, 12, 5, 0); // in yyyy,mm,dd,hour,minute,second form
            DateTime d2 = new DateTime(2011, 05, 31, 13, 5, 0);
            

            TimeSpan t = new TimeSpan();
            t = d2 - d1;

            string str = t.Hours.ToString();  // Here the value of the str will be 1 (1 hour difference)
Comments
Seemed to be the best answer of what I was looking for

Hey! I've done something exactly like this before! :)

DateTime and TimeSpan didn't work so well for me and required extra crap like Days, Year, Seconds, etc. I didn't like that. I only wanted the distance between two hours in one day. What I ended up doing was, if the time was a PM time, I converted that to military hours and subtracted from the start time.

For example:
Start Time = 5:00am
End Time = 2:00pm

2pm Military Time = 14:00. So I did 14 - 5 and 0 - 0, which gives me 9.

It'll get a bit more involved if the time isn't right on the hour. Minutes will tend to be wacked up, but there's an easy workaround. I have code if you want it.

Edited 5 Years Ago by WildBamaBoy: n/a

I'll have to try those TimeSpan. See WildBamaBoy, I can't always subtract AM from PM. Some shifts risk running into the next day, so it would be PM to AM or sometimes even AM to AM.

I think this TimeSpan might be the easiest, I'll give it a try after I get a break from work

TimeSpan is the way to go. AM and PM have nothing to do with it, say you have a timespan of 37 hours 15 minutes and and 13 seconds. Would that be AM or PM?
I think this is about hours worked, right?

Edited 5 Years Ago by ddanbe: n/a

AM and PM have nothing to do with it, say you have a timespan of 37 hours 15 minutes and and 13 seconds. Would that be AM or PM?
I think this is about hours worked, right?

He was telling me how my idea wouldn't work. :P

If you're going AM to AM I did the same thing just without converting to military hour. However I believe it does have a problem with PM to AM, but it worked for my needs how it was so I didn't bother to fix it. Not the best thing for you then.

Edited 5 Years Ago by WildBamaBoy: n/a

He was telling me how my idea wouldn't work. :P

If you're going AM to AM I did the same thing just without converting to military hour. However I believe it does have a problem with PM to AM, but it worked for my needs how it was so I didn't bother to fix it. Not the best thing for you then.

Yeah trust me I was originally building something like you said, but it was getting complicated.

And as for ddanbe
I am actually just trying to find the total time of a shift. In otherwords, it finds the total time of the shift off the start and end time. The purpose is sometimes we limit how many hours people can work in a week.

Use this code:

string dateString = "5/1/2008 8:30:52 AM";
DateTime date1 = DateTime.Parse(dateString, 
                          System.Globalization.CultureInfo.InvariantCulture);

I found it on: http://msdn.microsoft.com/en-us/library/system.datetime.aspx
Construct your own dateString from the values in your program. Should be easy I guess. Then use some code posted by sandeepparekh9. Look up what you can do with TimeSpan on MSDN and never worry about AM and PM again. DateTime and TimeSpan will take care of that.
Look here if you want to format date and time.

Edited 5 Years Ago by ddanbe: n/a

you could use something like this..

DateTime d1 = new DateTime(2011, 05, 31, 12, 5, 0); // in yyyy,mm,dd,hour,minute,second form
            DateTime d2 = new DateTime(2011, 05, 31, 13, 5, 0);
            

            TimeSpan t = new TimeSpan();
            t = d2 - d1;

            string str = t.Hours.ToString();  // Here the value of the str will be 1 (1 hour difference)

Well thanks for all the help to everyone, I mostly used the example I replied to and the did alittle magic of my own. For all I care I put in random dates, that hopefully will not interfer with anything

This question has already been answered. Start a new discussion instead.