方法一: http://blog.csdn.net/c_spark/archive/2009/08/14/4446460.aspx
size_t
strftime (char *s, size_t maxsize, const char *format, const struct tm *tp); //格式化输出函数
view plaincopy to clipboardprint?
#include
#include
int main ()
{
static char str_time[100];
struct tm *local_time = NULL;
time_t utc_time;
utc_time = time (NULL);
local_time = localtime(&utc_time);
strftime(str_time, sizeof(str_time), "%Y-%m-%d,%H:%M:%S", local_time);
printf ("Time: %s/n", str_time);
return 0;
}
运行输出:
./time
Time: 2009-08-14,13:59:22
方法二:
SYSTEMTIME mySystemTime;
//可能是时区问题,北京时间要比标准时间晚8个小时
GetLocalTime(&mySystemTime);//GetSystemTime好象得到的时间要差8个小时
CString filetime;
filetime.Format ( "%d-%d-%d %s%d %d:%d:%d ",
mySystemTime.wYear,mySystemTime.wMonth,mySystemTime.wDay, "星期 ",
mySystemTime.wDayOfWeek,mySystemTime.wHour,mySystemTime.wMinute,mySystemTime.wSecond);
方法三(MFC): http://blog.sina.com.cn/s/blog_4a6ea409010005ed.html
用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
Powered by Zoundry Raven