I have something like this:
char *current_day, *current_time; system("date +%F"); system("date +%T");
It prints the current day and time in the stdout, but I want to get this output or assign them to the current_day
and current_time
variables, so that I can do some processing with those values later on.
current_day ==> current day current_time ==> current time
The only solution that I can think of now is to direct the output to some file, and then read the file and then assign the values of date and time to current_day
and current_time
. But I think this is not a good way. Is there any other short and elegant way?
printf("The time is: %02d:%02d:%02d\n", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); We use the tm_hour , tm_min , and tm_sec members to express a human-readable time format.
Print Date/Time with Default Format. Print Date in DD-MM-YYYY Format. Print Time in HH:MM:SS Format. Print Date/Time with Month Name and AM/PM Format.
Return Value: This function returns current calendar time as a object of type time_t.
Use time()
and localtime()
to get the time:
#include <stdio.h> #include <time.h> int main() { time_t t = time(NULL); struct tm tm = *localtime(&t); printf("now: %d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With