C程序输入日期为dd-mm-yy,显示日期为1999年1月3日

时间:2017-07-09 04:47:43

标签: c datetime

我正在尝试撰写program in C,以dd-mm-yy格式从键盘读取数据,并希望将日期显示为January 3rd, 1999。 我该怎么办?

2 个答案:

答案 0 :(得分:1)

尝试一下,希望有助于解决您的问题

#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);
}

答案 1 :(得分:0)

使用strptime&amp; strftime。像这样 - &gt;

#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);
}

它将显示输出=&gt;

03 Jan 1999