我有一个变量:char date[11];
,我需要将当前日期放入其中,例如 29/06/2012
.
所以我会做类似的事情:
printf ("%s\n", date);
输出将是:29/06/2012
我只找到了用文字打印日期的选项,比如 Fri, June 2012
,但不是数字中的实际日期。
那么我怎样才能用数字打印当前日期呢?
最佳答案
可以引用这个函数strftime .我会让你弄清楚如何使用它:-)
既然你说你已经搜索过了,那我就给出答案:
// first of all, you need to include time.h
#include<time.h>
int main() {
// then you'll get the raw time from the low level "time" function
time_t raw;
time(&raw);
// if you notice, "strftime" takes a "tm" structure.
// that's what we'll be doing: convert "time_t" to "tm"
struct tm *time_ptr;
time_ptr = localtime(&raw);
// now with the "tm", you can format it to a buffer
char date[11];
strftime(date, 11, "%d/%m/%Y", time_ptr);
printf("Today is: %s\n", date);
}
关于c - 获取变量的当前日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11268603/