MFC之CTime类 和 CtimeSpan类的使用

此文就用一个程序表示,相信只要是学过C语言的都能看得懂的。

// CTimeTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "atltime.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	CTime strTime ;//用于将CTime对象格式化为字符串
	CTime curTime  = CTime::GetCurrentTime() ;//获取当前的时间并保存到curTime

	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; 

	//为计算时间差设置一个起始时间
	CTime startTime = CTime(2010,10,31,12,12,12) ;
	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 ;

	 将当前时间 curTime 对象格式化为字符串
	//strTime = curTime.Format(_T("%Y-%m-%d %H:%M:%S"));
	 输出格式化字符串,由于字符串使用 Unicode 字符,所以要使用 wcout 输出
	//wcout<<(LPCTSTR)strTime<<endl;
	getchar() ;

	return 0;
}
运行结果如下:

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值