❶ 用c语言获取时间
#include<stdio.h>
#include<time.h>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("当前系统时间:%s",asctime(timeinfo));
return0;
}
说明:
time_t // 时间类型(time.h 定义)
struct tm { // 时间结构,time.h 定义如下:
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}
time ( &rawtime ); // 获取时间,以秒计,从1970年1月一日起算,存于rawtime
localtime ( &rawtime ); //转为当地时间,tm 时间结构
asctime() // 转为标准ASCII时间格式:
//就是直接打印tm,tm_year 从1900年计算,所以要加1900,月tm_mon,从0计算,所以要加1
❷ C语言中如何获取当前系统时间的小时
程序主要通过当前系统日历的struct tm结构体获得,主要代码如下,
#include <stdio.h>
#include <time.h>
//程序功能输出当前时间在24H下的小时数
int main(int argc, char *argv[])
{
struct tm *ptr;
time_t lt;
time(<);//当前系统时间
ptr=localtime(<);//获取本地日历时间指针
printf("hour=%d(24H )\n",ptr->tm_hour);//输出24H下的小时数
return 0;
}
结构体tm定义如下,
struct tm {
int tm_sec; /* 秒–取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值从1900开始 */
int tm_wday; /* 星期–取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数–取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
long int tm_gmtoff; /*指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数*/
const char *tm_zone; /*当前时区的名字(与环境变量TZ有关)*/
};
❸ 用c语言如何获取系统当前时间的函数
1、C语言中读取系统时间的函数为time(),其函数原型为:
#include <time.h>
time_t time( time_t * ) ;
time_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。
2、C语言还提供了将秒数转换成相应的时间格式的函数:
char * ctime(const time_t *timer); //将日历时间转换成本地时间,返回转换后的字符串指针 可定义字符串或是字符指针来接收返回值
struct tm * gmtime(const time_t *timer); //将日历时间转化为世界标准时间(即格林尼治时间),返回结构体指针 可定义struct tm *变量来接收结果
struct tm * localtime(const time_t * timer); //将日历时间转化为本地时间,返回结构体指针 可定义struct tm *变量来接收结果
3、例程:
#include <time.h>
void main()
{
time_t t;
struct tm *pt ;
char *pc ;
time(&t);
pc=ctime(&t) ; printf("ctime:%s", pc );
pt=localtime(&t) ; printf("year=%d", pt->tm_year+1900 );
}
时间结构体struct tm 说明:
struct tm {
int tm_sec; /* 秒 – 取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值等于实际年份减去1900 */
int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
};
❹ C语言中 如何获取系统时间
#include<time.h>
int main()
{
time_t timep;
struct tm *p;
time (&timep);
p=gmtime(&timep);
printf("%d ",p->tm_sec); /*获取当前秒*/
printf("%d ",p->tm_min); /*获取当前分*/
printf("%d ",8+p->tm_hour);/*获取当前时,这里获取西方的时间,刚好相差八个小时*/
printf("%d ",p->tm_mday);/*获取当前月份日数,范围是1-31*/
printf("%d ",1+p->tm_mon);/*获取当前月份,范围是0-11,所以要加1*/
printf("%d ",1900+p->tm_year);/*获取当前年份,从1900开始,所以要加1900*/
printf("%d ",p->tm_yday); /*从今年1月1日算起至今的天数,范围为0-365*/
}
拓展资料:
C语言是一门通用计算机编程语言,广泛应用于底层开发。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。
尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。
二十世纪八十年代,为了避免各开发厂商用的C语言语法产生差异,由美国国家标准局为C语言制定了一套完整的美国国家标准语法,称为ANSI C,作为C语言最初的标准。目前2011年12月8日,国际标准化组织(ISO)和国际电工委员会(IEC)发布的C11标准是C语言的第三个官方标准,也是C语言的最新标准,该标准更好的支持了汉字函数名和汉字标识符,一定程度上实现了汉字编程。
C语言是一门面向过程的计算机编程语言,与C++,Java等面向对象的编程语言有所不同。
其编译器主要有Clang、GCC、WIN-TC、SUBLIME、MSVC、Turbo C等。
❺ c语言中获取当前时间的代码,求解释!!
(1)
time_ttime(time_t*timer);
The function returns this value, and if the argument is not a null pointer,
the value is also set to the object pointed by timer.
(2)
structtm*localtime(consttime_t*timer);
Uses the time pointed by timer to fill a tm
structure with the values that represent the corresponding local time.
(3)
structtm{
inttm_sec;/*secondsaftertheminute-[0,59]*/
inttm_min;/*minutesafterthehour-[0,59]*/
inttm_hour;/*hourssincemidnight-[0,23]*/
inttm_mday;/*dayofthemonth-[1,31]*/
inttm_mon;/*monthssinceJanuary-[0,11]*/
inttm_year;/*yearssince1900*/
inttm_wday;/*dayssinceSunday-[0,6]*/
inttm_yday;/*dayssinceJanuary1-[0,365]*/
inttm_isdst;/*daylightsavingstimeflag*/
};
( 4 )
intatoi(constchar*str);
Convert a string to integer.
你代码中的if语句就是拿当前时间records中第i个时间比较,如果当前时间的小时不大于records[i]的小时,且分钟小于records[i]中的分钟,则返回1(应该是没超出),否则返回0(超出)。
懂了吗,宝贝?
❻ 看过来,看过来 C语言获取系统时间的几种方式
我们在写C语言程序的时候,有的时候会用到读取本机的时间和日期,怎么做呢?其实很简单的,下面简单说一下:
C语言中读取系统时间的函数为time(),其函数原型为:#include <time.h>time_t time( time_t * ) ;
time_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。
可以调用ctime()函数进行时间转换输出:char * ctime(const time_t *timer);
将日历时间转换成本地时间,按年月日格式,进行输出,如:Wed Sep 23 08:43:03 2015C语言还提供了将秒数转换成相应的时间结构的函数:
struct tm * gmtime(const time_t *timer);//将日历时间转化为世界标准时间(即格林尼治时间)
struct tm * localtime(const time_t * timer);//将日历时间转为本地时间将通过time()函数返回的值,转成时间结构structtm :
struct tm {int tm_sec; /* 秒 – 取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值等于实际年份减去1900 */
int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/};
编程者可以根据程序功能的情况,灵活的进行日期的读取与输出了。
下面给出一段简单的代码:
#include<time.h>
intmain()
{
time_ttimep;
structtm*p;
time(&timep);
p=gmtime(&timep);
printf("%d ",p->tm_sec);/*获取当前秒*/
printf("%d ",p->tm_min);/*获取当前分*/
printf("%d ",8+p->tm_hour);/*获取当前时,这里获取西方的时间,刚好相差八个小时*/
printf("%d ",p->tm_mday);/*获取当前月份日数,范围是1-31*/
printf("%d ",1+p->tm_mon);/*获取当前月份,范围是0-11,所以要加1*/
printf("%d ",1900+p->tm_year);/*获取当前年份,从1900开始,所以要加1900*/
printf("%d ",p->tm_yday);/*从今年1月1日算起至今的天数,范围为0-365*/
}
❼ 标准C语言获取系统时间
1、代码如下:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
#include <time.h>
void main()
{
time_t t = time(NULL);
tm *tmTemp=localtime(&t);
printf("%04d-%02d-%02d %02d:%02d:%02d", tmTemp->tm_year+1900,tmTemp->tm_mon+1,tmTemp->tm_mday ,tmTemp->tm_hour,tmTemp->tm_min,tmTemp->tm_sec);
system("PAUSE");
}
2、不知道你说的"选择在两个时间中间的所有信息"是什么意思?
❽ c语言中取系统时间
主要分为两种方法:
1.这种方法比较高级
#include<time.h>
#include<stdio.h>
#include<time.h>
intmain(intargc,char**argv)
{
time_ttemp;
structtm*t;
time(&temp);
t=localtime(&temp);
printf("当前时间是: %d年%d月%d日 ",t->tm_year+1900,t->tm_mon+1,t->tm_mday);
printf("%d时%d分%d秒 ",t->tm_hour,t->tm_min,t->tm_sec);
/*
t结构体内的成员变量还有以下几个:
tm_wday 星期的第几天 tm_yday 这天是这年的第几天
*/
return0;
}
需要注意的是tm_year返回的是1900年之后的年数,tm_mon返回的比实际月份小1(至于为什么要这样设计,我不是太清楚)
2.这种方法较为简单方便,但是同时可能会对接下来的其它操作不利。
#include<time.h>
#include<stdio.h>
intmain(intargc,char**argv)
{
time_ttemp;
time(&temp);
printf("当前时间为: %s",ctime(&temp));
return0;
}
❾ C语言中 如何获取系统时间
方法一,#include<time.h>
int
main()
{
time_t
timep;
struct
tm
*p;
time
(&timep);
p=gmtime(&timep);
printf("%d\n",p->tm_sec);
/*获取当前秒*/
printf("%d\n",p->tm_min);
/*获取当前分*/
printf("%d\n",8+p->tm_hour);/*获取当前时,这里获取西方的时间,刚好相差八个小时*/
printf("%d\n",p->tm_mday);/*获取当前月份日数,范围是1-31*/
printf("%d\n",1+p->tm_mon);/*获取当前月份,范围是0-11,所以要加1*/
printf("%d\n",1900+p->tm_year);/*获取当前年份,从1900开始,所以要加1900*/
printf("%d\n",p->tm_yday);
/*从今年1月1日算起至今的天数,范围为0-365*/
}
方法二.#include <stdio.h>
#include <time.h>
int main ()
{
time_t t
struct tm * lt; time (&t);//获取Unix时间戳。
lt = localtime (&t);//转为时间结构。
printf ( "%d/%d/%d %d:%d:%d\n",lt->tm_year+1900, lt->tm_mon, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec);//输出结果
return 0;}
(9)c语言获得系统时间代码扩展阅读
#include
--
必须的时间函数头文件
time_t
--
时间类型(time.h
定义是typedef
long
time_t;
追根溯源,time_t是long)
struct
tm
--
时间结构,time.h
定义如下:
int
tm_sec;
int
tm_min;
int
tm_hour;
int
tm_mday;
int
tm_mon;
int
tm_year;
int
tm_wday;
int
tm_yday;
int
tm_isdst;
time
(
&rawtime
);
--
获取时间,以秒计,从1970年1月一日起算,存于rawtime
localtime
(
&rawtime
);
--
转为当地时间,tm
时间结构
asctime
()--
转为标准ASCII时间格式:
星期
月
日
时:分:秒
年
参考资料来源:网络
time函数
❿ C语言获取系统时间
#include <stdio.h>
#include <time.h>
void main ()
{ time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "\007The current date/time is: %s", asctime (timeinfo) );
exit(0);
}
=================
#include <time.h> -- 必须的时间函数头文件
time_t -- 时间类型(time.h 定义)
struct tm -- 时间结构,
time.h 定义如下:
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
time ( &rawtime ); -- 获取时间,
以秒计,从1970年1月一日起算,存于rawtime localtime ( &rawtime ); -- 转为当地时间,tm 时间结构
asctime ()-- 转为标准ASCII时间格式: 星期 月 日 时:分:秒 年 =========================================
你要的格式可这样输出: printf ( "M-d-d d:d:d\n",1900+timeinfo->tm_year, 1+timeinfo->tm_mon, timeinfo->tm_mday,timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec); 就是直接打印tm,tm_year 从1900年计算,所以要加1900, 月tm_mon,从0计算,所以要加1 其它你一目了然啦。