Mr.刘的一段格式化日期时间输出实现

/*********************************

*函数名: GetCurrentTimeString

*功能描述: 取当前时间并按 "07/10/26 12:01:01 AM" 格式化返回

*参数: strReturnTimeString, 引用类型,返回要得到的字符串

*返回值:  void

*备注: 平台Visual C++ 6.0, C语言风格

**********************************/

void MyClass::GetCurrentTimeString(CString &strReturnTimeString)
{
 strReturnTimeString="";
 struct tm *newtime;
    char am_pm[20] = STRING_AM;    //#define STRING_AM "AM"
    time_t long_time;

    time( &long_time );                /* Get time as long integer. */
    newtime = localtime( &long_time ); /* Convert to local time. */

    if( newtime->tm_hour > 12 )        /* Set up extension. */
            strcpy( am_pm, STRING_PM );  /* #define STRING_PM "PM" */
    if( newtime->tm_hour > 12 )        /* Convert from 24-hour */
            newtime->tm_hour -= 12;    /*   to 12-hour clock.  */
    if( newtime->tm_hour == 0 )        /*Set hour to 12 if midnight. */
            newtime->tm_hour = 12;
 int nYear = newtime->tm_year+1900;
 nYear = nYear - ( nYear / 100 ) * 100;

 strReturnTimeString.Format( "%02d/%02d/%02d %02d:%02d:%02d %s",
  nYear,  // year
  newtime->tm_mon+1, // month
  newtime->tm_mday, // day
  newtime->tm_hour, // hour
  newtime->tm_min, // min
  newtime->tm_sec, // sec
  am_pm ); // am pm
 return ;

你可能感兴趣的:(Mr.刘的一段格式化日期时间输出实现)