❶ 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 2015
C语言还提供了将秒数转换成相应的时间结构的函数:
struct tm * gmtime(const time_t *timer); //将日历时间转化为世界标准时间(即格林尼治时间)
struct tm * localtime(const time_t * timer); //将日历时间转化为本地时间
将通过time()函数返回的值,转换成时间结构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()为负。*/
};
编程者可以根据程序功能的情况,灵活的进行日期的读取与输出了。
例如:
#include<time.h>
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*/
}
❷ 如何用C语言获取当前系统时间
需要利用C语言的时间函数time和localtime,具体说明如下:
一、函数接口介绍:
1、time函数。
形式为time_t time (time_t *__timer);
其中time_t为time.h定义的结构体,一般为长整型。
这个函数会获取当前时间,并返回。 如果参数__timer非空,会存储相同值到__timer指向的内存中。
time函数返回的为unix时间戳,即从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒。
由于是秒作为单位的,所以这并不是习惯上的时间,要转为习惯上的年月日时间形式就需要另外一个函数了。
2、localtime函数。
形式为struct tm *localtime (const time_t *__timer);
其中tm为一个结构体,包含了年月日时分秒等信息。
这种结构是适合用来输出的。
二、参考代码:
#include<stdio.h>
#include<time.h>
intmain()
{
time_tt;
structtm*lt;
time(&t);//获取Unix时间戳。
lt=localtime(&t);//转为时间结构。
printf("%d/%d/%d%d:%d:%d ",lt->tm_year+1900,lt->tm_mon,lt->tm_mday,lt->tm_hour,lt->tm_min,lt->tm_sec);//输出结果
return0;
}
注意事项:
struct tm中的tm_year 值为实际年减去1900, 所以输出的时候要是lt->tm_year+1900。
❸ c# Windows窗体 状态栏上显示系统当前时间。应该如何实现
DateTime.Now.Tostring();
❹ C#如何写程序:“显示当前时间”
C#中获取当前时间方法:
DateTime 数字型
System.DateTime currentTime=new System.DateTime();
取当前年月日时分秒 currentTime=System.DateTime.Now;
取当前年 int 年=currentTime.Year;
取当前月 int 月=currentTime.Month;
取当前日 int 日=currentTime.Day;
取当前时 int 时=currentTime.Hour;
取当前分 int 分=currentTime.Minute;
取当前秒 int 秒=currentTime.Second;
取当前毫秒 int 毫秒=currentTime.Millisecond; (变量可用中文)
取中文日期显示——年月日时分 string strY=currentTime.ToString("f"); //不显示秒
取中文日期显示_年月 string strYM=currentTime.ToString("y");
取中文日期显示_月日 string strMD=currentTime.ToString("m");
取当前年月日,格式为:2003-9-23 string strYMD=currentTime.ToString("d");
取当前时分,格式为:14:24 string strT=currentTime.ToString("t");
DateTime.Now.ToString();//获取当前系统时间完整的日期和时间
DateTime.Now.ToLongDateString();//只显示日期 xxxx年xx月xx日,一个是长日期
DateTime.Now.ToShortDateString();//只显示日期 xxxx-xx-xx 一个是短日期
//今天 DateTime.Now.Date.ToShortDateString();
//昨天的 DateTime.Now.AddDays(-1).ToShortDateString();
//明天的 DateTime.Now.AddDays(1).ToShortDateString();
//本周(注意这里的每一周是从周日始至周六止)
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
//上周,上周就是本周再减去7天
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
//下周 本周再加上7天
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
//本月 本月的第一天是1号,最后一天就是下个月一号再减一天。
DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1"; //第一天
DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + "1").AddMonths(1).AddDays(-1).ToShortDateString();//最后一天
另一种方法:
DateTime now = DateTime.Now;
DateTime d1 = new DateTime(now.Year, now.Month, 1); //本月第一天
DateTime d2 = d1.AddMonths(1).AddDays(-1); //本月最后一天
PS:
DateTime.Now.DayOfWeek.ToString();//英文星期显示,Wednesday
(int)DateTime.Now.DayOfWeek 数字,若是周三,结果对应为3
DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn")); //中文星期显示
DateTime.Now.ToString("dddd");//中文星期显示
DateTime.Now.ToString("dddd,MMMM,dd ,yyyy", new System.Globalization.DateTimeFormatInfo());//显示日期格式Friday,July, 01,2009
DateTime.Now.ToString("dddd,dd MMMM,yyyy") //输出 星期三,30 一月,2008
❺ c++语言中如何实现显示系统时间
c++语言中可调用time()函数获得一个时间值,该时间值是从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数,数据类型为long。可以调用ctime()函数进行时间转换输出。
如果进行停车收费计算和显示,可如下操作:
定义两个long类型变量,in_time, out_time;用存储进场时间和离场时间
用out_time-in_time得到停车时间(秒),根据自定义收费规则进行计费
用ctime( &in_time)来显示可视进场时间
用ctime( &out_time)来显示可视进场时间
参考代码如下:
#include <ctime>
#include <iostream>
#include <windows.h>
using namespace std ;
void main(void)
{
long in_time, out_time;
time(&in_time); //得到入场时间
cout << "in time: " <<ctime(&in_time) <<endl ;
//停留
Sleep(3000); //模拟停三秒
time(&out_time);
cout << "out time: " <<ctime(&out_time) <<endl ;
cout << "stop time: " <<out_time-in_time <<endl ;
}
❻ c语言设计的系统中怎么显示实时的时间,
可以调用 time.h 里的时间函数显示 实时的时间。
例如:
#include <stdio.h>
#include <time.h>
int main(){
time_t t;
struct tm * timeinfo;
time(&t);
timeinfo = localtime ( &t );
printf ( "The current date/time is: %s", asctime (timeinfo) );
return 0;
}
调用DOS date time 命令显示 日期和时间也可以:
system("date /T"); system("time /T");
❼ C语言中怎样调用系统时间并动态显示!
void Time() //系统时间
{
printf("\n\n************* 欢迎进入*********系统 *************\n");
printf("\n\n 版本所属: ****** \n");
printf("\n\n *****\n\n ");
CString sDate;
CString *p;
p = &sDate;
while(1)
{
CTime Now=CTime::GetCurrentTime();
*p=Now.Format("%Y年 %m月 %d日 %H时 %M分 %S秒");
printf("%s", *p);
Sleep(1000);
for(int i=1; i<=strlen(*p); i++)
{
printf("\b"); //
printf(" ");
printf("\b");
}
}
printf("\n *******\n");
}
当运行之后,虽然实现了 实现动态显示当前系统时间, 但是无法继续执行 Time函数后面的程序了,即无法继续执行下面摇奖程序了。
如果是使用MFC来做,那么这种问题是不存在的,直接使用C语言,运行于DOS窗口。
❽ 用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*/
}
方法二.#include<stdio.h>
#include<time.h>
intmain()
{
time_tt
structtm*lt;time(&t);//获取Unix时间戳。
lt=localtime(&t);//转为时间结构。
printf("%d/%d/%d%d:%d:%d ",lt->tm_year+1900,lt->tm_mon,lt->tm_mday,
lt->tm_hour,lt->tm_min,lt->tm_sec);//输出结果
return0;}
(8)C使用工具箱显示当前时间扩展阅读
1、CTimeSpan类
如果想计算两段时间的差值,可以使用CTimeSpan类,具体使用方法如下:
CTime t1( 1999, 3, 19, 22, 15, 0 );
CTime t = CTime::GetCurrentTime();
CTimeSpan span=t-t1; //计算当前系统时间与时间t1的间隔
int iDay=span.GetDays(); //获取这段时间间隔共有多少天
int iHour=span.GetTotalHours(); //获取总共有多少小时
int iMin=span.GetTotalMinutes();//获取总共有多少分钟
int iSec=span.GetTotalSeconds();//获取总共有多少秒
2、timeb()函数
_timeb定义在SYSTIMEB.H,有四个fields
dstflag
millitm
time
timezone
void _ftime( struct _timeb *timeptr );
struct _timeb timebuffer;
_ftime( &timebuffer );
❾ C语言显示系统时间
#include<stdio.h>
#include<time.h>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("Thecurrentdate/timeis:%s",asctime(timeinfo));
return0;
}
time_t//时间类型(time.h定义)
structtm{//时间结构,time.h定义如下:
inttm_sec;
inttm_min;
inttm_hour;
inttm_mday;
inttm_mon;
inttm_year;
inttm_wday;
inttm_yday;
inttm_isdst;
}
time(&rawtime);//获取时间,以秒计,从1970年1月一日起算,存于rawtime
localtime(&rawtime);//转为当地时间,tm时间结构
asctime()//转为标准ASCII时间格式:
//就是直接打印tm,tm_year从1900年计算,所以要加1900,月tm_mon,从0计算,所以要加1