此文就用一个程序表示,相信只要是学过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;
- }