首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >c++计算程序运行时间_程序设计5个基本步骤

c++计算程序运行时间_程序设计5个基本步骤

作者头像
全栈程序员站长
发布2022-09-23 20:16:04
发布2022-09-23 20:16:04
47900
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是你们的朋友全栈君。

注意:

控制台需要加 #include “atltime.h” 头文件

代码语言:javascript
代码运行次数:0
运行
复制
#include "stdafx.h"
 
#include "atltime.h"
#include <iostream>
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{ 
   
	CString strTime;		//用于将CTime对象格式化为字符串
	//获取当前的系统时间
	CTime curTime = CTime::GetCurrentTime();
 
	int nYear = curTime.GetYear();
	int nMonth = curTime.GetMonth();
	int nDay = curTime.GetDay();
	int nHour = curTime.GetHour();
	int nMin = curTime.GetMinute();
	int nSec = curTime.GetSecond();
 
	/* 方法一 */
	//输出当前时间
	cout << "当前时间:" << endl;
	cout << nYear << "年" << nMonth << "月" << nDay << "日" << nHour << "时" << nMin << "分" << nSec << "秒" << endl;
 
	/* 方法二 */
	//将当前时间curTime格式化为字符串
	strTime = curTime.Format(_T("%Y-%m-%d %H:%M:%S %A"));
	//出书格式化字符串,由于字符串使用Unicode字符,所以要使用wcout输出
	wcout << (LPCTSTR)strTime << endl;
 
	//为计算时间差,需要设定一个起始时间并输出
	CTime startTime = CTime(2017,10,1,10,20,10);
	cout << "起始时间" << endl;
	cout << startTime.GetYear() << "-" << startTime.GetMonth() << "-" << startTime.GetDay() << " " 
		<< startTime.GetHour() << ":" << startTime.GetMinute() << ":" << startTime.GetSecond() << endl;
 
	//计算时间差
	CTimeSpan timeSpan;
	timeSpan = curTime - startTime;
	cout << "两个时间相差:" << endl;
	cout << timeSpan.GetDays() << "天" << timeSpan.GetHours() << "小时" 
		<< timeSpan.GetMinutes() << "分" << timeSpan.GetSeconds() << "秒"<< endl;
 
	cout << "总小时数:" << timeSpan.GetTotalHours() << "小时" << endl;
	cout << "总分钟数:" << timeSpan.GetTotalMinutes() << "分钟" << endl;
	cout << "总秒数:" << timeSpan.GetTotalSeconds() << "秒" << endl;
 
	getchar();
	return 0;
}

效果图:

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/170955.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档