CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2007
    Posts
    13

    Smile how can i convert DateTime value to string in C++

    //this is my demo program

    using namespace System;
    using namespace System::IO;

    void main()
    {
    DateTime dt = Directory::GetLastWriteTime( "C:\\debug.log" );

    //char str[] = (char)dt;
    string strTemp;
    }

    i want to convert the dt value to a string,
    i tried converting to char array it failed.

    pls do help me.

    Thanks in Advance

  2. #2
    Join Date
    Nov 2007
    Posts
    129

    Smile Re: how can i convert DateTime value to string in C++

    No, No, No...

    Do you even realized what you commented out does?
    [CODE]
    char str[] = (char)dt;
    [\CODE]

    (char)dt is a single character, being the first character in the dt string.
    On second thought, I don't even think it does that.
    You see, DateTime is an entire structure.
    Typecasting to a char will just take the most significant byte of the entire structure.
    Look up the structure if you don't believe me.

    Of course, that doesn't do what you want.

    use the ToString method like this:
    [CODE]
    std::string str = dt.ToString();
    [\CODE]

    Remember this is a C++ string, so you must #include <string>
    Print it out with cout
    Last edited by sjaycohn; February 27th, 2008 at 10:20 AM. Reason: Correctness

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured