Connecting Tech Pros Worldwide Help | Site Map

Military Time conversion

 
LinkBack Thread Tools Search this Thread
  #1  
Old October 2nd, 2007, 12:05 PM
drg
Guest
 
Posts: n/a
Default 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

  #2  
Old October 2nd, 2007, 12:25 PM
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Default Re: Military Time conversion

On Oct 2, 1:04 pm, drg <dr...@sbcglobal.netwrote:
Quote:
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

  #3  
Old October 2nd, 2007, 12:35 PM
=?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=
Guest
 
Posts: n/a
Default 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:
Quote:
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
>
  #4  
Old October 2nd, 2007, 01:25 PM
Chris Shepherd
Guest
 
Posts: n/a
Default Re: Military Time conversion

drg wrote:
Quote:
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.
  #5  
Old October 3rd, 2007, 11:45 AM
drg
Guest
 
Posts: n/a
Default Re: Military Time conversion

drg wrote:
Quote:
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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.