Convert Standard time to Military Time

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 12734 Views - Last Post: 31 August 2011 - 06:47 PM Rate Topic: -----

#1 jhoog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 13-June 11

Convert Standard time to Military Time

Posted 17 August 2011 - 12:29 PM

I want to create a new function within my class to convert standard time to military time, can someone help me? I am fairly new to programming and not sure how to go about doing this?
Is This A Good Question/Topic? 0
  • +

Replies To: Convert Standard time to Military Time

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Re: Convert Standard time to Military Time

Posted 17 August 2011 - 12:34 PM

You don't convert time. Period. Time is time. Its a DateTime object.

But you can display it how you like

myDateTimeObject.ToString("HH:mm:ss"); // 24 hour time
myDateTimeObject.ToString("hh:mm:ss"); // 12 hour time

Was This Post Helpful? 2
  • +
  • -

#3 jhoog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 13-June 11

Re: Convert Standard time to Military Time

Posted 17 August 2011 - 12:39 PM

makes sense, but what if i am pulling the time from a textbox that the user enters in standard 12 hour time? I need to take the 12 hour time and turn that into 24 hour time for other reasons. So the user enters a time into a text box and it is stored as a 12 hour time, but I need it to be shown in 24 hour time. Same thing?

This post has been edited by jhoog: 17 August 2011 - 12:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Re: Convert Standard time to Military Time

Posted 17 August 2011 - 12:53 PM

Read up on that link I gave you for DateTime object.

What you would do is .TryParse the text back into a DateTime object.

From there you have a real DateTime that you can manipulate in lots of ways: Display it as you like, .AddDays(), subtract one DateTime from another to get a TimeSpan... etc.

By the way, if you haven't noticed this about the DIC site...

At the bottom of the thread is a list of links to similar threads that the site thinks might help you, based on keywords like "military time".

It looks like there are a few threads here that probably have some code examples, in addition to the MSDN page I gave you.

Attached Image

This post has been edited by tlhIn`toq: 17 August 2011 - 12:49 PM

Was This Post Helpful? 3
  • +
  • -

#5 jhoog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 13-June 11

Re: Convert Standard time to Military Time

Posted 18 August 2011 - 08:26 AM

I am still struggling with this, I am not quite understanding, and none of the other topics really address my issue. Any way you could provide sample code? I just want to be able to take the user input 12 hour time and store it as 24 hour time, i figured easiest way was to create a method inside my date time class but I dont know how to do this, I am not quite understanding for some reason. I know I first need to split the time string to give me back hours and also am or pm to determine if i need to add 12 to the time or not

This post has been edited by jhoog: 18 August 2011 - 08:27 AM

Was This Post Helpful? 0
  • +
  • -

#6 sithius92  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 160
  • Joined: 01-August 08

Re: Convert Standard time to Military Time

Posted 18 August 2011 - 08:34 AM

Take a look here. There are formats to get a 24-hour clock. You would just have to convert what the user inputs to a new DateTime using the 24-hour clock format.
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4425
  • View blog
  • Posts: 7,694
  • Joined: 08-June 10

Re: Convert Standard time to Military Time

Posted 18 August 2011 - 08:46 AM

View Postjhoog, on 18 August 2011 - 10:26 AM, said:

I am still struggling with this, I am not quite understanding, and none of the other topics really address my issue. Any way you could provide sample code? I just want to be able to take the user input 12 hour time and store it as 24 hour time, i figured easiest way was to create a method inside my date time class but I dont know how to do this, I am not quite understanding for some reason. I know I first need to split the time string to give me back hours and also am or pm to determine if i need to add 12 to the time or not



What do you mean "store it"? If you're talking about in a database, they don't store time that way anyway. The point we're trying to tell you is that time isn't represented in any specific format to the computer. You can take a time string, parse it into a DateTime object, then format that object any way you want, including military time. You need to decouple the concept of data and formatting in your mind; it's a very important lesson for any programmer to learn. The computer stores data in whatever way is convenient, and the language provides you mechanisms to display it any number of ways.

To begin, the first place you should look is here: DateTime.TryParse. Give that a thorough perusal to learn how to take a string from a textbox and make a DateTime out of it.

Next, you should look at the DateTime.ToString method to format and output a DateTime.

And no, giving you the sample code straight out would defeat the purpose of this forum. We're not interested in giving out copy/pastable solutions. We're interested in helping you become a better programmer, which means helping you help yourself.
Was This Post Helpful? 0
  • +
  • -

#8 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Convert Standard time to Military Time

Posted 18 August 2011 - 08:47 AM

View Postjhoog, on 18 August 2011 - 08:26 AM, said:

I just want to be able to take the user input 12 hour time and store it as 24 hour time


I see this often. You don't 'store it as a 24 hour time'. DateTime stores the value for you, however it wants. You don't care how it stores it. Treat DataTime as a black box: You don't know what goes on inside nor do you care. DateTime has methods to return it's value, and you can use them to format the time however you like. Want AM/PM, it does that. Want 24 hour clock, it does that too. But, again, *you don't have to worry about how it stores the value*.

Others have given links on how to convert a string into a DateTime object, so I won't go into that.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Re: Convert Standard time to Military Time

Posted 18 August 2011 - 08:50 AM

Show me what you have coded so far and we will go from there.
But here at DIC we don't just hand out coding solutions without seeing that the poster has at least made an effort to implement the suggests we've made.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Re: Convert Standard time to Military Time

Posted 18 August 2011 - 08:56 AM

Received as a PM:

jhoog, on 18 August 2011 - 09:37 AM, said:

I have posted on the forum and havent really gotten much help and I was wondering if you would be able to guide me in the right direction. I am working in C# btw. So here is what I got going on. I have a form that has the user input the time with hours in one box minutes in the next and the third is a drop down asking am or pm.
So the user enters the time in standard 12 hour format and it is stored. Well I need the format to be in 24 hour military style format for another part of the code I'm working on. I assume easiest way would be to create a new method within my DateTime class to make easier for future use. I know I need to split the string in order to check for am or pm before adding the 12 to the time but i am not sure how to split it or where to go from there for that matter. Here is a little of what I started but it isnt much at all, not even sure if its correct to begin with

public String convertToMilitaryFormatFromStandardTime(String militaryTime)
    {
        String StandardTime = "";
        String MilitaryTime = "";

        String[] timeSplit = militaryTime.Split(':');
        if (timeSplit.Length < 2)
        { 
        
            

        }

            return militaryTime;
        
    }


I am going to move this to the public thread you already started.

You are actually getting plenty of help there.
What you aren't getting is someone writing the code for you.

I see no effort at all on your part to make a DateTime object out of your string for the time. This was the advice you were given along with links on how to do it. But you aren't trying to take that advice.

So what are we supposed to do to help you if you don't even try the advice we give you?

This post has been edited by tlhIn`toq: 18 August 2011 - 08:57 AM

Was This Post Helpful? 2
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Re: Convert Standard time to Military Time

Posted 18 August 2011 - 09:06 AM

Here is as much sample as I am going to provide without seeing some effort on your part:

Attached Image

Attached Image

Attached Image
Was This Post Helpful? 2
  • +
  • -

#12 jhoog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 13-June 11

Re: Convert Standard time to Military Time

Posted 18 August 2011 - 09:09 AM

Here is what I got

public String convertToMilitaryFormatFromStandardTime(String militaryTime)
    {
        String time = "HH:MM PM";
        String[] split1 = time.Split(':');
        String[] split2 = split1[1].Split(' ');
        if(split2[1] == "PM")
        {
        split1[0] = (int.Parse(split1[0] + 12) + "");
        }

        return split1[0] + ":" + split2[0];
        
    }

This post has been edited by Curtis Rutland: 18 August 2011 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#13 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 262
  • View blog
  • Posts: 1,460
  • Joined: 07-April 08

Re: Convert Standard time to Military Time

Posted 18 August 2011 - 09:21 AM

you are making this harder then it needs to be. You have been given everything you need in order to do this correctly, yet you seem to not want to take the time to even attempt it.

First you will want to make sure that your string militaryTime is actually a date using DateTime.TryParse (not giving you links as they have already been given.

Then once you have verified that it is a correct date (and stored it into a DateTime variable as that is a piece of what tryparse does) then you return it using one of the .ToString calls that has already been given to you. We are NOT going to give you the code, we have given you the means to do what your asking. Take what you've been given and attempt it, and if you have any other questions, please feel free to come back to us.
Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4425
  • View blog
  • Posts: 7,694
  • Joined: 08-June 10

Re: Convert Standard time to Military Time

Posted 18 August 2011 - 10:33 AM

Go back and read my post. I explain how to convert between strings and datetimes and back to strings. Don't expect help if you're not going to even bother trying the help we offer.
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Re: Convert Standard time to Military Time

Posted 18 August 2011 - 12:02 PM

I've already provided code in post #11 along with example output in 12 and 24 hour format.

Let's lighten up on the OP and quit repeating the information that several have given.

He other uses it or not. He other comes back to us or not.
For all we know he is away from his computer for a week so badgering him won't do any good.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2