Get the current date directly from the machine

2

I need to implement the functionality of getting the date directly from the machine in my project (library system). I wanted it in the format dia/mês/ano , to generate the fine for book delivery delay automatically, however, I can only get it this way:

IsitpossibletogetthedatedirectlyfromthemachineintheformatIspokeof?

Followthecode:

#include<stdio.h>#include<time.h>intmain(void){time_tmytime;mytime=time(NULL);printf(ctime(&mytime));return0;}

link

    
asked by anonymous 16.10.2016 / 15:51

1 answer

2

You have to use the localtime() function to give you the date in a structure, then can mount as you wish. There are people who create functions ready to abstract it.

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

int main(void) {
    time_t mytime;
    mytime = time(NULL);
    struct tm tm = *localtime(&mytime);
    printf("Data: %d/%d/%d/\n", tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900);
}

See working on ideone and C ++ Shell >.

    
16.10.2016 / 16:00