㈠ c语言中如何将年月日时分秒值转为秒数用unsigned int类型存储起来
"C/C++,time()返值long类型,32位系统,与unsignedint度相等19701月10:0:0现秒数C/C++整套关time函数,time.h,基于秒数
㈡ C语言时间,怎么把time_t类型的时间,转化成年、月、日、时、分、秒呢
可以使用gmtime函数或localtime函数将time_t类型的时间日期转换为struct tm类型(年、月、日、时、分、秒)。
使用time函数返回的是一个long值,该值对用户的意义不大,一般不能根据其值确定具体的年、月、日等数据。gmtime函数可以方便的对time_t类型数据进行转换,将其转换为tm结构的数据方便数据阅读。gmtime函数的原型如下:struct tm *gmtime(time_t *timep);localtime函数的原型如下:struct tm *localtime(time_t *timep);将参数timep所指的time_t类型信息转换成实际所使用的时间日期表示方法,将结果返回到结构tm结构类型的变量。gmtime函数用来存放实际日期时间的结构变量是静态分配的,每次调用gmtime函数都将重写该结构变量。如果希望保存结构变量中的内容,必须将其复制到tm结构的另一个变量中。gmtime函数与localtime函数的区别:gmtime函数返回的时间日期未经时区转换,是UTC时间(又称为世界时间,即格林尼治时间)。localtime函数返回当前时区的时间。
转换日期时间表示形式time_t类型转换为struct tm类型示例:
#include <stdio.h>
#include <time.h>
int main()
{
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};/*指针字符数组*/ time_t t;
struct tm *p;
t=time(NULL);/*获取从1970年1月1日零时到现在的秒数,保存到变量t中*/ p=gmtime(&t); /*变量t的值转换为实际日期时间的表示格式*/
printf("%d年%02d月%02d日",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf(" %s ", wday[p->tm_wday]);
printf("%02d:%02d:%02d\n", p->tm_hour, p->tm_min, p->tm_sec);
return 0;
}
注意:p=gmtime(&t);此行若改为p=localtime(&t);则返回当前时区的时间。
㈢ 1. C语言编程,怎么编写 时 分 秒 的程序
#include<stdio.h>
int main(){
int hour,minute,second;
printf("请输入时间:");
scanf("%d:%d:%d",&hour,&minute,&second);
printf("Time:%02d:%02d:%02d\n",hour,minute,second);
return 0;
}
㈣ C语言秒的转换
根据输入的秒数,转换成相应的时,分,秒数据输出过程为:定义变量h,m,s来存储转换结果定义seconds变量,接收用户输入得到小时数:h=seconds/3600;去除小时数:seconds%=3600; 得到分钟数:m=seconds/60;得到秒数:s=seconds%60;输出结果参考代码:#includeint main(){ int h,m,s,seconds; printf("input sec: ");scanf("%d", &seconds ); h=seconds/3600; seconds %= 3600 ; m=seconds/60; s=seconds%60; printf("%d:%d:%d\n", h,m,s ); return 0;}运行结果:input sec: 145674:2:47
㈤ 关于C语言中“年月日时分秒”的算法。欢迎各位解答。
我只解时分秒。年月日自己去揣摩:
(秒:mm,分:ff,时:ss)
设置一个定时器,时间为一秒,一秒过后 : mm++,
如果mm大于60; mm=0 && ff++ ;
如果 ff 大于60 ff=0 && ss++ ;
如果 ss 大于24 ss=0 && …….
剩下的问题,就是把mm,ff,ss,分别个十位分开并且显示出来(用数码管或者LED)
㈥ 用c语言编一个时钟程序实现时分秒计时功能
#include<stdio.h>
#include<time.h>
int main()
{
time_t t1,t2;
struct tm *ptm;
char timestr[128]={'\0'};
time(&t1);
while(1)
{
time(&t2);
if(difftime(t2,t1)>=1)
{
system("cls");
ptm=localtime(&t2);
strftime(timestr,128,"%H:%M:%S",ptm);
printf("%s\n",timestr);
t1=t2;
}
}
system("PAUSE");
return 0;
}
㈦ C语言如何获取本地时间,然后取时、分、秒的值
#include <stdio.h>
#include <time.h>
int main()
{time_t timep;
struct tm *tp;
time(&timep);
int p;
tp = localtime(&timep); //取得系统时间
printf("Today is %d-%d-%d ", (1900 + tp->tm_year), (1 + tp->tm_mon), tp->tm_mday);
printf("Now is %d:%02d:%02d ", tp->tm_hour, tp->tm_min, tp->tm_sec);
p=tp->tm_sec;
printf("p=%d ",p);
return 0;
}