我正在为键盘修改一些驱动程序软件,其中一部分是将日期输出到键盘屏幕的插件。目前它是1月1日,但我真的希望它是1、2、3或4日或其他。
我一直在到处寻找某种代码,这些代码将给我一些实现方法的想法,但我只能找到C#的示例,而我正在使用C。
编辑:
const char *ordinals[] = {"", "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"};
sprintf(date, "%s %s", ordinals[t->tm_mday], mon);
最佳答案
由于只需要为1
到31
编号,因此最简单的方法是定义一个序数数组,如下所示:
const char *ordinals[] = {"", "1st", "2nd", "3rd", "4th"..., "31st"};
...
printf("%s of %s", ordinals[dayNumber], monthName);
这比算法上要好,因为它可读性更高,并且更易于国际化,如果您稍后再碰到这一点。
关于c - 将 "th", "nd"或 "rd"添加到C中的日期,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14113010/