`
java-mans
  • 浏览: 11433517 次
文章分类
社区版块
存档分类
最新评论

c++之获取当前时间

 
阅读更多

我以为有一个Date的类,不像Java,C++没有这个类,可以使用C库time.h的函数来获取当前时间,或者调用DOS的date

方法一:

#include <iostream>
#include <ctime>
#include<string>
using namespace std;

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  cout<<"Current local time and date:"<< asctime (timeinfo)<<endl;
 system("pause");
  return 0;
}

方法二:

#include<iostream>
#include<ctime>
#include<string>
using namespace std;
int main(){
	time_t lltime;
	time(lltime);
	cout<<"the time is "<<ctime(lltime)<<endl;
	system("pause");
	return 0;
}


方法三:

include <ctime>
#include<string>
using namespace std;

#define CCT (+8)

int main ()
{
  time_t rawtime;
  struct tm * ptm;

  time ( &rawtime );

  ptm = gmtime ( &rawtime );
 
  cout<<"Beijing (China):"<<(ptm->tm_hour+CCT)%24<<":"<<ptm->tm_min<<endl;
  system("pause");
  return 0;
}


方法四:

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
	system("time");
	system("pause");
	return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics