anam ahmed
anam ahmed

Reputation: 138

C program to input date as dd-mm-yy and display date as January 3rd, 1999

I'm trying to write a program in C, read data from the keyboard in the form dd-mm-yy and want to display the date as January 3rd, 1999. How can i do it?

Upvotes: 0

Views: 1712

Answers (2)

Masum billah
Masum billah

Reputation: 982

Try it, hope helps to solve your problem

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

int main()
{
   struct tm tm_info;
   char buffer[255];
   char days[32][5] = {" ","1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th","21st","22nd","23rd","24th","25th","26th","27th","28th","29th","30th","31st"};

    strptime("09-07-2017", "%d-%m-%Y ", &tm_info);
    strftime(buffer, 26, "%B", &tm_info);
    printf("%s ",buffer);

    strftime(buffer, 26, "%d", &tm_info);
    int day = int(buffer[0]-'0')*10 + int(buffer[1]-'0');
    printf("%s, ",days[day]);

    strftime(buffer, 26, "%Y", &tm_info);
    printf("%s ",buffer);
}

Upvotes: 1

jbsu32
jbsu32

Reputation: 1064

Use strptime & strftime. Like this ->

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

int main()
{
   struct tm tm;
   char buf[255];

   memset(&tm, 0, sizeof(struct tm));
   strptime("3-1-1999", "%d-%m-%Y ", &tm);
   strftime(buf, sizeof(buf), "%d %b %Y", &tm);
   puts(buf);
   exit(EXIT_SUCCESS);
}

It will show the output =>

03 Jan 1999

Upvotes: 0

Related Questions