How to display current date and time in C

2 Answers

0 votes
#include <stdio.h>
#include <time.h>

int main(void) {
    time_t t = time(NULL);
    
    printf("%s", ctime(&t));
}




/*
run:

Sat Jun 26 06:52:42 2021

*/

 



answered Jun 26, 2021 by avibootz
0 votes
#include <stdio.h>
#include <time.h>

int main(void) {
    time_t t = time(NULL);
    
    struct tm lm = *localtime(&t);
    
    printf("%d-%02d-%02d %02d:%02d:%02d", lm.tm_mday, lm.tm_mon + 1, 
                                          lm.tm_year + 1900, lm.tm_hour, 
                                          lm.tm_min, lm.tm_sec);
}




/*
run:

26-06-2021 06:55:03

*/

 



answered Jun 26, 2021 by avibootz

Related questions

1 answer 161 views
1 answer 163 views
1 answer 144 views
1 answer 122 views
1 answer 153 views
1 answer 121 views
...