jpantalion
jpantalion

Reputation: 11

Date format in C

I am trying to change a date entered in the dd/mm/yyyy format to the Month day, year format and I've got it done for the most part but my output adds an extra weird character after the day. This is my code

#include <stdio.h>
#include <string.h>
void main()
{
    char userDate[11];
    char dateWord[11];
    char day[2];
    char year[4];
    printf("Welcome to the Date Formatter.\nPlease input a date in the form of mm/dd/yyyy.\n");
    scanf("%s", userDate);

    day[0] = userDate[3];
    day[1] = userDate[4];
    year[0] = userDate[6];
    year[1] = userDate[7];
    year[2] = userDate[8];
    year[3] = userDate[9];

    if (userDate[0] == '0' && userDate[1] == '1')
        strcpy(dateWord, "January");
    if (userDate[0] == '0' && userDate[1] == '2')
        strcpy(dateWord, "February");
    if (userDate[0] == '0' && userDate[1] == '3')
        strcpy(dateWord, "March");
    if (userDate[0] == '0' && userDate[1] == '4')
        strcpy(dateWord, "April");
    if (userDate[0] == '0' && userDate[1] == '5')
        strcpy(dateWord, "May");
    if (userDate[0] == '0' && userDate[1] == '6')
        strcpy(dateWord, "June");
    if (userDate[0] == '0' && userDate[1] == '7')
        strcpy(dateWord, "July");
    if (userDate[0] == '0' && userDate[1] == '8')
        strcpy(dateWord, "August");
    if (userDate[0] == '0' && userDate[1] == '9')
        strcpy(dateWord, "September");
    if (userDate[0] == '1' && userDate[1] == '0')
        strcpy(dateWord, "October");
    if (userDate[0] == '1' && userDate[1] == '1')
        strcpy(dateWord, "November");
    if (userDate[0] == '1' && userDate[1] == '2')
        strcpy(dateWord, "December");


    printf("The date is:\n");
    printf("%s %s, %s\n", dateWord, day, year);
}

And the output is

c:\CompSci\C Programming>dateconvert
Welcome to the Date Formatter.
Please input a date in the form of mm/dd/yyyy.
01/23/1998
The date is:
January 23╢ , 1998

I'm not sure why the ╢ is being printed.

Upvotes: 0

Views: 14209

Answers (1)

Schwern
Schwern

Reputation: 164679

There's already functions for date parsing and formatting, strptime and strftime. So you can use them. First, use strptime to parse the date into a struct tm.

#include <time.h>

struct tm date;
strptime( userDate, "%m/%d/%Y", &date );

The date is now in a struct tm and from there you can manipulate it with the normal date functions including strftime for formatting.

char formatted_date[40];
strftime( formatted_date, 40, "%B %d, %Y", &date );
puts( formatted_date );

One of the benefits of using strftime is it will honor the user's locale and provide the month in the appropriate language. C won't honor locales by default, you have to call setlocale (LC_ALL, ""); from locale.h.

$ ./test
Welcome to the Date Formatter.
Please input a date in the form of mm/dd/yyyy.
01/02/1999
January 02, 1999

$ LC_ALL=es_ES ./test
Welcome to the Date Formatter.
Please input a date in the form of mm/dd/yyyy.
01/02/1999
enero 02, 1999

Note that your scanf should be limited to the size of the userDate buffer else it can overrun the buffer. %s should always have a limit.

scanf("%10s", userDate);

And that while some compilers will accept void main, it is non-standard. It should always be int main.

Upvotes: 7

Related Questions