C++ 获取系统时间

实现这个功能的方法有很多,有兴趣的朋友可以去自己查找一下。

获取系统的时间  time.cpp

#include <iostream>
#include <time.h>
#include <string>

int main()
{
	std::string s;
	char stime[256] = {0};
	time_t now_time;
	time(&now_time);
	 s = ctime(&now_time);
	std::cout << s << std::endl;
	return 0;
}

通过编译,g++ -time.cpp -o time ,运行./time,后可以获得系统时间。

输出为:

Wed Dec 24 13:51:07 2014

然后通过函数strftime()可以选择自己想要输出的格式,

如:

只是输出时,分,秒:

#include <iostream>
#include <time.h>
#include <string>

int main()
{
	std::string s;
	char stime[256] = {0};
	time_t now_time;
	time(&now_time);
	strftime(stime,sizeof(stime),"%H:%M:%S",localtime(&now_time));
	s = stime + '\0';
	std::cout << s << std::endl;
	return 0;
}

编译后运行,输出:

13:54:31


转载于:https://my.oschina.net/bobwei/blog/359961

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值