Get current time in C

I want to get the current time of my system. For this, I use the following code in C:

time_t now; struct tm *mytime = localtime(&now); if ( strftime(buffer, sizeof buffer, "%X", mytime) ) { printf("time1 = \"%s\"\n", buffer); } 

The problem is that this code gives some random time. In addition, random time is different every time. I need the current time of my system.

+62
c time time-t
Feb 28 '11 at 12:27
source share
8 answers

Copied from here :

 /* localtime example */ #include <stdio.h> #include <time.h> int main () { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); printf ( "Current local time and date: %s", asctime (timeinfo) ); return 0; } 

(just add "void" to the main () argument list for this to work in C)

+88
Feb 28 '11 at 12:33
source share

Initialize the now variable.

 time_t now = time(0); // Get the system time 

The localtime function is used to convert the time value in the passed time_t to struct tm , it does not actually receive the system time.

+54
Feb 28 '11 at 12:31
source share

simple way:

 #include <time.h> #include <stdio.h> int main(void) { time_t mytime; mytime = time(NULL); printf("Current Time : %s", ctime(&mytime)); return 0; } 
+24
Sep 05 '13 at 12:09 on
source share

To extend the answer from @mingos above, I wrote the following function to format my time in a specific format ([dd mm yyyy hh: mm: ss]).

 // Store the formatted string of time in the output void format_time(char *output){ time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); sprintf(output, "[%d %d %d %d:%d:%d]",timeinfo->tm_mday, timeinfo->tm_mon + 1, timeinfo->tm_year + 1900, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); } 

More information on struct tm can be found here .

+5
May 29 '15 at 4:59
source share
 #include<stdio.h> #include<time.h> void main() { time_t t; time(&t); printf("\n current time is : %s",ctime(&t)); } 
+3
Aug 10 '15 at 4:40
source share

guys, you can use this function to get the current local time. if you want gmtime, use the gmtime function instead of localtime. greetings

 time_t my_time; struct tm * timeinfo; time (&my_time); timeinfo = localtime (&my_time); CCLog("year->%d",timeinfo->tm_year+1900); CCLog("month->%d",timeinfo->tm_mon+1); CCLog("date->%d",timeinfo->tm_mday); CCLog("hour->%d",timeinfo->tm_hour); CCLog("minutes->%d",timeinfo->tm_min); CCLog("seconds->%d",timeinfo->tm_sec); 
+2
Jul 27 '15 at 6:19 06:19
source share
 #include <stdio.h> #include <time.h> void main() { time_t t; time(&t); clrscr(); printf("Today date and time : %s",ctime(&t)); getch(); } 
0
Jan 05 '15 at 7:16
source share

Guys, I got a new way to get system time. although its length is full of stupid work, but in this way you can get the system time in integer format.

 #include <stdio.h> #include <stdlib.h> int main() { FILE *fp; char hc1,hc2,mc1,mc2; int hi1,hi2,mi1,mi2,hour,minute; system("echo %time% >time.txt"); fp=fopen("time.txt","r"); if(fp==NULL) exit(1) ; hc1=fgetc(fp); hc2=fgetc(fp); fgetc(fp); mc1=fgetc(fp); mc2=fgetc(fp); fclose(fp); remove("time.txt"); hi1=hc1; hi2=hc2; mi1=mc1; mi2=mc2; hi1-=48; hi2-=48; mi1-=48; mi2-=48; hour=hi1*10+hi2; minute=mi1*10+mi2; printf("Current time is %d:%d\n",hour,minute); return 0; } 
-four
Oct 31 '13 at
source share



All Articles