How to get FULL current time.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: How to get FULL current time.

  1. #1
    Join Date
    Mar 2012
    Posts
    13

    How to get FULL current time.

    Hello all.

    So, I'm struggling with Systemtime in C++
    I made strings to Hour, Minute and Second.
    Here they are.

    String^ GrabHour()
    {
    DateTime GetDateTimeInfo = DateTime::Now;
    GetDateTimeInfo.Hour.ToString();
    return GetDateTimeInfo.Hour.ToString();

    }
    String^ GrabMinute()
    {
    DateTime GetDateTimeInfo = DateTime::Now;
    GetDateTimeInfo.Minute.ToString();
    return GetDateTimeInfo.Minute.ToString();

    }
    String^ GrabSecond()
    {
    DateTime GetDateTimeInfo = DateTime::Now;
    GetDateTimeInfo.Second.ToString();
    return GetDateTimeInfo.Second.ToString();
    }

    Now the problem is. If the System time is 07, the String shows 7,
    But I need to have two numbers. Not 7 but, 07. Otherwise my program won't functioning.

    My question now is, is there a way to "int" 7 = 07 or something?
    Or is there a FULL method to get the time, only the time. Not the date?

    Thanks

  2. #2
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    7,994

    Re: How to get FULL current time.

    You can pass format specifiers to ToString method.
    Example
    Code:
          String^ GrabHour()
          {
             DateTime GetDateTimeInfo = DateTime::Now;
             return GetDateTimeInfo.Hour.ToString("00"); // two digits
          }
    For more info, see Formatting Types in MSDN Library.


    [ Moved thread ]
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  3. #3
    Join Date
    Mar 2012
    Posts
    13

    Re: How to get FULL current time.

    Quote Originally Posted by ovidiucucu View Post
    You can pass format specifiers to ToString method.
    Example
    Code:
          String^ GrabHour()
          {
             DateTime GetDateTimeInfo = DateTime::Now;
             return GetDateTimeInfo.Hour.ToString("00"); // two digits
          }
    For more info, see Formatting Types in MSDN Library.


    [ Moved thread ]
    Solved.
    This is the solving solution!
    Thanks!

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    Re: How to get FULL current time.

    To extend on what ovidiucucu already stated correctly...

    Code:
          String^ GrabHour()
          {
             DateTime GetDateTimeInfo = DateTime::Now;
             return GetDateTimeInfo.Hour.ToString("00"); // two digits
          }
    Actually, thre's not even a need to convert the hour value to an int before formatting it. Using a custom time format string, you can obtain it by formatting the DateTime directly:


    Code:
          String^ GrabHour()
          {
             DateTime GetDateTimeInfo = DateTime::Now;
             return GetDateTimeInfo.ToString("HH"); // two digits
          }
    And, of course, there are also corresponding format strings for the minute ("mm") and the second ("ss"). Such a format string can also be used to obtain the complete time (without date) as three times two characters plus time separators by using the format string "HH:mm:ss".

    Moreover, given this is the common way to write it in the current culture, this complete format is equivalent to the shorter (and culture-aware) long time format that is written as "T". Finally, as this format is used really frequently in programs, there's a DateTime method that returns a time formatted that way without the need to pass a format string at all: ToLongTimeString(). So, the straightest way to get to the complete formatted time, based on your code, would be:

    Code:
          String^ GrabTime()
          {
             DateTime GetDateTimeInfo = DateTime::Now;
             return GetDateTimeInfo.ToLongTimeString(); // six digits, two separators
          }
    Quote Originally Posted by Miraclezz View Post
    Solved.
    This is the solving solution!
    Thanks!
    Fine. In this situation it's good practice (though unfortunately rarely done) to mark the thread [RESOLVED] using the Thread Tools menu at the top of the thread display, as an aid to other users seeing the thread.
    Last edited by Eri523; March 17th, 2012 at 03:52 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center