C 获取时间,包括 年月日,时分秒,以及分别获得

char* GetTime(const char* Mode)/* date Day Mon Year Hour Min Sec */
{
	char* Year ;char *Mon;char *Day;char *Hour;char *Min;char *Sec;
	time_t rawtime;
	struct tm * timeinfo;
	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	char* date = asctime(timeinfo);
	printf("%s\n",date);

	if(strcmp(Mode,"date")==0)
		return date ;
	if(strcmp(Mode,"Year")==0)
	{
		Year = (char*)malloc(3*sizeof(char));
		assert(Year!=NULL);
		memcpy(Year,date+20,4);
		memset(Year+4,0,1);

		printf("%s\n",Year);
		return Year ;
	}
	if(strcmp(Mode,"Mon")==0)
	{
		Mon = (char*)malloc(2*sizeof(char));
		assert(Mon!=NULL);
		memcpy(Mon,date+4,3);
		memset(Mon+3,0,1);

		printf("%s\n",Mon);
		return Mon ;
	}
	if(strcmp(Mode,"Day")==0)
	{
		Day = (char*)malloc(1*sizeof(char));
		assert(Day!=NULL);
		memcpy(Day,date+8,2);
		memset(Day+2,0,1);

		printf("%s\n",Day);
		return Day ;
	}
	if(strcmp(Mode,"Hour")==0)
	{
		Hour = (char*)malloc(1*sizeof(char));
		assert(Hour!=NULL);

		memcpy(Hour,date+11,2);
		memset(Hour+2,0,1);

		printf("%s\n",Hour);
		return Hour ;
	}
	if(strcmp(Mode,"Min")==0)
	{
		Min = (char*)malloc(1*sizeof(char));
		assert(Min!=NULL);

		memcpy(Min,date+14,2);
		memset(Min+2,0,1);

		printf("%s\n",Min);
		return Min ;
	}
	if(strcmp(Mode,"Sec")==0)
	{
		Sec = (char*)malloc(1*sizeof(char));
		assert(Sec!=NULL);

		memcpy(Sec,date+17,2);
		memset(Sec+2,0,1);

		printf("%s\n",Sec);
		return Sec ;
	}
	return NULL;
}





获取的方法例如:

Gettime(“date”)

这样就获得了 整个时间

Gettime("Day")  就获取了日期






你可能感兴趣的:(C 获取时间,包括 年月日,时分秒,以及分别获得)