得到当前时间的方法一般都是得到从1900年0点0分到现在的秒数,然后转为年月日时分秒的形式得到当前的时间(时分秒)。主要方法如下:

1)使用CRT函数

C++代码  
 char szCurrentDateTime[];
time_t nowtime;
struct tm* ptm;
time(&nowtime);
ptm = localtime(&nowtime);
sprintf(szCurrentDateTime, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
ptm->tm_year + , ptm->tm_mon + , ptm->tm_mday,
ptm->tm_hour, ptm->tm_min, ptm->tm_sec);

2)使用SYSTEMTIME

C++代码  
 char szCurrentDateTime[];
SYSTEMTIME systm;
GetLocalTime(&systm);
sprintf(szCurrentDateTime, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
systm.wYear, systm.wMonth, systm.wDay,
systm.wHour, systm.wMinute, systm.wSecond);

3)使用CTime

C++代码  
 char szCurrentDateTime[];
CTime nowtime;
nowtime = CTime::GetCurrentTime(); sprintf(szCurrentDateTime, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
nowtime.GetYear(), nowtime.GetMonth(), nowtime.GetDay(),
nowtime.GetHour(), nowtime.GetMinute(), nowtime.GetSecond());

用VC++获取系统时间几种方法:

A:

1 使用time_t time( time_t * timer )    精确到秒,计算时间差使用double difftime( time_t timer1, time_t timer0 )
2 使用clock_t clock() 得到的是CPU时间    精确到1/CLOCKS_PER_SEC秒
3 使用DWORD GetTickCount() 得到的是系统运行的时间 精确到毫秒
4 如果使用MFC的CTime类,可以用CTime::GetCurrentTime() 精确到秒
5 要获取高精度时间,可以使用下面的方法,然后用两次计数器的差除以Frequency就得到时间。

     BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)       //获取系统的计数器的频率
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)//获取计数器的值

6 还有David的文章中提到的方法:
    Multimedia Timer Functions
    The following functions are used with multimedia timers.
    timeBeginPeriod/timeEndPeriod/timeGetDevCaps/timeGetSystemTime
    timeGetTime/timeKillEvent/TimeProc/timeSetEvent  精度很高

   
附:
GetTickCount()和GetCurrentTime()都只精确到55ms(1个tick就是55ms)。如果要精确到毫秒,应该使用timeGetTime函数或QueryPerformanceCounter函数
GetSystemTime返回的是格林威志标准时间
GetLocalTime,和上面用法一样,返回的是你所在地区的时间,中国返回的是北京时间
 
VOID GetSystemTime(
LPSYSTEMTIME lpSystemTime // address of system time structure
);
函数就可以获得了,其中LPSYSTEMTIME 是个结构体
含:年,月,日,周几,小时,分,秒,毫秒。

VC++获得当前系统时间的几种方案

//方案- 优点:仅使用C标准库;缺点:只能精确到秒级

 #include < time.h>
#include < stdio.h>
int main( void )
{
time_t t = time( );
char tmp[];
strftime( tmp, sizeof(tmp), " %Y/%m/%d %X %A 本年第%j天 %z" , localtime(&t) );
puts( tmp );
return ;
}

//方案二 优点:能精确到毫秒级;缺点:使用了windows API

 #include < windows.h>
#include < stdio.h>
int main( void )
{
SYSTEMTIME sys;
GetLocalTime( &sys );
printf( " M/d/d d:d:d.d 星期\n"
,sys.wYear,sys.wMonth,sys.wDay
,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds
,sys.wDayOfWeek);
return ;
}

//方案三,优点:利用系统函数

 #include< stdlib.h>
#include< iostream>
using namespace std;
void main(){
system(" time" );
}

可以改变电脑的时间设定

方案4:

 #include< iostream>
#include< ctime>
using namespace std;
int main()
{
time_t now_time;
now_time = time(NULL);
cout< < now_time;
return ;
}

另一:_strdate(tempstr);

另二: CTime

 CString CTestView::GetTime()
{
CTime CurrentTime=CTime::GetCurrentTime();
CString strTime;
strTime.Format(" %d:%d:%d" ,CurrentTime.GetHour(), CurrentTime.GetMinute()
,CurrentTime.GetSecond());
return strTime;
}

VC++ 时间类型转换: CTime, COleDateTime, time_t, CString

CTime==>CString

 CTime time;
time.GetCurrentTime();
CString str;
str.Format("%s",time.Format("%y:%m:%d %H-%M-%S") //方法
CString str;
CTime t = CTime::GetCurrentTime();
str.Format("%d-%d-%d",t.GetYear(),t.GetMonth(),t.GetDay());
//方法
CString strTime;
CTime tTime = CTime::GetCurrentTime();
strTime = tTime.Format("%Y-%m-%d %H:%M:%S");

CString ==>CTime

  //方法1
 strCString="2003-10-27 6:24:37"; //CString--->COleDateTime
COleVariant vtime(strCString);
vtime.ChangeType(VT_DATE);
COleDateTime time4=vtime;
5 //方法2
COleDateTime time1(,,,,,); //COleDataTime--->CTime
SYSTEMTIME systime;
VariantTimeToSystemTime(time1, &systime);
CTime tm(systime);
10 //方法3
time_t time2=tm.GetTime(); //CTime--->time_t
COleDateTime time3(time2); //time_t--->COleDateTime

时间差

 COleDateTime strFirst,strEnd;
strFirst = COleDateTime(strFirst.GetYear(),strFirst.GetMonth(),strFirst.GetDay(),,,);
strEnd = COleDateTime(tmCurrent.GetYear(),tmCurrent.GetMonth(),tmCurrent.GetDay(),,,);
tspan= strEnd - strFirst;

VC-基础:VC中得到当前系统的时间和日期的更多相关文章

  1. Oracle 在SQL语句中如何获取系统当前时间并进行操作

    select sysdate from dual;select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; select to_char(s ...

  2. Android开发之java代码中获取当前系统的时间工具类

    /** * 获取当前时间 * * @return */ public String getTime() { Date date = new Date();// 创建一个时间对象,获取到当前的时间 Si ...

  3. linux debian 时间设置中无法选择“自动设定时间和日期”

    没有安装ntpdate 执行:apt-get install ntpdate ntp.sjtu.edu.cn 202.120.2.101 (上海交通大学网络中心NTP服务器地址)s1a.time.ed ...

  4. Oracle,MySQL,sqlserver三大数据库如何获取系统当前时间

    Oracle中如何获取系统当前时间:用SYSDATE() MySQL中获取系统当前时间主要有以下几点: (1)now()函数以('YYYY-MM-dd HH:mm:SS')返回当前的日期时间,可以直接 ...

  5. date 显示或设置系统时间和日期

    显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ...

  6. Linux 设置系统时间和日期 API

    嵌入式Linux 设置时间和日期 API ,它是busybox要提取的源代码. Linux设置时间和日期的步骤: 1. 设置系统时间和日期: 2. 该系统的时间和日期,同步到硬件. #include ...

  7. vc++基础班[24]---系统各种路径信息的获取

    vc++基础班[24]---系统各种路径信息的获取 ------------------------------------------ Begin ------------------------- ...

  8. vc++基础班[25]---系统信息的获取

    --------------------------------------------------------------------------- VC 驿站 WwW.CcTry.CoM 多抽出一 ...

  9. 在VC资源文件中加入声音资源

    本文介绍如何在VC资源文件中加入自己的声音资源,使自己的应用程序可以播放声音. 1.首先用文本编辑器(如记事本)打开资源文件(.rc文件) 在最后加入自己的声音资源,如下IDW WAVE " ...

随机推荐

  1. 字符串函数 replace() 方法妙用

    alert('10 12 14 16'.replace(/d+/g,function(match){ return parseInt(match,10) > 10 ? '*' : match; ...

  2. E20190404-hm

    prepend vt. 预先考虑,预先计划,预谋;

  3. art-template在项目中的应用

    art-template 是一个简约.超快的模板引擎.它采用作用域预声明的技术来优化模板渲染速度,从而获得接近 JavaScript 极限的运行性能,并且同时支持 NodeJS 和浏览器. 下面介绍在 ...

  4. python 如何在 command 中能够找到 其他module

    部分代码如下: __author__ = 'norsd' # coding=utf8 # 上句说明使用utf8编码 try: import os import sys import time #关键语 ...

  5. NPOI用WorkbookFactory读写 2007以上格式文件(xlsx)

    //我用的最新的2.2.1版本 //第一步:引用DLL,5个全导入,包括ICSHARP.ZIP,是个开源压缩工具包.XLSX是压缩格式,需要它来解压 //第二部: using NPOI.SS.User ...

  6. Validation(3)--全局参数异常校验捕获及返回XML解决

    @RestControllerAdvice原创直接上代码,后面再说怎么用1.这个是一个Form,用来接收参数的,一个简单的NotEmpty注解校验,merchantName这个参数是必传的: pack ...

  7. RDS 导出Mysqlbinlog_二进制日志

    1:首先进入RDS后台查看Mysqlbin_log日志,可以通过以下命令进行查看: show master logs; 或 show binary logs; 2:比如我们获取的是:mysql-bin ...

  8. centos 7 安装node.js

    Node.js v10.x安装命令 curl --silent --location https://rpm.nodesource.com/setup_10.x | bash - Node.js v5 ...

  9. IP服务-3-DHCP

    DHCP代表了动态IP地址分配的下一阶段.DHCP建立在BOOTP协议格式的基础上,专注于动态分配多种信息,以及为未来的扩展提供灵活的消息结构,并且无需预先定义每个客户端的MAC地址.DHCP提供的功 ...

  10. OSPF-1-OSPF的数据库交换(2)

    2.Hello过程: (1)在同一子网中发现其他运行OSPF的路由器 所有启用了OSPF的接口,都会监听发往224.0.0.5的组播Hello消息,这是表示所有OSPF路由器的组播地址.Hello包使 ...