How to adjust the format dd/mm/yyyy

Asked

Viewed 562 times

1

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

#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 date provided in a variable and I suppose, from the statement of my exercise, that it should be in the dd/mm/yyyy format, because there is a later need to search through the date.

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

There is some doubt in the statement about the date being in the format above, but I don’t want to take the risk and try for this format. The survey I referred to is the user informing two dates (start and end) and search all the records made on this date.

Any questions, just ask.

1 answer

3


If you only want to store in a variable, you can use sprintf in place 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 );
}
  • Thanks for the help! I think it will work perfectly as I need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.