CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2002
    Location
    London, ON <- Yes That's Canada, not UK
    Posts
    2

    Question NEWBIE - System Time

    This might be easy for most of you, but I am just starting so I need help. I need to get the Current System time and put it into a INT type variable for each of these 3 components (hour, minute, second)

    Any help would be greatly appreciated

    Thanks

  2. #2
    Join Date
    Oct 2002
    Posts
    36
    One way;

    Code:
    #include <iostream>
    #include <windows.h>
    
    int main(void){	
    	
    	SYSTEMTIME st = {0};
    	
    	GetSystemTime(&st);
    	
    	int second = st.wSecond,
    		hour = st.wHour,
    		minute = st.wMinute;
    	
    	
    	std::cout << "Hour:Minute:Second" << std::endl;
    	std::cout << hour << ":" << minute << ":";
    	std::cout << second << std::endl;
    
    }

  3. #3
    Join Date
    Oct 2002
    Location
    London, ON <- Yes That's Canada, not UK
    Posts
    2
    Thanks dumah. the code helped me a lot.

  4. #4
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    If you don't care about portability and you only need to run this
    on Windows systems, the code you have seen is fine. If you want
    to take the code to other platforms, then you could use the
    ANSI approach.
    Code:
    #include <time.h>  // for C
    #include <ctime>   // for C++
    
    char currentTime[256];
    time_t timeBuf;
    struct tm* pTime;
    
    time(&timeBuf);
    pTime = localTime(&timeBuf);
    sprintf(currentTime, "%02d/%02d%04d, %02d:%02d:%02d",
                                    pTime->tm_mon+1, pTime->tm_mday,
                                    (1900 + pTime->tm_year), pTime->tm_hour,
                                    pTime->tm_min, pTime->tm_sec);
    You can find some information on this at Microsoft's ANSI-C
    function page.

    http://msdn.microsoft.com/library/de...nReference.asp

    --Paul

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