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

本文介绍了四种不同的方法来获取当前时间:使用C标准库精确到秒、Windows API精确到毫秒、调用系统命令以及直接输出时间戳。每种方法都有其优缺点,并适用于不同场景。
//方案— 优点:仅使用C标准库;缺点:只能精确到秒级


#include <time.h>


#include <stdio.h>


int main( void )


{


time_t t = time( 0 );


char tmp[64];


strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A 本年第%j天 %z",


localtime(&t) );


puts( tmp );


return 0;


}


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


#include <windows.h>


#include <stdio.h>


int main( void )


{


SYSTEMTIME sys;


GetLocalTime( &sys );


printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n"


,sys.wYear,sys.wMonth,sys.wDay


,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds


,sys.wDayOfWeek);


return 0;


}


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


#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 0;

}

另一:_strdate(tempstr);

另二:

CString CTestView::GetTime()


{


CTime CurrentTime=CTime::GetCurrentTime();


CString strTime;


strTime.Format("%d:%d:%d",CurrentTime.GetHour(), CurrentTime.GetMinute(),CurrentTime.GetSecond());

return strTime;


} language=VBScript>call ReplaceSubjectHTML_emote(592915)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值