分享

用CTime类得到当前日期、时间、星期,并格式化输出

 herowuking 2015-06-13
用CTime类得到当前日期、时间、星期,并格式化输出 
 
① 定义一个CTime类对象 CTime time; 
② 得到当前时间 time = CTime::GetCurrentTime(); 
③ GetYear( ),GetMonth( ), GetDay( ), GetHour( ), GetMinute( ), GetSecond( ), GetDayOfWeek( ) 返回整型(int)对应项目 
④ 将当前时间格式化 CString date = time.Format("%Y-%m-%d %H:%M:%S %W-%A"); 
说明: 
1) 结果为:2006-10-13 17:23:47 41-Friday 
2) 格式符号说明 
%a —— 星期(缩写英文),如Fri; 
%A —— 星期(全写英文),如Friday 
%b —— 月份(缩写英文),如Oct 
%B —— 月份(全写英文),如 October 
%c —— 月/日/年 时:分:秒,如 10/13/06 19:17:17 
%d —— 日期(1 ~ 31) 
%H —— 时(24小时制)(0 ~ 23) 
%I —— 时(12小时制)(0 ~ 12) 
%j —— 一年当中的第几天,(1 ~ 366) 
%m —— 月份(数字 1 ~ 12) 
%M —— 分(0 ~ 59) 
%p —— 12小时中的A M/PM指示,或者AM,或者PM 
%S —— 秒(0 ~ 59) 
%U —— 一年中的第几周,星期日作为每周的第一天(0 ~ 53) 
%w —— 星期(数字表示,0 ~ 6,0代表星期日) 
%W —— 一年中的第几周,星期一作为每周的第一天(0 ~ 53) 
%x —— 月/日/年,%c的前半段 
%X —— 时/分/秒,%c的后半段 
%y —— 年份(不带世纪,如 06) 
%Y —— 年份(带世纪,如 2006) 
%z,%Z —— 时区名称或缩写,如果时区未知,此字符为空,如“中国标准时间” 
%% —— % 
 
“#”标志的含义: 
① %#a, %#A, %#b, %#B, %#p, %#X, %#z, %#Z, %#% ——“#” 被忽略 
② %#c —— 把%c中的数字变成英文,再在前面加上星期, 如:“Tuesday, March 14, 1995, 12:41:29”. 
③ %#x —— 把%x中的数字变成英文,再在前面加上星期,如:Tuesday, March 14, 1995 
④ %#d, %#H, %#I, %#j, %#m, %#M, %#S, %#U, %#w, %#W, %#y, %#Y —— 如果开 头为0,去掉开头的0 
CSting ---------------------->CTime
CString   timestr   =   "2000年04月05日";   
  int   year,month,day;   
  BYTE   tt[5];   
  //get   year   
  memset(tt,   0,   sizeof(tt));   
  tt[0]   =   timestr[0];   
  tt[1]   =   timestr[1];   
  tt[2]   =   timestr[2];   
  tt[3]   =   timestr[3];   
  year=   atoi((char   *)tt);   
    
  //get   month   
  memset(tt,   0,   sizeof(tt));   
  tt[0]   =   timestr[6];   
  tt[1]   =   timestr[7];   
  month   =   atoi((char   *)tt);   
    
  //get   day   
  memset(tt,   0,   sizeof(tt));   
  tt[0]   =   timestr[10];   
  tt[1]   =   timestr[11];   
    
  CTime   time(year,month,day,0,0,0);
---- or -----------
CString   timestr   =   "2000年04月05日";   
  int   a,b,c   ;   
  sscanf(timestr.GetBuffer(timestr.GetLength()),"%d年%d月%d日",&a,&b,&c);   
  CTime   time(a,b,c,0,0,0);     
--------or - ---------------------
 CString   s("2001-8-29   19:06:23");   
  int   nYear,   nMonth,   nDate,   nHour,   nMin,   nSec;   
  sscanf(s,   "%d-%d-%d   %d:%d:%d",   &nYear,   &nMonth,   &nDate,   &nHour,   &nMin,   &nSec);   
  CTime   t(nYear,   nMonth,   nDate,   nHour,   nMin,   nSec);

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多