获取当前系统时间的几种方法
2012-11-28 21:57
537 查看
在开发应用程序时往往需要获取当前系统时间。
//方案- 优点:仅使用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);
另二: CTime
CString CTestView::GetTime()
{
CTime CurrentTime=CTime::GetCurrentTime();
CString strTime;
strTime.Format(" %d:%d:%d" ,CurrentTime.GetHour(), CurrentTime.GetMinute()
,CurrentTime.GetSecond());
return strTime;
}
//方案- 优点:仅使用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);
另二: CTime
CString CTestView::GetTime()
{
CTime CurrentTime=CTime::GetCurrentTime();
CString strTime;
strTime.Format(" %d:%d:%d" ,CurrentTime.GetHour(), CurrentTime.GetMinute()
,CurrentTime.GetSecond());
return strTime;
}
相关文章推荐
- VC++获取当前系统时间的几种方法
- MFC获取系统当前时间的几种方法
- 获取当前系统时间(三种方法)
- java 获取系统时间的几种方法
- Java获取系统时间的几种方法
- Java各种获取系统当前时间方法 和 格式
- 关于linux下获取系统当前时间的方法汇总
- 批处理bat标准化获取当前系统日期的几种方法,补0
- Java获取系统时间的几种方法
- java中获取当前系统时间的方法
- Java中获取当前系统的时间的方法
- java获取当前时间的几种方法
- oc之获取系统当前时间的方法
- 获取系统时间的几种方法,最高精确1ms
- JAVA中获取当前系统时间的两种方法
- cocos2d-x在win32和iOS、android下获取当前系统时间的方法
- javascript获取系统当前时间的方法
- 获取系统当前时间的方法---与时间相关的方法
- android 获取系统时间的几种方法
- android开发 获取比当前系统时间提前十天的方法