Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,940 questions

51,877 answers

573 users

How to get the current (now) date and time value in C

3 Answers

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

int main(int argc, char **argv) 
{ 
    time_t t = time(NULL);
    struct tm dt = *localtime(&t);
    
    printf("date time now: %d-%d-%d %d:%d:%d\n", dt.tm_year + 1900, 
                                                 dt.tm_mon + 1, 
                                                 dt.tm_mday, 
                                                 dt.tm_hour, 
                                                 dt.tm_min, 
                                                 dt.tm_sec);
    
    return(0);
}

/*
run:

date time now: 2015-6-16 9:54:49

*/

 



answered Jun 16, 2015 by avibootz
0 votes
#include <stdio.h>
#include <time.h>

int main(int argc, char **argv) 
{ 
    time_t t = time(NULL);
    struct tm *dt = localtime(&t);
    char s_dt[50];
    
    strftime(s_dt, sizeof(s_dt), "%c", dt);
    printf("date time (now) = %s\n", s_dt);
    
    return(0);
}

/*
run:

date time (now) = 06/16/15 11:55:06

*/

 



answered Jun 16, 2015 by avibootz
0 votes
#include <stdio.h>
#include <time.h>

int main(int argc, char **argv) 
{ 
    time_t t;
    struct tm *dt;
    time(&t);
    dt = localtime(&t);
    
    printf("date time now: %d-%d-%d %d:%d:%d\n", dt->tm_year + 1900, 
                                                 dt->tm_mon + 1, 
                                                 dt->tm_mday, 
                                                 dt->tm_hour, 
                                                 dt->tm_min, 
                                                 dt->tm_sec);
    
    return(0);
}

/*
run:

date time now: 2015-7-11 10:55:24

*/

 



answered Jul 11, 2015 by avibootz

Related questions

6 answers 398 views
1 answer 95 views
1 answer 131 views
1 answer 91 views
...