如何得到指定的日期格式?

daehappy 2004-07-27 04:03:31
想得到类似 "11:05:41.218 CST Tue Jul 27 2004" 的格式,请问用什么函数?

用ctime得到的是 "Tue Jul 27 15:31:23 2004",不是我所想要的。
...全文
248 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
daehappy 2004-07-27
  • 打赏
  • 举报
回复
谢谢,但毫秒怎么取呢?
geesun 2004-07-27
  • 打赏
  • 举报
回复
size_t strftime(char *s, size_t max, const char *format,
const struct tm *tm);

DESCRIPTION
The strftime() function formats the broken-down time tm according to
the format specification format and places the result in the character
array s of size max.

Ordinary characters placed in the format string are copied to s without
conversion. Conversion specifiers are introduced by a ‘%’ character,
and are replaced in s as follows:

%a The abbreviated weekday name according to the current locale.

%A The full weekday name according to the current locale.

%b The abbreviated month name according to the current locale.

%B The full month name according to the current locale.

%c The preferred date and time representation for the current
locale.

%C The century number (year/100) as a 2-digit integer. (SU)

%d The day of the month as a decimal number (range 01 to 31).

%D Equivalent to %m/%d/%y. (Yecch - for Americans only. Americans
should note that in other countries %d/%m/%y is rather common.
This means that in international context this format is ambigu-
ous and should not be used.) (SU)

%e Like %d, the day of the month as a decimal number, but a leading
zero is replaced by a space. (SU)

%E Modifier: use alternative format, see below. (SU)

%F Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)

%G The ISO 8601 year with century as a decimal number. The 4-digit
year corresponding to the ISO week number (see %V). This has
the same format and value as %y, except that if the ISO week
number belongs to the previous or next year, that year is used
instead. (TZ)

%g Like %G, but without century, i.e., with a 2-digit year (00-99).
(TZ)

%h Equivalent to %b. (SU)

%H The hour as a decimal number using a 24-hour clock (range 00 to
23).

%I The hour as a decimal number using a 12-hour clock (range 01 to
12).

%j The day of the year as a decimal number (range 001 to 366).

%k The hour (24-hour clock) as a decimal number (range 0 to 23);
single digits are preceded by a blank. (See also %H.) (TZ)

%l The hour (12-hour clock) as a decimal number (range 1 to 12);
single digits are preceded by a blank. (See also %I.) (TZ)

%m The month as a decimal number (range 01 to 12).

%M The minute as a decimal number (range 00 to 59).

%n A newline character. (SU)

%O Modifier: use alternative format, see below. (SU)

peter9606 2004-07-27
  • 打赏
  • 举报
回复
fprintf()
柯本 2004-07-27
  • 打赏
  • 举报
回复
用:
size_t strftime(char *s, size_t maxsize, const char *fmt, const struct tm *t);

size_t wcsftime(wchar_t *s, size_t maxsize, const wchar_t *fmt, const struct tm *t);
ex:
#include <stdio.h>
#include <time.h>
#include <dos.h>

int main(void)
{
struct tm *time_now;
time_t secs_now;
char str[80];

tzset();
time(&secs_now);
time_now = localtime(&secs_now);
strftime(str, 80,
"It is %M minutes after %I o'clock (%Z) %A, %B %d 19%y",
time_now);
printf("%s\n",str);
return 0;
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧