C语言中,怎样获得当前年份和月份?

要求,年份和月份分别赋值给i和k!!

第1个回答  2013-02-28
若用标准c语言库
#include<stdio.h>
#include<time.h>
int main()
{
time_t now;
time(&now);
printf("\nNow is:%s\n",ctime(&now));
return 0;
}
若用TC编程器
#include<dos.h>
#include<stdio.h>
main()
{
struct date d;
struct time t;
getdate(&d);
gettime(&t);
printf("the current day is:%d-%d-%d\n",d.da_year,d.da_mon,d.da_day);
printf("the current time is:%d:%d:%d",t.ti_hour,t.ti_min,t.ti_sec);
getch();
}追问

我希望把年份直接赋值个I,月份直接赋值给K

追答

那 直接 用 getdate(&d)后 加 l=d.da_year;k=d.da_mon 就好啦 不过这只能在TC下运行

本回答被提问者和网友采纳
第2个回答  2013-02-28
String date=this.DateTime.now.tostring('yyyy-MM-dd');
String i=substring(date,0,4);
String k=substring(date,5,7);追问

String ?有错误提示
'String' : undeclared identifier

第3个回答  2013-02-28
补充:
vc的话 CTime now=CTime::GetCurrentTime();
int year=now.GetYear();
int month=now.GetMonth();
i=year;k=month;追问

#include#include
main(){
int i,k;
CTime now=CTime::GetCurrentTime();
int year=now.GetYear();
int month=now.GetMonth();
i=year;k=month;
printf("%d年%d月",i,k);
}

根据你的意思,我写了这么一个代码。可是运行的时候有错误提示
可以解释清楚些吗?写个完整的代码给我吧

第4个回答  2013-02-28
你指的是从系统,还是输入?
i和k的类型是什么?追问

I和K 的类型都是整型,肯定是从系统啦,直接输入有啥意思

追答

#include

char datebuf[128];
char timebuf[128];

_strdate( datebuf ); //日期 格式是:02/28/13
_strtime( timebuf ); //时间 格式是:17:42:46

追问

我希望的是把年份直接赋值个I,月份直接赋值给K,而不是输出整个日期

追答

根据字符还不好取值计算啊?