Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-03

Convert complicated string to DateTime

6 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Gabor Boros

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

leledumbo
to Gabor Boros
Gabor Boros wrote 8 lines
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?

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.

View this message in context: http://www.nabble.com/Convert-complicated-string-to-DateTime-tp22317964p22323185.html Sent from the Free Pascal - General mailing list .
Gabor Boros
to leledumbo

And? I don't understand your suggestion.

leledumbo írta:

Show quoted text 13 lines
Gabor Boros wrote: > 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? > 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.
leledumbo
to Gabor Boros
Gabor Boros wrote
And? 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.

View this message in context: http://www.nabble.com/Convert-complicated-string-to-DateTime-tp22317964p22325731.html Sent from the Free Pascal - General mailing list .
Gabor Boros
to leledumbo

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.

leledumbo
to Gabor Boros
Gabor Boros wrote
But 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.

View this message in context: http://www.nabble.com/Convert-complicated-string-to-DateTime-tp22317964p22344372.html Sent from the Free Pascal - General mailing list .