How to print the OS date & time in C

1 Answer

0 votes
#include <stdio.h>
#include <time.h>
 
int main(void)
{
    time_t current_time;
    char *time_string, s[30];
    struct tm *tm_info;
 
    // print date time exampe 1
    current_time = time(NULL); // time as seconds
    time_string = ctime(&current_time); // Convert current_time to date-time format
    printf("%s\n", time_string);
    
    // print date time example 2
    time(&current_time);
    tm_info = localtime(&current_time);
    strftime(s, 30, "%Y:%m:%d - %H:%M:%S", tm_info);
    printf("%s\n", s);
    
    return 0;
}


answered Sep 7, 2014 by avibootz
...