首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

请教怎么把以下格式的字符串转化成DateTime

2012-02-25 
请问如何把以下格式的字符串转化成DateTime例如说 2005年12月30日 我试过用StrToDate的第二种重载,并把S

请问如何把以下格式的字符串转化成DateTime
例如说 "2005年12月30日 "

我试过用StrToDate的第二种重载,并把ShortDateFormat设置成 "yyyy年mm月dd日 ",仍旧报错。

[解决办法]
呵呵
ss:TFormatSettings;
strtodate(now,ss);
里面的ss你要按如下格式写

type

TFormatSettings = record
CurrencyFormat: Byte;
NegCurrFormat: Byte;
ThousandSeparator: Char;
DecimalSeparator: Char;
CurrencyDecimals: Byte;
DateSeparator: Char;
TimeSeparator: Char;
ListSeparator: Char;
CurrencyString: string;
ShortDateFormat: string;
LongDateFormat: string;
TimeAMString: string;
TimePMString: string;
ShortTimeFormat: string;
LongTimeFormat: string;

ShortMonthNames: array[1..12] of string;
LongMonthNames: array[1..12] of string;
ShortDayNames: array[1..7] of string;
LongDayNames: array[1..7] of string;
TwoDigitYearCenturyWindow: Word;
end;
[解决办法]
一个比较简单的方法是:
先用StringReplace()函数把 "yyyy年mm月dd日 ",中的“年”、“月”、“日”替换成 "- ",就变成了可以直接StrToDate的 "yyyy-mm-dd "格式了。
[解决办法]
请参考以下程序:
能识别无分隔符的yyyymmdd格式、有任意单字节分隔符的yyyy-m-d yy-m-d、有任意双字节分隔符的yyyy年m月d日格式 yy年m月d日格式。某些不规则日期也可识别如:02年3月04日
function MyStrToDate(const AValue: String; var Date: TDateTime): Boolean;
var
year, month, day: Integer;
DateStr, temp2: string;
begin
result:= false;
year:=0;
month:=0;
day:=0;
DateStr:= AValue;

try
//处理年
temp2:= Copy(DateStr, 1, 4);
if TryStrToInt(temp2, year) then //四位年
DateStr:= Copy(DateStr, 5, length(DateStr)-4)
else
begin //两位年,不支持无分隔符格式
temp2:= Copy(DateStr, 1, 2);
if TryStrToInt(temp2, year) then
begin
DateStr:= Copy(DateStr, 3, length(DateStr)-2);
if year <40 then year:=year+2000 else year:=year+1900;//处理两位年。
end
else
exit;
end;

//处理月份,支持一位,两位
if (Ord(DateStr[1])> =48) and (Ord(DateStr[1]) <=57) then //无分隔符格式时,不支持一位月
begin
temp2:= Copy(DateStr, 1, 2);
if TryStrToInt(temp2, month) then
DateStr:=Copy(DateStr, 3, length(DateStr)-2)
else
exit;
end
else //有分隔符时
begin
if ByteType(DateStr, 1)=mbSingleByte then //单字节字符分隔
DateStr:=Copy(DateStr, 2, length(DateStr)-1)
else //双字节字符分隔
DateStr:=Copy(DateStr, 3, length(DateStr)-2);
temp2:= Copy(DateStr, 1, 2);
if TryStrToInt(temp2, month) then //两位月份
DateStr:= Copy(DateStr, 3, Length(DateStr)-2)
else //一位月份
begin
temp2:= Copy(DateStr, 1, 1);
if TryStrToInt(temp2, month) then
DateStr:= Copy(DateStr, 2, Length(DateStr)-1)
else
exit;
end
end;

//处理日,支持一位,两位
if (Ord(DateStr[1])> =48) and (Ord(DateStr[1]) <=57) then //无分隔符格式时,不支持一位日
begin
temp2:= DateStr;
if not TryStrToInt(temp2, day) then exit;
end
else //有分隔符时
begin
if ByteType(DateStr, 1)=mbSingleByte then //单字节字符分隔
DateStr:=Copy(DateStr, 2, length(DateStr)-1)
else //双字节字符分隔
DateStr:=Copy(DateStr, 3, length(DateStr)-2);
temp2:= DateStr;
if TryStrToInt(temp2, day) then


DateStr:= Copy(DateStr, 2, Length(DateStr)-2)
else
exit;
end;

result:= TryEncodeDate(year, month, day, Date);
except
end;
end;

[解决办法]
还是要想好自己的需求吧,如果面向多地区多国语言,并且不规范的日期格式,Delphi没有现成的转换函数,还是要自己写。
适当界定需求规模,可以减少很多开发时间,不要太钻牛角尖。不知道LZ的实际应用时怎样的,如果确实那么复杂,自己做函数吧,其实也没多困难,把思路理清楚就行了,用最简单的逻辑去实现。上面的函数只写了30分钟,测试花了10分钟。
[解决办法]
BackInDark() ( ) 信誉:100 2007-7-16 15:00:18 得分: 0

一个比较简单的方法是:
先用StringReplace()函数把 "yyyy年mm月dd日 ",中的“年”、“月”、“日”替换成 "- ",就变成了可以直接StrToDate的 "yyyy-mm-dd "格式了。

===================
想到一块去了哈~~

还有 oracal 里 好象就能直接转换的

热点排行