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

用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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值