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

    how to get now() or time() into a string?

    Well, I am saving files, and I would like to save them to a folder named after now) or date() and time() , so when my program starts it makes a string with this in it. The string has to be formatted so windows will allow it to be used as a folder name.
    How can i make a string with the contents of Now() or similar in it?
    thanks

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Well...I assume that you are referring to the Visual Basic 'Now" function? Anyway...what about the following...
    Code:
    #include <sstream>
    #include <windows.h>
    
    SYSTEMTIME Time;
    ::GetLocalTime(&Time);
    
    std::stringstream strsName;
    
    strsName << std::setfill('0') 
             << "MyLogfile_"
             << Time.wYear
             << std::setw(2) << Time.wMonth
             << std::setw(2) << Time.wDay
             << "_"
             << std::setw(2) << Time.wHour
             << std::setw(2) << Time.wMinute
             << std::setw(2) << Time.wSecond
             << ".log";

  3. #3
    Join Date
    Jun 2004
    Location
    England
    Posts
    90

    Exclamation

    Code:
      LPSYSTEMTIME lpSystemTime = NULL;
        if(!SetCurrentDirectory(szDir))
        {
          sprintf(g_szErrorString, "Invalid trail file directory name %s - ", szDir);
          return -1;
        }
        lpSystemTime = new SYSTEMTIME;
    
        GetLocalTime(lpSystemTime);
        //Create unique name for trail file depending on current date,
        //i.e. AudTrailYYYYMMDDHHMMSS
        sprintf(m_szTrailFileName, "%s%s%.4u_%.2u_%.2u_%.2u%.2u_%.2u%s", szDir, "\\AudTrail", 
          (UINT)lpSystemTime->wYear, (UINT)lpSystemTime->wMonth, (UINT)lpSystemTime->wDay, 
          (UINT)lpSystemTime->wHour, (UINT)lpSystemTime->wMinute, (UINT)lpSystemTime->wSecond, ".txt");
        break;
    You can use this code, only difference from Andreas is I use sprintf
    Cheers
    The most knowledgeable people are those who know that they know nothing.

  4. #4
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    If you're using MFC, here's another (easier) way:
    Code:
    CTime now = CTime ::GetCurrentTime();
    CString strNow = now.Format("%Y%m%d%H%M%S");

  5. #5
    Join Date
    Jul 2004
    Posts
    182
    Thank you all for the response. What I am looking for is something in this format:
    08_08_2004_8_42_PM
    or similar, so its easily read, and used to create a directory. I don't use the MFC as I develope under borland, so and stl or vcl way would be very appreciated. Perhapse a function that returns a std::string ?
    thanks again

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Code:
    #include <sstream>
    #include <string>
    #include <windows.h>
    
    std::string GetTimestamp()
    {
      SYSTEMTIME Time;
      ::GetLocalTime(&Time);
    
      std::stringstream strsTime;
    
      strsTime << std::setfill('0') 
               << Time.wYear
               << std::setw(2) << Time.wMonth
               << std::setw(2) << Time.wDay
               << "_"
               << std::setw(2) << Time.wHour
               << std::setw(2) << Time.wMinute
               << std::setw(2) << Time.wSecond
               << ".log";
    
      return strsTime.str();
    }
    The formatting can be done as you want...

  7. #7
    Join Date
    Jul 2004
    Posts
    182
    Thank you all very much

  8. #8
    Join Date
    Sep 2004
    Posts
    16

    Re: how to get now() or time() into a string?

    hi gstercken,

    Thankyou very much for suggesting such a easier method.

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