XP和Win7的日期格式在系统默认中并不一致,比如在XP下默认是'2012-1-1',在win7下却变成了'2012/1/1'。
办法1---替换法:
strSetTime :='2012/1/1' ;
if pos('/', strSetTime) > 0 then
strSetTime := StringReplace(strSetTime, '/', '-', [rfReplaceAll]);
strSetTime := StringReplace(strSetTime, '/', '-', [rfReplaceAll]);
办法 2---TFormatSettings
使用TFormatSettings来就OK了
var
t:TDateTime;
fs:TFormatSettings;
begin
fs.ShortDateFormat:='yyyy-mm-dd';
fs.DateSeparator:='-';
t := StrToDate('2010-01-01',fs);//, fs.LongDateFormat);
ShowMessage('01/01/2075 = '+DateTimeToStr(t,fs));
var
t:TDateTime;
fs:TFormatSettings;
begin
fs.ShortDateFormat:='yyyy-mm-dd';
fs.DateSeparator:='-';
t := StrToDate('2010-01-01',fs);//, fs.LongDateFormat);
ShowMessage('01/01/2075 = '+DateTimeToStr(t,fs));