Hi,
I want to convert a string to a TDateTime variable. In example the string is: 'November 21 09:42:21 2008'. I tried ScanDateTime with 'mmmm dd hh:nn:ss yyyy' pattern but not working. How can I convert this type of string to DateTime?
Gabor

Mailing lists / fpc-pascal / 2009-03
6 messages · 2 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi,
I want to convert a string to a TDateTime variable. In example the string is: 'November 21 09:42:21 2008'. I tried ScanDateTime with 'mmmm dd hh:nn:ss yyyy' pattern but not working. How can I convert this type of string to DateTime?
Gabor
ScanDateTime does the opposite of what FormatDateTime does, so you should look at the output of FormatDateTime with that pattern. Note that if it outputs String (such as that 'November'), it will depend on your localization.
And? I don't understand your suggestion.
leledumbo írta:
Gabor Boros wroteAnd? I don't understand your suggestion.
What's the output of this:
FormatDateTime('mmmm dd hh:nn:ss yyyy',Now);that way you can understand what you should supply to ScanDateTime as the corresponding value for mmmm format. I mean, maybe it's not November, but something else equivalent in your localization.
leledumbo írta:
What's the output of this: FormatDateTime('mmmm dd hh:nn:ss yyyy',Now);
március 04 09:42:02 2009
that way you can understand what you should supply to ScanDateTime as the corresponding value for mmmm format. I mean, maybe it's not November, but something else equivalent in your localization.
But I defined LongMonthNames in a TFormatSettings type variable and passed to ScanDateTime.
Gabor Boros wroteBut I defined LongMonthNames in a TFormatSettings type variable and passed to ScanDateTime.
That's what you didn't tell us. I'm not sure either, perhaps a bug in ScanDateTime. This little program shows it (got EConvertError):
uses
SysUtils,DateUtils;
var
f: TFormatSettings;
t: TDateTime;
begin
f:=DefaultFormatSettings;
f.LongMonthNames[11]:='November';
t:=ScanDateTime('mmmm dd hh:nn:ss:yyyy','November 21 09:42:21:2008',f);
WriteLn(FormatDateTime('mmmm dd hh:nn:ss yyyy',t));
end.but surprisingly, if I change the line that calls ScanDateTime to:
t:=ScanDateTime('mmmm dd:hh:nn:ss:yyyy','November 21:09:42:21:2008',f);it works. But then, only hh:nn:ss part returns the correct value. You may want to post a bug report.
Btw, could you post your complete TFormatSettings? Maybe it's related somehow.