How to adjust the format dd / mm / yyyy

1

I have the following code that takes the instant computer date.

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

void main () {
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);

    printf("Agora: %d/%d/%d", tm.tm_mday, tm.tm_mon+1, tm.tm_year+1900);
}

I need to save the given date into a variable and I suppose, by the statement in my exercise, that it should be in the format dd / mm / yyyy, as there is a further need to search through the date.

What would be the easiest method to add the bars? Wipe the vector with the date and in each position go putting the bar (was that what I thought), or is there any more efficient solution?

There is some doubt in the statement as to the date being in the above format, but I do not want to take the risk and try for this format. The research I referred to is the user informing two dates (beginning and end) and searching all the registrations made on this date.

Any questions, please ask.

    
asked by anonymous 24.10.2014 / 02:25

1 answer

3

If you just want to save to a variable, you can use sprintf instead of printf :

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

void main () {
    char minhaString[30];
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);

    sprintf( minhaString, "Agora: %d/%d/%d", tm.tm_mday, tm.tm_mon+1, tm.tm_year+1900 );
}
    
24.10.2014 / 02:31