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

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

用ctime得到的是 "Tue Jul 27 15:31:23 2004",不是我所想要的。
...全文
167 点赞 收藏 4
写回复
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;
}

回复 点赞
发动态
发帖子
C语言
创建于2007-09-28

3.2w+

社区成员

24.0w+

社区内容

C语言相关问题讨论
社区公告
暂无公告