Sign in Join

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

Mailing lists / fpc-pascal / 2014-02

DefaultFormatSettings usage

8 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

silvioprog

Hello,

I'm using this configuration:

initialization
  DefaultFormatSettings.DateSeparator := '-';
  DefaultFormatSettings.TimeSeparator := ':';
  DefaultFormatSettings.ShortDateFormat := 'yyyy-mm-dd';
  DefaultFormatSettings.ShortTimeFormat := 'hh:nn:ss';

but, when i try:

var
  t: TDateTime;
begin
  t := StrToDate('2013-02-04 11:39:54'); // to format yyyy-mm-dd hh:nn:ss
end;

result: "2013-02-04 11:39:54" is not a valid date format.

What am I doing wrong?

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

Dimitrios Chr. Ioannidis
to silvioprog

Hi,

Στις 4/2/2014 3:54 μμ, ο/η silvioprog έγραψε:

Show quoted text 19 lines
Hello, I'm using this configuration: initialization DefaultFormatSettings.DateSeparator := '-'; DefaultFormatSettings.TimeSeparator := ':'; DefaultFormatSettings.ShortDateFormat := 'yyyy-mm-dd'; DefaultFormatSettings.ShortTimeFormat := 'hh:nn:ss'; but, when i try: var t: TDateTime; begin t := StrToDate('2013-02-04 11:39:54'); // to format yyyy-mm-dd hh:nn:ss end; result: "2013-02-04 11:39:54" is not a valid date format.

Try the overload StrToDate(S, DefaultFormatSettings), or better declare a FFormatSettings: TFormatSettings setup it as you like pass that to the StrToDate leaving the DefaultFormatSettings.

Regards,

Dimitrios Chr. Ioannidis

Reply by email

silvioprog
to Dimitrios Chr. Ioannidis

2014-02-04 Dimitrios Chr. Ioannidis :

Show quoted text 33 lines
Hi, Στις 4/2/2014 3:54 μμ, ο/η silvioprog έγραψε: Hello, > > I'm using this configuration: > > initialization > DefaultFormatSettings.DateSeparator := '-'; > DefaultFormatSettings.TimeSeparator := ':'; > DefaultFormatSettings.ShortDateFormat := 'yyyy-mm-dd'; > DefaultFormatSettings.ShortTimeFormat := 'hh:nn:ss'; > > but, when i try: > > var > t: TDateTime; > begin > t := StrToDate('2013-02-04 11:39:54'); // to format yyyy-mm-dd hh:nn:ss > end; > > result: "2013-02-04 11:39:54" is not a valid date format. > Try the overload StrToDate(S, DefaultFormatSettings), or better declare a FFormatSettings: TFormatSettings setup it as you like pass that to the StrToDate leaving the DefaultFormatSettings. Regards, -- Dimitrios Chr. Ioannidis

Thank you Dimitrios!

Worked with:

var
  t: TDateTime;
begin
  t := StrToDateTime('2013-02-04 11:39:54');
end;

sorry, I forgot to use the function StrToDateTime. :S

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

waldo kitty
to silvioprog: I'm using this configuration: initialization DefaultFormatSettings.DateSeparator := '-';…
silvioprog wrote
t := StrToDate('2013-02-04 11:39:54'); // to format yyyy-mm-dd hh:nn:ss

the above is StrToDate but you are also sending the time... you would see similar if you were sending the above string to StrToTime...

StrToDate only accepts date formats as StrToTime only accepts time formats...

if you want to send both, you have to use StrToDateTime ;)

NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.

Reply by email

silvioprog
to waldo kitty

2014-02-04 waldo kitty :

Show quoted text 17 lines
On 2/4/2014 8:54 AM, silvioprog wrote: > t := StrToDate('2013-02-04 11:39:54'); // to format yyyy-mm-dd hh:nn:ss > the above is StrToDate but you are also sending the time... you would see similar if you were sending the above string to StrToTime... StrToDate only accepts date formats as StrToTime only accepts time formats... if you want to send both, you have to use StrToDateTime ;) -- NOTE: No off-list assistance is given without prior approval. Please keep mailing list traffic on the list unless private contact is specifically requested and granted.

Yes yes, after I saw that in typing fast, I forgot to use StrToDateTime.

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

silvioprog
to Marco van de Voort: In our previous episode, silvioprog said: Or use…

2014-02-04 Marco van de Voort :

Show quoted text 14 lines
In our previous episode, silvioprog said: > Worked with: > > var > t: TDateTime; > begin > t := StrToDateTime('2013-02-04 11:39:54'); > end; > > sorry, I forgot to use the function StrToDateTime. :S Or use http://www.freepascal.org/docs-html/rtl/dateutils/scandatetime.html

Very nice option, I did not know this function. It would be better (or fast) that the SysUtils date funcs? For example, in my new units, would be better to use ScanDateTime instead of StrToDateTime?

Thx Marco! :)

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

silvioprog
to silvioprog

2014-02-05 silvioprog :

Show quoted text 24 lines
2014-02-04 Marco van de Voort : In our previous episode, silvioprog said: > > Worked with: > > > > var > > t: TDateTime; > > begin > > t := StrToDateTime('2013-02-04 11:39:54'); > > end; > > > > sorry, I forgot to use the function StrToDateTime. :S > > Or use > > http://www.freepascal.org/docs-html/rtl/dateutils/scandatetime.html Very nice option, I did not know this function. It would be better (or fast) that the SysUtils date funcs? For example, in my new units, would be better to use ScanDateTime instead of StrToDateTime? Thx Marco! :)

It would be nice if you could:

  if TryScanDateTime(datestring, datetime) then
    raise EMyOwnExcept.Create('My own datetime exception message');
Silvio Clécio My public projects - github.com/silvioprog

Reply by email