zoukankan      html  css  js  c++  java
  • 【VS开发】CTime和CTimeSpan使用

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

    [html] view plain copy
     print?
    1. // CTimeTest.cpp : Defines the entry point for the console application.  
    2. //  
    3.   
    4. #include "stdafx.h"  
    5. #include "atltime.h"  
    6. #include <iostream>  
    7. using namespace std;  
    8.   
    9. int _tmain(int argc, _TCHAR* argv[])  
    10. {  
    11.     CTime strTime ;//用于将CTime对象格式化为字符串  
    12.     CTime curTime  = CTime::GetCurrentTime() ;//获取当前的时间并保存到curTime  
    13.   
    14.     int nYear = curTime.GetYear() ;  
    15.     int nMonth = curTime.GetMonth() ;  
    16.     int nDay = curTime.GetDay() ;  
    17.     int nHour = curTime.GetHour() ;  
    18.     int nMin = curTime.GetMinute() ;  
    19.     int nSec = curTime.GetSecond() ;  
    20.   
    21.     cout << "输出当前时间:" << endl ;  
    22.     cout << nYear << "年"    
    23.          << nMonth<< "月"  
    24.          << nDay  << "日"  
    25.          << nHour << "时"    
    26.          << nMin<< "分"  
    27.          << nSec  << "秒" << endl;   
    28.   
    29.     //为计算时间差设置一个起始时间  
    30.     CTime startTime = CTime(2010,10,31,12,12,12) ;  
    31.     cout << "起始时间:" << endl ;  
    32.     cout << startTime.GetYear() << "年"  
    33.          <<startTime.GetMonth() << "月"  
    34.          <<startTime.GetDay()   << "日"  
    35.          <<startTime.GetHour()  << "时"  
    36.          <<startTime.GetMinute()<< "分"  
    37.          <<startTime.GetSecond()<< "秒"  
    38.          << endl ;   
    39.     //计算时间差  
    40.     CTimeSpan timeSpan ;  
    41.     timeSpan = curTime - startTime ;  
    42.     cout << "两时时间差" << endl ;   
    43.     cout<<timeSpan.GetDays()<<"天"  
    44.         <<timeSpan.GetHours()<<"小时"  
    45.         <<timeSpan.GetMinutes()<<"分"  
    46.         <<timeSpan.GetSeconds()<<"秒"  
    47.         <<endl ;  
    48.   
    49.     cout<<"总小时数:"<<timeSpan.GetTotalHours()<<"小时"<<endl ;  
    50.     cout<<"总分钟数:"<<timeSpan.GetTotalMinutes()<<"分"<<endl ;  
    51.     cout<<"总秒数:"<<timeSpan.GetTotalSeconds()<<"秒"<<endl ;  
    52.   
    53.     //// 将当前时间 curTime 对象格式化为字符串  
    54.     //strTime = curTime.Format(_T("%Y-%m-%d %H:%M:%S"));  
    55.     //// 输出格式化字符串,由于字符串使用 Unicode 字符,所以要使用 wcout 输出  
    56.     //wcout<<(LPCTSTR)strTime<<endl;  
    57.     getchar() ;  
    58.   
    59.     return 0;  
    60. }  
    运行结果如下:
  • 相关阅读:
    【BZOJ 4151 The Cave】
    【POJ 3080 Blue Jeans】
    【ZBH选讲·树变环】
    【ZBH选讲·拍照】
    【ZBH选讲·模数和】
    【CF Edu 28 C. Four Segments】
    【CF Edu 28 A. Curriculum Vitae】
    【CF Edu 28 B. Math Show】
    【CF Round 439 E. The Untended Antiquity】
    【CF Round 439 C. The Intriguing Obsession】
  • 原文地址:https://www.cnblogs.com/huty/p/8518655.html
Copyright © 2011-2022 走看看