Hello i want to get my computer time using c++ i googled, but i couldnt find anything that suits me...

Time code should be able to use this function: if(hours == 10) { do something... }

Can you help me?
Thank you.

Recommended Answers

All 8 Replies

standard library time.h will do what you want. time() returns the current time in seconds since some pre-determined date (epoch), such as 1 Jan 1970. With that you can call localtime() or gmtime() to convert seconds into a structure that contains year, month, day, hour, and seconds.

I found those earlier, they seem to be 'C' and dont run with the rest of mine C++ program...

Yes they work in c++ too. There is no standard c++ time class. Just include <ctime> instead of <time.h>

Sorry im not that smart to do it without example.
I tried this code and it worked! But it's C i cant make it to work with C++

#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <ctime>

int main()
{
while(1 < 2) {
SYSTEMTIME st;
GetSystemTime(&st);
st.wHour = st.wHour + 3;
printf("Hour:%d\nMinutes:%d\n" ,st.wHour,st.wMinute);
if (st.wMinute == 34 ) { blablabla }
system("cls");
}
     system("pause");
     }

But it's C i cant make it to work with C++

Be more specific. Why doesn't it work with C++? Are you getting compilation errors? Runtime errors?

How about you post the EXACT code you're trying. The code you posted here won't work for obvious reasons: blablabla

GetSystemTime() is an MS-Windows api function, not standard C or C++. If that is what you want to do then you don't need <ctime> header file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.