Sign in Join

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

Mailing lists / fpc-pascal / 2014-11

date formating dificulty

11 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Philippe

I wrote a program:

- windows 8 32 bits

- lazarus 1.2.4 ... fpc
2.6.4

then I copied it to another computer

- windows 7 64 bits

-
lazarus 1.0.14 .... fpc 2.6.2

and got into trouble with dates format!
programs behave diferent way, and what was working on the fisrt computer does not work properly in the second!

I guess I need some tutorial (I
looked for but did not find anything :( !)

why formatDateTime(
'DD/MM/YYYY', now) returns 18-11-2014 and not 18/11/2014 ?

strtodate(
formatDateTime( 'DD/MM/YYYY', now)) works ok on the w 8 32 bits, but arise an exception on the w 7 64 bits.

I am confused with the
following syntax

function FormatDateTime(
 const FormatStr: string;
 DateTime: TDateTime [1];

const FormatSettings: TFormatSettings [2]

):string;

how does it work between two format especifications: FormatStr and FormatSettings ?

appreciate any example of good
programming pratise of data formating to avoid bad surprise moving program from an OS to another !

Philippe

Links:
------
[1]
http://www.freepascal.org/docs-html/rtl/system/tdatetime.html [2]
http://www.freepascal.org/docs-html/rtl/sysutils/tformatsettings.html

Philippe
to Philippe
Philippe wrote
I wrote a program:

- windows 8 32 bits

- lazarus 1.2.4 ... fpc 2.6.4 then I

copied it to another computer

- windows 7 64 bits - lazarus

1.0.14 .... fpc 2.6.2

and got into trouble with dates format!

programs behave diferent way, and what was working on the fisrt computer does not work properly in the second!

I guess I need some tutorial

(I looked for but did not find anything :( !)

why formatDateTime(

'DD/MM/YYYY', now) returns 18-11-2014 and not 18/11/2014 ?

strtodate( formatDateTime( 'DD/MM/YYYY', now)) works ok on the w 8 32 bits, but arise an exception on the w 7 64 bits.

I am confused

with the following syntax

function FormatDateTime( const
FormatStr: string;
DateTime: TDateTime [2]; const

FormatSettings: TFormatSettings [3]

):string; how does it

work between two format especifications: FormatStr and FormatSettings ?

appreciate any example of good programming pratise of data

formating to avoid bad surprise moving program from an OS to another !

Philippe on

http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html

I

found

Program Example19; { This program demonstrates the

StrToDate function }

Uses sysutils; Procedure TestStr (S :
String);
begin Writeln (S,' : ',DateToStr(StrToDate(S)));
end;
Show quoted text 5 lines
Begin Writeln ('ShortDateFormat ',ShortDateFormat);
TestStr(DateTimeToStr(Date));
TestStr('05'+DateSeparator+'05'+DateSeparator+'1999');
TestStr('5'+DateSeparator+'5');
TestStr('5'); End. the compiler

warns that ShortDateFormat is deprecated ... which is not informed in the doc ...

_______________________________________________

fpc-pascal maillist -

http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal [1]

Links:
------
[1]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal [2]
http://www.freepascal.org/docs-html/rtl/system/tdatetime.html [3]
http://www.freepascal.org/docs-html/rtl/sysutils/tformatsettings.html

Graeme Geldenhuys
to Philippe: I wrote a program: - windows 8 32 bits - lazarus 1.2.4 ... fpc 2.6.4 then I copied it to…
Philippe wrote
why formatDateTime( 'DD/MM/YYYY', now) returns 18-11-2014 and not 18/11/2014 ?

As far as I remember the "/" character in the format string is not a literal character. It is a place holder for the date delimiter (varies by regional settings) - just like what happens to DD MM and YYYY in the format string.

Regards,
Graeme

This email and its attachments may be confidential and are intended solely for the use of the individual to whom it is addressed. Any views or opinions are solely those of the author and do not necessarily represent those of Quality Technology Systems Ltd. If you are not the intended recipient of this email and its attachments, you must take no action based upon them, nor must you copy or show them to anyone. If you receive the message in error, please immediately delete it, destroy all copies of it and notify the sender. Internet email is not a totally secure medium and therefore Quality Technology Systems Ltd does not accept legal responsibility for the contents of this message. We have taken steps to ensure that this email and any attachments are free from viruses. However, we cannot accept any responsibility for any virus transmitted by us and recommend that you subject any incoming email to your own virus checking procedure. Registered in England No. 3208823

Michael Van Canneyt
to Philippe: - windows 8 32 bits copied it to another computer 1.0.14 .... fpc 2.6.2 programs behave…
Philippe wrote 75 lines
Em 18.11.2014 09:49, Philippe escreveu: I wrote a program: - windows 8 32 bits - lazarus 1.2.4  ... fpc 2.6.4 then I copied it to another computer - windows 7 64 bits - lazarus 1.0.14 .... fpc 2.6.2 and got into trouble with dates format! programs behave diferent way, and what was working on the fisrt computer does not work properly in the second! I guess I need some tutorial (I looked for but did not find anything :( !) why formatDateTime( 'DD/MM/YYYY', now) returns 18-11-2014 and not 18/11/2014 ?   strtodate( formatDateTime( 'DD/MM/YYYY', now)) works ok on the w 8 32 bits, but arise an exception on the w 7 64 bits.   I am confused with the following syntax function FormatDateTime(   const FormatStr: string;   DateTime: TDateTime;   const FormatSettings: TFormatSettings ):string; how does it work between two format especifications: FormatStr and FormatSettings ? appreciate any example of good programming pratise of data formating to avoid bad surprise moving program from an OS to another ! Philippe   on http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html I found Program Example19; { This program demonstrates the StrToDate function } Uses sysutils; Procedure TestStr (S : String); begin Writeln (S,' : ',DateToStr(StrToDate(S))); end; Begin Writeln ('ShortDateFormat ',ShortDateFormat); TestStr(DateTimeToStr(Date)); TestStr('05'+DateSeparator+'05'+DateSeparator+'1999'); TestStr('5'+DateSeparator+'5'); TestStr('5'); End. the compiler warns that ShortDateFormat is deprecated ... which is not informed in the doc ...

It should be, I will check.

Anyway, as Graeme hinted: ShortDateFormat contains a format string, and / is a placeholder for the date separator character on the computer on which the program is running.

If you want to force a character, you can enclose it in quotes: ShortDateFormat:='dd"/"mm"/"yyyy'
will always use / as the date separator, no matter what the internatonalization settings on the PC.

But it is bad practice to do so.

Secondly, it is a common mistake to think that StrToDate will always correctly perform the opposite of DateToStr. It does not, but this is documented.

Michael.

Philippe
to Michael Van Canneyt
Michael Van Canneyt wrote
On Tue, 18
Philippe wrote
> Em 18.11.2014 09:49, Philippe escreveu:

I wrote a program: - windows 8 32 bits - lazarus 1.2.4 ... fpc 2.6.4 then I copied it to another computer - windows 7 64 bits - lazarus 1.0.14 .... fpc 2.6.2 and got into trouble with dates format! programs behave diferent way, and what was working on the fisrt computer does not work properly in the second! I guess I need some tutorial (I looked for but did not find anything :( !) why formatDateTime( 'DD/MM/YYYY', now) returns 18-11-2014 and not 18/11/2014 ? strtodate( formatDateTime( 'DD/MM/YYYY', now)) works ok on the w 8 32 bits, but arise an exception on the w 7 64 bits. I am confused with the following syntax function FormatDateTime( const FormatStr: string; DateTime: TDateTime; const FormatSettings: TFormatSettings ):string; how does it work between two format especifications: FormatStr and FormatSettings ? appreciate any example of good programming pratise of data formating to avoid bad surprise moving program from an OS to another ! Philippe on http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html [1] I found Program Example19; { This program demonstrates the StrToDate function } Uses sysutils; Procedure TestStr (S : String); begin Writeln (S,' : ',DateToStr(StrToDate(S))); end; Begin Writeln ('ShortDateFormat ',ShortDateFormat); TestStr(DateTimeToStr(Date));
TestStr('05'+DateSeparator+'05'+DateSeparator+'1999'); TestStr('5'+DateSeparator+'5'); TestStr('5'); End. the compiler warns that ShortDateFormat is deprecated ... which is not informed in the doc ...

It should be, I will check. Anyway, as Graeme hinted:

ShortDateFormat contains a format string, and / is a placeholder for the date

separator character on the computer on which the program is

running.

If you want to force a character, you can enclose it in

quotes:

ShortDateFormat:='dd"/"mm"/"yyyy' will always use / as the

date separator, no matter what the internatonalization settings on the PC.

But it is bad practice to do so. Secondly, it is a common

mistake to think that StrToDate will always correctly perform the opposite of DateToStr.

It does not, but this is documented.

Michael.

Thanks for your answer. It is a good start.

As you wrote it
is not a good pratice to do so. ... So I can repeat part of the question! ... Where can I find tutorial wh

solid; margin-left:5px;

width:100%">

____________________________________________

al
maillist -

Links:
------
[1]
http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html

Philippe
to Michael Van Canneyt: It should be, I will check. Anyway, as Graeme hinted: ShortDateFormat contains a format…
Michael Van Canneyt wrote
On Tue, 18
Philippe wrote
> Em 18.11.2014 09:49, Philippe escreveu:

I wrote a program: - windows 8 32 bits - lazarus 1.2.4 ... fpc 2.6.4 then I copied it to another computer - windows 7 64 bits - lazarus 1.0.14 .... fpc 2.6.2 and got into trouble with dates format! programs behave diferent way, and what was working on the fisrt computer does not work properly in the second! I guess I need some tutorial (I looked for but did not find anything :( !) why formatDateTime( 'DD/MM/YYYY', now) returns 18-11-2014 and not 18/11/2014 ? strtodate( formatDateTime( 'DD/MM/YYYY', now)) works ok on the w 8 32 bits, but arise an exception on the w 7 64 bits. I am confused with the following syntax function FormatDateTime( const FormatStr: string; DateTime: TDateTime; const FormatSettings: TFormatSettings ):string; how does it work between two format especifications: FormatStr and FormatSettings ? appreciate any example of good programming pratise of data formating to avoid bad surprise moving program from an OS to another ! Philippe on http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html [1] I found Program Example19; { This program demonstrates the StrToDate function } Uses sysutils; Procedure TestStr (S : String); begin Writeln (S,' : ',DateToStr(StrToDate(S))); end; Begin Writeln ('ShortDateFormat ',ShortDateFormat); TestStr(DateTimeToStr(Date));
TestStr('05'+DateSeparator+'05'+DateSeparator+'1999'); TestStr('5'+DateSeparator+'5'); TestStr('5'); End. the compiler warns that ShortDateFormat is deprecated ... which is not informed in the doc ...

It should be, I will check. Anyway, as Graeme hinted:

ShortDateFormat contains a format string, and / is a placeholder for the date

separator character on the computer on which the program is

running.

If you want to force a character, you can enclose it in

quotes:

ShortDateFormat:='dd"/"mm"/"yyyy' will always use / as the

date separator, no matter what the internatonalization settings on the PC.

But it is bad practice to do so. Secondly, it is a common

mistake to think that StrToDate will always correctly perform the opposite of DateToStr.

It does not, but this is documented.

Michael.

I still don't know how to use at the same time formatStr

and FormatSettings in FormatDateTime.

I´ll like very much to find a

tutorial or some example about the good practice to manage dates and be sure it will do the same thing on different machines ....

Show quoted text 6 lines
Thanks _______________________________________________ fpc-pascal maillist

-

http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal [2]

Links:
------
[1]
http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html [2]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Philippe
to Philippe
Philippe wrote
Em 18.11.2014 10:27,
Michael Van Canneyt wrote
> On Tue, 18 Nov 2014, Philippe
> >> Em 18.11.2014 09:49, Philippe escreveu: I wrote a

program: - windows 8 32 bits - lazarus 1.2.4 ... fpc 2.6.4 then I copied it to another computer - windows 7 64 bits - lazarus 1.0.14 .... fpc 2.6.2 and got into trouble with dates format! programs behave diferent way, and what was working on the fisrt computer does not work properly in the second! I guess I need some tutorial (I looked for but did not find anything :( !) why formatDateTime( 'DD/MM/YYYY', now) returns 18-11-2014 and not 18/11/2014 ? strtodate( formatDateTime( 'DD/MM/YYYY', now)) works ok on the w 8 32 bits, but arise an exception on the w 7 64 bits. I am confused with the following syntax function FormatDateTime( const FormatStr: string; DateTime: TDateTime; const FormatSettings: TFormatSettings ):string; how does it work between two format especifications: FormatStr and FormatSettings ? appreciate any example of good programming pratise of data formating to avoid bad surprise moving program from an OS to another ! Philippe on http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html [1] I found Program Example19; { This program demonstrates the StrToDate function } Uses sysutils; Procedure TestStr (S : String); begin Writeln (S,' : ',DateToStr(StrToDate(S))); end; Begin Writeln ('ShortDateFormat ',ShortDateFormat); TestStr(DateTimeToStr(Date));
TestStr('05'+DateSeparator+'05'+DateSeparator+'1999'); TestStr('5'+DateSeparator+'5'); TestStr('5'); End. the compiler warns that ShortDateFormat is deprecated ... which is not informed in the doc ...

> > It should be, I will check. > > Anyway, as Graeme hinted:

ShortDateFormat contains a format string, and / is a placeholder for the date

> separator character on the computer on which the program is

running.

> > If you want to force a character, you can enclose it in

quotes:

> ShortDateFormat:='dd"/"mm"/"yyyy' > will always use / as the

date separator, no matter what the internatonalization settings on the PC.

> > But it is bad practice to do so. > > Secondly, it is a

common mistake to think that StrToDate will always correctly perform the opposite of DateToStr.

> It does not, but this is documented. > >

Michael.

> > I still don't know how to use at the same time formatStr

and FormatSettings in FormatDateTime.

> > I´ll like very much to find

a tutorial or some example about the good practice to manage dates and be sure it will do the same thing on different machines ....

> >

Thanks

> > interesting stuff ... but ... out of date!!!! using

deprecatings ... (from year 2000 !!!) ... does not help much!!!

> >

http://www.math.uni-leipzig.de/pool/tuts/FreePascal/units/node21.html#SECTION002141000000000000000

> > _______________________________________________ > fpc-pascal

maillist -

>

http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal [2]

Michael Van Canneyt
to Philippe
Philippe wrote 36 lines
Em 19.11.2014 12:17, Philippe escreveu: Em 18.11.2014 10:27, Michael Van Canneyt escreveu: On Tue, 18 Nov 2014, Philippe wrote: Em 18.11.2014 09:49, Philippe escreveu: I wrote a program: - windows 8 32 bits - lazarus 1.2.4  ... fpc 2.6.4 then I copied it to another computer - windows 7 64 bits - lazarus 1.0.14 .... fpc 2.6.2 and got into trouble with dates format! programs behave diferent way, and what was working on the fisrt computer does not work properly in the second! I guess I need some tutorial (I looked for but did not find anything :( !) why formatDateTime( 'DD/MM/YYYY', now) returns 18-11-2014 and not 18/11/2014 ?   strtodate( formatDateTime( 'DD/MM/YYYY', now)) works ok on the w 8 32 bits, but arise an exception on the w 7 64 bits.   I am confused with the following syntax function FormatDateTime(   const FormatStr: string;   DateTime: TDateTime;   const FormatSettings: TFormatSettings ):string; how does it work between two format especifications: FormatStr and FormatSettings ? appreciate any example of good programming pratise of data formating to avoid bad surprise moving program from an OS to another ! Philippe   on http://www.freepascal.org/docs-html/rtl/sysutils/strtodate.html I found Program Example19; { This program demonstrates the StrToDate function } Uses sysutils; Procedure TestStr (S : String); begin Writeln (S,' : ',DateToStr(StrToDate(S))); end; Begin Writeln ('ShortDateFormat ',ShortDateFormat); TestStr(DateTimeToStr(Date)); TestStr('05'+DateSeparator+'05'+DateSeparator+'1999'); TestStr('5'+DateSeparator+'5'); TestStr('5'); End. the compiler warns that ShortDateFormat is deprecated ... which is not informed in the doc ... It should be, I will check. Anyway, as Graeme hinted: ShortDateFormat contains a format string, and / is a placeholder for the date separator character on the computer on which the program is running. If you want to force a character, you can enclose it in quotes: ShortDateFormat:='dd"/"mm"/"yyyy' will always use / as the date separator, no matter what the internatonalization settings on the PC. But it is bad practice to do so. Secondly, it is a common mistake to think that StrToDate will always correctly perform the opposite of DateToStr. It does not, but this is documented. Michael. I still don't know how to use at the same time formatStr and FormatSettings in FormatDateTime. I´ll like very much to find a tutorial or some example about the good practice to manage dates and be sure it will do the same thing on different machines ....

Good practice is to avoid converting to and from string the whole time. If you need to store a timestamp, use the ISO 8601 format or RFC 3339 (or is it 3999?).

Regarding formatsettings:

Since some versions, most date/time/money/float formatting routines support passing a TFormatSettings record to it to control the formatting. (this is mostly done for multi-threaded apps where different threads can have different settings, like in web-apps).

If no TFormatSettings record is passed, the global FormatSettings record is used.

The various format variables which existed prior to this change, have simply been moved to this global FormatSettings record, with some backwards compatibility code.

So if you had old code that did something like:

   ShortDateStr:='dd/mm/yyyy';

you should now use

   FormatSettings.ShortDateStr:='dd/mm/yyyy';

That is all there is to it. Hardly worth a tutorial.

But if you have suggestions for the documentation, I will be glad to add them.

Michael.

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote
If you need to store a timestamp, use the ISO 8601 format or RFC 3339 (or is it 3999?).

I can't agree more!!

Snippet from tiOPF code

  { Summary of ISO 8601  http://www.cl.cam.ac.uk/~mgk25/iso-time.html }
  cIntlDateTimeStor = 'yyyymmdd"T"hhnnss';    // for storage
  cIntlDateTimeDisp = 'yyyy-mm-dd hh:nn:ss';  // for display
  CIntlDateDisp     = 'yyyy-mm-dd';  // for display

See tiIntlDateDispAsDateTime(), tiIntlDateStorAsDateTime() in tiOPF's tiUtils.pas unit.

I even store TDateTime data as cIntlDateTimeStor formatted strings in databases. Never had an issue with date conversions since.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/
waldo kitty
to Philippe: I wrote a program: - windows 8 32 bits - lazarus 1.2.4 ... fpc 2.6.4 then I copied it to…
Philippe wrote
> I still don't know how to use at the same time formatStr and FormatSettings in FormatDateTime.

i'm not sure i understand your delima... perhaps you are trying to determine how the FormatSettings work and how you can force the same output format on different OSes? i found the following (slightly modified by me for CLI) delphi code that might help... the original code resides at http://www.delphibasics.co.uk/RTL.asp?Name=FormatDateTime

in the code, we output the results of the various FormatDateTime usages... then we change the values of the date and time formatting variables and run the same outputs again...

> I´ll like very much to find a tutorial or some example about the good > practice to manage dates and be sure it will do the same thing on different > machines ....

in some cases, you can fall back to delphi tutorials... it can be troublesome in some cases where things are slightly different between FPC/Lazarus and Delphi... the biggest problem i personally run into is that so much example code is GUI oriented and i still code for CLI...

anyway, here is the output of the following code...

*NOTE:* i think we've found a bug in (fpc?) trunk or perhaps it is just an unexpected side effect due to the system's settings... the day and month are reversed in the first separators line... as the comments in the code note, we're trying to work with 5 Jun 49... but this output was done on a US winwhatever (vista) system... it might be getting in the way by forcing mm/dd/yy format from the dd/mm/yy work string... someone else will have to speak to that as i would expect it to do the same in the second output set but it doesn't...

System formatting:
Working with date string = 05/06/49 01:02:03.004 Separators dd/mm/yy hh:nn:ss = 06/05/49 01:02:03
ShortMonths mmm = May
LongMonths mmmm = May
ShortDays ddd = Thu
LongDays dddd = Thursday
ShortDate ddddd = 5/6/2049
LongDate dddddd = Thursday, May 06, 2049 AMPM hhampm = 01AM
ShortTime t = 1:02
LongTime tt = 1:02:03
2DigitCentury dd/mm/yyyy = 06/05/2049

Modified formatting:
Working with date string = 05-06-49 01_02_03.004 Separators dd/mm/yy hh:nn:ss = 05-06-49 01_02_03
ShortMonths mmm = JUN
LongMonths mmmm = JUNE
ShortDays ddd = SUN
LongDays dddd = SUNDAY
ShortDate ddddd = 05-JUN-49
LongDate dddddd = SUNDAY 05 of JUNE of 1949 AMPM hhampm = 01morning
ShortTime t = 01_02_03
LongTime tt = 01 _ 02 _ 03 . 004
2DigitCentury dd/mm/yyyy = 05-06-1949

===== snip =====
program DTFormatting;
uses SysUtils;
var
   DateStr : String;
   myDate  : TDateTime;
begin
   // Set up our TDateTime variable with a full date and time :
   // 5th of June 2049 at 01:02:03.004  (.004 milli-seconds)
   //
   // Note that 49 is treated as 2049 as follows :
   //               TwoDigitYearCenturyWindow => 50
   //                            Current year => 2008 (at time of writing)
   //      Subtract TwoDigitYearCenturyWindow => 1958
   //            2 digit year to be converted => 49
   //  Compare with the last 2 digits of 1958 => Less
   //      So the year is in the next century => 2049
   // (58 would be converted to 1958)
   WriteLn('System formatting:');
   DateStr := '05/06/49 01:02:03.004';
   WriteLn('  Working with date string     = '+DateStr);
   myDate := StrToDateTime(DateStr);
   // Demonstrate default locale settings
   // Use the DateSeparator and TimeSeparator values
   WriteLn('  Separators dd/mm/yy hh:nn:ss = '+FormatDateTime('dd/mm/yy
hh:nn:ss', myDate));
   // Use ShortMonthNames
   WriteLn('  ShortMonths              mmm = '+FormatDateTime('mmm', myDate));
   // Use LongMonthNames
   WriteLn('  LongMonths              mmmm = '+FormatDateTime('mmmm', myDate));
   // Use ShortDayNames
   WriteLn('  ShortDays                ddd = '+FormatDateTime('ddd', myDate));
   // Use LongDayNames
   WriteLn('  LongDays                dddd = '+FormatDateTime('dddd', myDate));
   // Use the ShortDateFormat string
   WriteLn('  ShortDate              ddddd = '+FormatDateTime('ddddd', myDate));
   // Use the LongDateFormat string
   WriteLn('  LongDate              dddddd = '+FormatDateTime('dddddd', myDate));
   // Use the TimeAmString
   WriteLn('  AMPM                  hhampm = '+FormatDateTime('hhampm', myDate));
   // Use the ShortTimeFormat string
   WriteLn('  ShortTime                  t = '+FormatDateTime('t', myDate));
   // Use the LongTimeFormat string
   WriteLn('  LongTime                  tt = '+FormatDateTime('tt', myDate));
   // Use the TwoDigitCenturyWindow
   WriteLn('  2DigitCentury     dd/mm/yyyy = '+FormatDateTime('dd/mm/yyyy',
myDate));
   WriteLn('');
   WriteLn('Modified formatting:');
   // Now change the defaults
   DefaultFormatSettings.DateSeparator      := '-';
   DefaultFormatSettings.TimeSeparator      := '_';
   DefaultFormatSettings.ShortDateFormat    := 'dd/mmm/yy';
   DefaultFormatSettings.LongDateFormat     := 'dddd dd "of" mmmm "of" yyyy';
   DefaultFormatSettings.TimeAMString       := 'morning';
   DefaultFormatSettings.TimePMString       := 'afternoon';
   DefaultFormatSettings.ShortTimeFormat    := 'hh:nn:ss';
   DefaultFormatSettings.LongTimeFormat     := 'hh : nn : ss . zzz';
   DefaultFormatSettings.ShortMonthNames[6] := 'JUN';
   DefaultFormatSettings.LongMonthNames[6]  := 'JUNE';
   DefaultFormatSettings.ShortDayNames[1]   := 'SUN';
   DefaultFormatSettings.LongDayNames[1]    := 'SUNDAY';
   DefaultFormatSettings.TwoDigitYearCenturyWindow := 75; // This means 49 is
treated as 1949
   // Set up our TDateTime variable with the same value as before
   // **except that we must use the new date and time separators**
   // The TwoDigitYearCenturyWindow variable only takes effect here
   DateStr := '05-06-49 01_02_03.004';
   WriteLn('  Working with date string     = '+DateStr);
   myDate := StrToDateTime(DateStr);
   // Use the DateSeparator and TimeSeparator values
   WriteLn('  Separators dd/mm/yy hh:nn:ss = '+FormatDateTime('dd/mm/yy
hh:nn:ss', myDate));
   // Use ShortMonthNames
   WriteLn('  ShortMonths              mmm = '+FormatDateTime('mmm', myDate));
   // Use LongMonthNames
   WriteLn('  LongMonths              mmmm = '+FormatDateTime('mmmm', myDate));
   // Use ShortDayNames
   WriteLn('  ShortDays                ddd = '+FormatDateTime('ddd', myDate));
   // Use LongDayNames
   WriteLn('  LongDays                dddd = '+FormatDateTime('dddd', myDate));
   // Use the ShortDateFormat string
   WriteLn('  ShortDate              ddddd = '+FormatDateTime('ddddd', myDate));
   // Use the LongDateFormat string
   WriteLn('  LongDate              dddddd = '+FormatDateTime('dddddd', myDate));
   // Use the TimeAmString
   WriteLn('  AMPM                  hhampm = '+FormatDateTime('hhampm', myDate));
   // Use the ShortTimeFormat string
   WriteLn('  ShortTime                  t = '+FormatDateTime('t', myDate));
   // Use the LongTimeFormat string
   WriteLn('  LongTime                  tt = '+FormatDateTime('tt', myDate));
   // Use the TwoDigitCenturyWindow
   WriteLn('  2DigitCentury     dd/mm/yyyy = '+FormatDateTime('dd/mm/yyyy',
myDate));
end.
===== snip =====
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.
Philippe
to waldo kitty
waldo kitty wrote
On 11/19/2014 9:17
Philippe wrote
>> I still don't know how to use at the same

time formatStr and FormatSettings in FormatDateTime.

i'm not sure i

understand your delima... perhaps you are trying to determine how

the

FormatSettings work and how you can force the same output format on

different OSes? i found the following (slightly modified by me for CLI) delphi

code that might help... the original code resides at

http://www.delphibasics.co.uk/RTL.asp?Name=FormatDateTime [1]in the code, we output the results of the various FormatDateTime usages... then

we change the values of the date and time formatting variables and

run the same

outputs again... >> I´ll like very much to find a

tutorial or some example about the good practice to manage dates and be sure it will do the same thing on different machines ....

in some

cases, you can fall back to delphi tutorials... it can be troublesome in

some cases where things are slightly different between FPC/Lazarus

and Delphi...

the biggest problem i personally run into is that so

much example code is GUI

oriented and i still code for CLI...

anyway, here is the output of the following code...

*NOTE:* i think

we've found a bug in (fpc?) trunk or perhaps it is just an

unexpected

side effect due to the system's settings... the day and month are

reversed in the first separators line... as the comments in the code note, we're

trying to work with 5 Jun 49... but this output was done

on a US winwhatever

(vista) system... it might be getting in the way

by forcing mm/dd/yy format from

the dd/mm/yy work string... someone

else will have to speak to that as i would

expect it to do the same

in the second output set but it doesn't...

System formatting:

Working with date string = 05/06/49 01:02:03.004

Separators dd/mm/yy

hh:nn:ss = 06/05/49 01:02:03

ShortMonths mmm = May LongMonths mmmm =

May

ShortDays ddd = Thu LongDays dddd = Thursday ShortDate ddddd =

5/6/2049

LongDate dddddd = Thursday, May 06, 2049 AMPM hhampm =

01AM

ShortTime t = 1:02 LongTime tt = 1:02:03 2DigitCentury

dd/mm/yyyy = 06/05/2049

Modified formatting: Working with date

string = 05-06-49 01_02_03.004

Separators dd/mm/yy hh:nn:ss = 05-06-49

01_02_03

ShortMonths mmm = JUN LongMonths mmmm = JUNE ShortDays

ddd = SUN

LongDays dddd = SUNDAY ShortDate ddddd = 05-JUN-49

LongDate dddddd = SUNDAY 05 of JUNE of 1949

AMPM hhampm = 01morning

ShortTime t = 01_02_03

LongTime tt = 01 _ 02 _ 03 . 004

2DigitCentury dd/mm/yyyy = 05-06-1949

===== snip ===== program
DTFormatting;
Show quoted text 6 lines
uses SysUtils; var DateStr : String; myDate
: TDateTime;
begin // Set up our TDateTime variable with a full

date and time :

// 5th of June 2049 at 01:02:03.004 (.004

milli-seconds)

// // Note that 49 is treated as 2049 as follows :
// TwoDigitYearCenturyWindow => 50
// Current year => 2008 (at time of

writing)

// Subtract TwoDigitYearCenturyWindow => 1958 // 2 digit

year to be converted => 49

// Compare with the last 2 digits of 1958

=> Less

// So the year is in the next century => 2049 // (58 would

be converted to 1958)

WriteLn('System formatting:'); DateStr
:= '05/06/49 01:02:03.004';
WriteLn(' Working with date string =
'+DateStr);
myDate := StrToDateTime(DateStr); // Demonstrate

default locale settings

// Use the DateSeparator and TimeSeparator

values

WriteLn(' Separators dd/mm/yy hh:nn:ss =

'+FormatDateTime('dd/mm/yy

hh:nn:ss', myDate)); // Use

ShortMonthNames

WriteLn(' ShortMonths mmm = '+FormatDateTime('mmm',
myDate));
// Use LongMonthNames WriteLn(' LongMonths mmmm =
'+FormatDateTime('mmmm', myDate));
// Use ShortDayNames WriteLn('
ShortDays ddd = '+FormatDateTime('ddd', myDate));
// Use

LongDayNames

WriteLn(' LongDays dddd = '+FormatDateTime('dddd',
myDate));
// Use the ShortDateFormat string WriteLn(' ShortDate
ddddd = '+FormatDateTime('ddddd', myDate));
// Use the

LongDateFormat string

WriteLn(' LongDate dddddd =
'+FormatDateTime('dddddd', myDate));
// Use the TimeAmString
WriteLn(' AMPM hhampm = '+FormatDateTime('hhampm', myDate));
// Use

the ShortTimeFormat string

WriteLn(' ShortTime t =
'+FormatDateTime('t', myDate));
// Use the LongTimeFormat string
WriteLn(' LongTime tt = '+FormatDateTime('tt', myDate));
// Use the

TwoDigitCenturyWindow

WriteLn(' 2DigitCentury dd/mm/yyyy =

'+FormatDateTime('dd/mm/yyyy',

myDate)); WriteLn('');
WriteLn('Modified formatting:');
// Now change the defaults
DefaultFormatSettings.DateSeparator := '-';
DefaultFormatSettings.TimeSeparator := '_';
DefaultFormatSettings.ShortDateFormat := 'dd/mmm/yy';
DefaultFormatSettings.LongDateFormat := 'dddd dd "of" mmmm "of" yyyy';
DefaultFormatSettings.TimeAMString := 'morning';
DefaultFormatSettings.TimePMString := 'afternoon';
DefaultFormatSettings.ShortTimeFormat := 'hh:nn:ss';
DefaultFormatSettings.LongTimeFormat := 'hh : nn : ss . zzz';
DefaultFormatSettings.ShortMonthNames[6] := 'JUN';
DefaultFormatSettings.LongMonthNames[6] := 'JUNE';
DefaultFormatSettings.ShortDayNames[1] := 'SUN';
DefaultFormatSettings.LongDayNames[1] := 'SUNDAY';
DefaultFormatSettings.TwoDigitYearCenturyWindow := 75; // This means 49
is
treated as 1949 // Set up our TDateTime variable with the

same value as before

// **except that we must use the new date and

time separators**

// The TwoDigitYearCenturyWindow variable only takes

effect here

DateStr := '05-06-49 01_02_03.004'; WriteLn(' Working
with date string = '+DateStr);
Show quoted text 6 lines
myDate := StrToDateTime(DateStr); // Use the DateSeparator and TimeSeparator values WriteLn('

Separators dd/mm/yy hh:nn:ss = '+FormatDateTime('dd/mm/yy

hh:nn:ss',
myDate));
// Use ShortMonthNames WriteLn(' ShortMonths mmm =
'+FormatDateTime('mmm', myDate));
// Use LongMonthNames WriteLn('
LongMonths mmmm = '+FormatDateTime('mmmm', myDate));
// Use

ShortDayNames

WriteLn(' ShortDays ddd = '+FormatDateTime('ddd',
myDate));
// Use LongDayNames WriteLn(' LongDays dddd =
'+FormatDateTime('dddd', myDate));
// Use the ShortDateFormat

string

WriteLn(' ShortDate ddddd = '+FormatDateTime('ddddd',
myDate));
// Use the LongDateFormat string WriteLn(' LongDate
dddddd = '+FormatDateTime('dddddd', myDate));
// Use the

TimeAmString

WriteLn(' AMPM hhampm = '+FormatDateTime('hhampm',
myDate));
// Use the ShortTimeFormat string WriteLn(' ShortTime t
= '+FormatDateTime('t', myDate));
// Use the LongTimeFormat

string

WriteLn(' LongTime tt = '+FormatDateTime('tt', myDate));
// Use the TwoDigitCenturyWindow
WriteLn(' 2DigitCentury dd/mm/yyyy =

'+FormatDateTime('dd/mm/yyyy',

myDate)); end. ===== snip

=====

Thanks to you, to Graeme and Michael!

That almost what I was
looking for. And did not find. Quite simple indeed!

More as a
curiosity: how should one use formatDateTime( formatStr, dt, formatSettings);

Philippe

Links:
------
[1]
http://www.delphibasics.co.uk/RTL.asp?Name=FormatDateTime