TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

Military Time conversion

C#(C Sharp)

 
 
 
Thread Tools Display Modes
Old 02-10-2007, 01:03 PM   #1
drg
Guest
 
Posts: n/a
Military Time conversion

My assignment is actually encapsulation which is not the problem -- my
program needs to take two military times and calculate the minutes
elapsed between the two. I have everything worked out so far except how
to convert the military time from a string to an int. Or should I be
casting instead?


Student needs help.

thanks,
DRG
 
Old 02-10-2007, 01:03 PM   #2
Family Tree Mike
Guest
 
Posts: n/a
RE: Military Time conversion

I would split your string to two substrings, one for the hours and one for
the minutes. I don't think there is a standard for 1345 or 13:45. Once
split, then I would use integer.TryParse(sHours, out hours); and
integer.TryParse(sMinutes, out minutes);

"drg" wrote:

> My assignment is actually encapsulation which is not the problem -- my
> program needs to take two military times and calculate the minutes
> elapsed between the two. I have everything worked out so far except how
> to convert the military time from a string to an int. Or should I be
> casting instead?
>
>
> Student needs help.
>
> thanks,
> DRG
>

 
Old 02-10-2007, 01:03 PM   #3
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Re: Military Time conversion

On Oct 2, 1:04 pm, drg <dr...@sbcglobal.net> wrote:
> My assignment is actually encapsulation which is not the problem -- my
> program needs to take two military times and calculate the minutes
> elapsed between the two. I have everything worked out so far except how
> to convert the military time from a string to an int. Or should I be
> casting instead?
>
> Student needs help.


Use DateTime.ParseExact to convert it to a DateTime, specifying the
format.

You can subtract one DateTime from another to get a TimeSpan
representing the difference between the two.

Jon

 
Old 02-10-2007, 01:03 PM   #4
Chris Shepherd
Guest
 
Posts: n/a
Re: Military Time conversion

drg wrote:
> My assignment is actually encapsulation which is not the problem -- my
> program needs to take two military times and calculate the minutes
> elapsed between the two. I have everything worked out so far except how
> to convert the military time from a string to an int. Or should I be
> casting instead?


You should be using DateTime.Parse (or possibly DateTime.ParseExact) to
get the DateTime that reflects your first and second time, then just
subtract one from the other and you'll have your TimeSpan. The only
catch may be the handling of military time, but it should work.

example:

DateTime a = DateTime.Parse("10:59:44 PM");
DateTime b = DateTime.Parse("14:12:10");

TimeSpan c = b - a;

Chris.
 
Old 03-10-2007, 12:02 PM   #5
drg
Guest
 
Posts: n/a
Re: Military Time conversion

drg wrote:
> My assignment is actually encapsulation which is not the problem -- my
> program needs to take two military times and calculate the minutes
> elapsed between the two. I have everything worked out so far except how
> to convert the military time from a string to an int. Or should I be
> casting instead?
>
>
> Student needs help.
>
> thanks,
> DRG

Thanks, guys! This is twice I have used this group as a 'last resort'
and twice you have come through for me. Guess I should check here first
next time.

I had to used the DateTime.ParseExact() in my clsMilitary.cs code and
the DateTime.Parse() in my militaryTimeDiff method in the frmMain.cs
code. Don't know if that is how its suppose to be but it works.

DRG
 
 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
need to generate a line and block alert roster military style. Alert Roster Microsoft Office 0 25-09-2007 06:04 AM
military time in java mrshinton01@gmail.com Java 1 22-09-2007 02:32 AM
MFC to .NET DLL Conversion James Simpson VC.NET 1 05-09-2007 09:22 AM
Do You Know U.S. Military Basics? Petrowhisky General Discussions 0 27-03-2006 09:06 AM
US Military to Probe 2005 Iraq Firefight Strider General Discussions 0 18-03-2006 05:29 AM


< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 01:16 PM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional