locked
How to convert SYSTEMTIME to String RRS feed

  • Question

  • Hi - Haven't seen this discussed before so thought I'd ask. I'm very new to all of this.

    I want to convert a SYSTEMTIME to STring as below:

      SYSTEMTIME nowT;

      GetSystemTime(&nowT);

      String ^strT = nowT.ToString();

    Of course it doesn't like my "ToString()" above...  Any ideas???

    Thanks!!

    Rob


    • Edited by rwashmore Tuesday, January 13, 2015 3:22 PM
    Tuesday, January 13, 2015 3:22 PM

Answers

  • >I want to convert a SYSTEMTIME to STring as below:

    Rob,

    Have a look at the Windows GetDateFormat & GetTimeFormat APIs.

    Dave

    • Marked as answer by Shu 2017 Thursday, January 22, 2015 9:23 AM
    Tuesday, January 13, 2015 3:39 PM
  • How about a simply Concat something like

     SYSTEMTIME nowT;
     GetSystemTime(&nowT);
     String ^strT = String::Concat(nowT.wDay,nowT.wDayOfWeek,nowT.wHour);

    Thanks


    Rupesh Shukla

    • Marked as answer by Shu 2017 Thursday, January 22, 2015 9:22 AM
    Tuesday, January 13, 2015 3:41 PM
  • On 13/01/2015 16:22, rwashmore wrote:

    Hi - Haven't seen this discussed before so thought I'd ask. I'm very new to all of this.

    I want to convert a SYSTEMTIME to STring as below:

       SYSTEMTIME nowT;

       GetSystemTime(&nowT);

       String ^strT = nowT.ToString();

    Of course it doesn't like my "ToString()" above...  Any ideas???

    You may want to use GetTimeFormatEx() and GetDateFormatEx() APIs (or GetTimeFormat()/GetDateFormat() if you need to target Windows XP, since the ...Ex() APIs are ista+).

    Note that these are native Win32 APIs, that require native C character buffers. But once you have created the native string, you can use it to build a managed C++/CLI String^.

    Giovanni


    • Edited by Giovanni Dicanio Tuesday, January 13, 2015 3:48 PM
    • Marked as answer by Shu 2017 Thursday, January 22, 2015 9:22 AM
    Tuesday, January 13, 2015 3:44 PM
  • Given that you are using C++/CLI, I would probably go the route:

    DateTime nowT = DateTime::Now;
    String ^strT = nowT.ToString(<Whatever format specifier you want here>);

    If you really want to use a SYSTEMTIME (perhaps from another source) then something like:

    SYSTEMTIME nowT;
    GetSystemTime(&nowT);
    DateTime nowDT = new DateTime(nowT.wYear, nowT.wMonth, nowT.wDay, nowT.wHour, nowT.wMinute, nowT.wSecond, nowT.wMilliseconds, DateTimeKind::Utc);
    
    String ^strT = nowDT .ToString(<whatever format you want here>);

    • Marked as answer by Shu 2017 Thursday, January 22, 2015 9:22 AM
    Tuesday, January 13, 2015 4:20 PM

All replies

  • >I want to convert a SYSTEMTIME to STring as below:

    Rob,

    Have a look at the Windows GetDateFormat & GetTimeFormat APIs.

    Dave

    • Marked as answer by Shu 2017 Thursday, January 22, 2015 9:23 AM
    Tuesday, January 13, 2015 3:39 PM
  • How about a simply Concat something like

     SYSTEMTIME nowT;
     GetSystemTime(&nowT);
     String ^strT = String::Concat(nowT.wDay,nowT.wDayOfWeek,nowT.wHour);

    Thanks


    Rupesh Shukla

    • Marked as answer by Shu 2017 Thursday, January 22, 2015 9:22 AM
    Tuesday, January 13, 2015 3:41 PM
  • On 13/01/2015 16:22, rwashmore wrote:

    Hi - Haven't seen this discussed before so thought I'd ask. I'm very new to all of this.

    I want to convert a SYSTEMTIME to STring as below:

       SYSTEMTIME nowT;

       GetSystemTime(&nowT);

       String ^strT = nowT.ToString();

    Of course it doesn't like my "ToString()" above...  Any ideas???

    You may want to use GetTimeFormatEx() and GetDateFormatEx() APIs (or GetTimeFormat()/GetDateFormat() if you need to target Windows XP, since the ...Ex() APIs are ista+).

    Note that these are native Win32 APIs, that require native C character buffers. But once you have created the native string, you can use it to build a managed C++/CLI String^.

    Giovanni


    • Edited by Giovanni Dicanio Tuesday, January 13, 2015 3:48 PM
    • Marked as answer by Shu 2017 Thursday, January 22, 2015 9:22 AM
    Tuesday, January 13, 2015 3:44 PM
  • Given that you are using C++/CLI, I would probably go the route:

    DateTime nowT = DateTime::Now;
    String ^strT = nowT.ToString(<Whatever format specifier you want here>);

    If you really want to use a SYSTEMTIME (perhaps from another source) then something like:

    SYSTEMTIME nowT;
    GetSystemTime(&nowT);
    DateTime nowDT = new DateTime(nowT.wYear, nowT.wMonth, nowT.wDay, nowT.wHour, nowT.wMinute, nowT.wSecond, nowT.wMilliseconds, DateTimeKind::Utc);
    
    String ^strT = nowDT .ToString(<whatever format you want here>);

    • Marked as answer by Shu 2017 Thursday, January 22, 2015 9:22 AM
    Tuesday, January 13, 2015 4:20 PM
  • Will do!!  Thanks Dave!!
    Wednesday, January 14, 2015 4:55 PM
  • Interesting - thanks Rupesh for the code!!!
    • Edited by rwashmore Wednesday, January 14, 2015 4:57 PM
    Wednesday, January 14, 2015 4:56 PM
  • Nice!!  Thanks Giovanni - I'll check it out!!
    Wednesday, January 14, 2015 4:56 PM
  • Two great options - Thanks Simon!!!
    • Edited by rwashmore Wednesday, January 14, 2015 4:58 PM
    Wednesday, January 14, 2015 4:57 PM
  • Interesting - thanks Rupesh for the code!!!

    Actually using DateTime is a better option as it will make your life easy.

    Thanks


    Rupesh Shukla

    Thursday, January 15, 2015 4:00 PM