有没有函数把这种时间格式 Fri,23 Apr 1999 11:06:41 转化为xxxx年xx月xx日...的格式(60分)

  • 主题发起人 主题发起人 delphi4.0
  • 开始时间 开始时间
StrToDateTime()好像可以转换,不过我看帮助,可能要改什么
系统变量才可以做到.
我试了一次没成功.
大家努力吧.
 
那肯定得自己写了.
 
var
m:TDateTime;
begin
m:=now;
label1.caption:=FormatDateTime('YYYY"年" MMMM DD"日" TT',m);
end;
 
小李子说的没错,下面给出相关函数:
function FormatDateTime(const Format: string;
DateTime: TDateTime): string;
Description
FormatDateTime formats the date-and-time value given by DateTime using the format given by Format. The following format specifiers are supported:
Specifier Displays
c Displays the date using the format given by the ShortDateFormat global variable, followed by the time using the format given by the LongTimeFormat global variable. The time is not displayed if the fractional part of the DateTime value is zero.
d Displays the day as a number without a leading zero (1-31).
dd Displays the day as a number with a leading zero (01-31).
ddd Displays the day as an abbreviation (Sun-Sat) using the strings given by the ShortDayNames global variable.
dddd Displays the day as a full name (Sunday-Saturday) using the strings given by the LongDayNames global variable.
ddddd Displays the date using the format given by the ShortDateFormat global variable.
dddddd Displays the date using the format given by the LongDateFormat global variable.
m Displays the month as a number without a leading zero (1-12). If the m specifier immediately follows an h or hh specifier, the minute rather than the month is displayed.
mm Displays the month as a number with a leading zero (01-12). If the mm specifier immediately follows an h or hh specifier, the minute rather than the month is displayed.
mmm Displays the month as an abbreviation (Jan-Dec) using the strings given by the ShortMonthNames global variable.
mmmm Displays the month as a full name (January-December) using the strings given by the LongMonthNames global variable.
yy Displays the year as a two-digit number (00-99).
yyyy Displays the year as a four-digit number (0000-9999).
h Displays the hour without a leading zero (0-23).
hh Displays the hour with a leading zero (00-23).
n Displays the minute without a leading zero (0-59).
nn Displays the minute with a leading zero (00-59).
s Displays the second without a leading zero (0-59).
ss Displays the second with a leading zero (00-59).
t Displays the time using the format given by the ShortTimeFormat global variable.
tt Displays the time using the format given by the LongTimeFormat global variable.
am/pm Uses the 12-hour clock for the preceding h or hh specifier, and displays 'am' for any hour before noon, and 'pm' for any hour after noon. The am/pm specifier can use lower, upper, or mixed case, and the result is displayed accordingly.
a/p Uses the 12-hour clock for the preceding h or hh specifier, and displays 'a' for any hour before noon, and 'p' for any hour after noon. The a/p specifier can use lower, upper, or mixed case, and the result is displayed accordingly.
ampm Uses the 12-hour clock for the preceding h or hh specifier, and displays the contents of the TimeAMString global variable for any hour before noon, and the contents of the TimePMString global variable for any hour after noon.
/ Displays the date separator character given by the DateSeparator global variable.
: Displays the time separator character given by the TimeSeparator global variable.
'xx'/"xx" Characters enclosed in single ordo
uble quotes are displayed as-is, anddo
not affect formatting.
DateTimeToStr(DateTime: TDateTime): string
var
ADate: TDateTime;
begin
ADate := StrToDate(Edit1.Text);
Label1.Caption := DateToStr(ADate);
其实在DELPHI帮助中你可以用INDEX来查询!
 
拜托. 看清楚题目再答.
 
曹大哥的说法是正确的,datetimetostr和strtodatetime是根据sysutil的一些变
量来进行转换的,这些变量对应control panel中的设定,比如其中的短日期类型,
在sysutils里叫做shorttime,而这些变量是在程序初始化的时候初始化的,在程序
执行中,要调用application的一个方法才能够刷新,你只要修改了这些变量,就可
以方便的使用上面的两个函数,将这个古怪的字符串换成tdatetime类型了。
至于改成什么样子我也不知道。8-(
 
那要看这个日期放在哪里,如果放在数据库里的TDATETIME字段里,当然没有问题
否则你只能先用API改变系统当前的时间设置,再进行日期转换(我没有试过,呵呵)
 
自己写函数吧!
还有,如果你的时间是取自数据库,
应该看看数据库的内置函数。
例如Sybase.....
 
是DATETIME就FORMATDATETIME,一点问题也没有
不过看问题不那么简单,呵呵,自己玩吧
 

得自己写: 方法如下:
1.先把字符串转成tdatetime(d4中有带格式转换的函数,应该不是
strtodatetime,具体叫什么我已经忘了 ^_^;
2.formatdatetime('YYYY"年" MMMM DD"日" TT', 第一步结果);

 

得自己写: 方法如下:
1.先把字符串转成tdatetime(d4中有带格式转换的函数,应该不是
strtodatetime,具体叫什么我已经忘了 ^_^;
2.formatdatetime('YYYY"年" MMMM DD"日" TT', 第一步结果);

 
看了一下SysUtils.pas中的StrToDateTime函数, 其中调用了两个单元私有函数,
ScanDate, ScanTime, ScanChar, ScanNumber来对字符串解码很复杂, 在涉及到
几个系统变量数组,看了头就大,自己写一个一定会很累。
 
so easy, set these Global varible first:
ShortDateFormat := 'yy"年"m"月"d"日"';
LongTimeFormat := 'h"时"nn"分"ss"秒"';
then
u can use DateTimeTostr, DateToStr to convert date to string.
DateTimeToStr use ShortDateFormat &
LongTimeFormat.
DateToStr use ShortDateFormat only.
see also:
DateSeperator
LongDateFormat
TimeAMString
TimePMString
ShortTimeFormat
....
if u need translate string to datetime first, then
convert it back to string, following function is what i wrote in my component:
function StrToDateDef(s: string): TDateTime;
const
monthdays : array [boolean, 1..12] of word = (
(1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335),
(1, 32, 61, 92, 122, 153, 183, 214, 245, 275, 306, 336)
);
var
i, j, k: Integer;
dt : array [0..5] of integer;
begin
s := trim(s);
i := 1;
k := 0;
dt[0] := 0;
dt[1] := 1;
dt[2] := 1;
dt[3] := -1;
dt[4] := 0;
dt[5] := 0;
while (i <= length(s)) and not (s in ['0','1','2','3','4','5','6','7','8','9'])do
inc(i);
j := i;
while j <= length(s)do
begin
while (i <= length(s)) and (s in ['0','1','2','3','4','5','6','7','8','9'])do
inc(i);
dt[k] := strtoint(copy(s, j, i - j));
inc(k);
if k > 5 then
break;
while (i <= length(s)) and not (s in ['0','1','2','3','4','5','6','7','8','9'])do
inc(i);
j := i;
end;
if dt[1] > 12 then
begin
dt[0] := dt[0] + (dt[1]-1) div 12 + 1;
dt[1] := (dt[1] - 1) div 12 + 1;
end;
if (dt[0] >=0) and (dt[0]<9999) then
begin
result := strtodate(inttostr(dt[0])+dateseparator+'1'+dateseparator+'1')-1;
i := monthdays[isleapyear(dt[0]), dt[1]];
result := result + i + dt[2] - 1;
if (dt[3] >= 0) then
begin
if dt[5] > 59 then
begin
dt[4] := dt[4] + dt[5] div 60;
dt[5] := dt[5] mod 60;
end;
if dt[4] > 59 then
begin
dt[3] := dt[3] + dt[4] div 60;
dt[4] := dt[4] mod 60;
end;
if dt[3] > 23 then
begin
result := result + dt[3] div 24;
dt[3] := dt[3] mod 24;
end;
if ((pos(TimePmString, s)<>0)
or (pos('PM', uppercase(s)) <> 0)) and (dt[3]<12) then
dt[3] := dt[3] + 12;
result := Result + EncodeTime(dt[3], dt[4], dt[5], 0);
end;
end else
result := now;
end;

this function even can translate the string "1999 25.333 56:61:01" to
correct datetime.
 
直接设置"短日期格式",例如:
ShortDateFormat := 'yyyy-mm-dd';
最好在应用程序一开始就进行设置,
在整个应用程序中都管用。
 
多人接受答案了。
 
后退
顶部