Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TDateTime format usage

1,278 views
Skip to first unread message

Rob Decker

unread,
Jul 11, 2002, 4:56:48 AM7/11/02
to
Hi,

I am storing a TDateTime in an ini file. I live in America where Christmas
is 12/25/2002. Users elsewhere in the world write it as 25/12/2002. When
my program reads in the DateTime (12/25/2002) in a country that uses the
other format, they get errors.

My question: What format should I store the DateTime in?

Thanks,

Rob


Werner

unread,
Jul 11, 2002, 5:32:51 AM7/11/02
to
Hi Rob,

I use to "normalize" the date, when I store it in a string. I use the format
'yyyymmdd'.

DateString := FormatDateTime('yyyymmdd', Date);
-> can be stored in Inifile

and back to TDateTime
DateVar := EncodeDate(StrToInt(Copy(DateString, 1, 4)),
StrToInt(Copy(DateString, 5, 2)), StrToInt(Copy(DateString, 7, 2)));

Hope that helped.
Regards, Werner


"Rob Decker" <r...@XXrdcomp.net> schrieb im Newsbeitrag
news:3d2d484d_2@dnews...

Daniel Hansen

unread,
Jul 11, 2002, 5:34:20 AM7/11/02
to

In your application you can set the ShortDateFormat global variable to
what you like to use. ShortDateFormat is initiated to the value from
Regional settings, but you are free to set it explicitly.

HTH,
Daniel

Daniel Hansen

unread,
Jul 11, 2002, 7:00:45 AM7/11/02
to
Why not just set the global variable ShortDateFormat when your app
starts, and then just use DateToStr and StrToDate. I Really think that
is much easier.

HTH,
Daniel

Werner

unread,
Jul 11, 2002, 8:28:26 AM7/11/02
to
Hi Daniel,

shure this is easier, but I prefer to set ShortDateFormat to a constant
format for screen output only. I don´t like to switch it frequently for
Inistorage and back for screen-output, but that is only my personal flavour.
And by the way: my solution ain´t *that* complicated, is it?

Werner


"Daniel Hansen" <daniel...@gecapital.com> schrieb im Newsbeitrag
news:3D2D655D...@gecapital.com...

John Herbster

unread,
Jul 11, 2002, 8:46:50 AM7/11/02
to
"Rob Decker" <r...@XXrdcomp.net> wrote ...
> What format should I store [in an TIniFile] the DateTime?

I recommend storing DateTime in TIniFiles as a float. --JohnH


Jens Gruschel

unread,
Jul 11, 2002, 9:05:39 AM7/11/02
to
> Why not just set the global variable ShortDateFormat when your app
> starts, and then just use DateToStr and StrToDate. I Really think that
> is much easier.

It's not easier if you want to store dates to a file in a common format, but
also want to display dates on the screen in the format of the user's local
settings.

Jens

Werner

unread,
Jul 11, 2002, 9:42:21 AM7/11/02
to
"John Herbster" <Jo...@petronworld.com> schrieb im Newsbeitrag
news:3d2d7e3c_2@dnews...

> "Rob Decker" <r...@XXrdcomp.net> wrote ...
> > What format should I store [in an TIniFile] the DateTime?
>
> I recommend storing DateTime in TIniFiles as a float. --JohnH
>

Possible, but hard to read, when you look into the Inifile

Regards, Werner

Ken White

unread,
Jul 11, 2002, 9:16:11 AM7/11/02
to
Rob,

> My question: What format should I store the DateTime in?

Since you're only storing the date portion and not the time,
why not store it as an integer:

Ini.WriteInteger('Holidays', 'Christmas', Trunc(XMas));

Reading it back:

XMas := Ini.ReadInteger('Holidays', 'Christmas', 0);

Now, no worries about the date format at all anywhere.

Ken
---
Ken White
kwh...@adpsi.com

Clipper Functions for Delphi and C++ Builder
http://www.adpsi.com

Mike Walsh

unread,
Jul 11, 2002, 9:08:33 AM7/11/02
to
> It's not easier if you want to store dates to a file in a common format,
but
> also want to display dates on the screen in the format of the user's local
> settings.
>
> Jens

Especially if the file you store the dates is ever shared amongst different
machines with different locales.

Mike


Daniel Hansen

unread,
Jul 11, 2002, 10:11:33 AM7/11/02
to
Agreed,

Daniel

Daniel Hansen

unread,
Jul 11, 2002, 10:20:31 AM7/11/02
to
This is the good solution, as long the ini-file is not to be manually
edited.

Daniel

Daniel Hansen

unread,
Jul 11, 2002, 10:17:53 AM7/11/02
to
Werner wrote:
> Hi Daniel,
>
> shure this is easier, but I prefer to set ShortDateFormat to a constant
> format for screen output only. I don´t like to switch it frequently for
> Inistorage and back for screen-output, but that is only my personal flavour.
> And by the way: my solution ain´t *that* complicated, is it?

Absolutely not, and after reading more of this thread I conclude that
other (than my) solutions is much better.

Thanks,
Daniel

Peter Below (TeamB)

unread,
Jul 11, 2002, 2:23:05 PM7/11/02
to
In article <3d2d7e3c_2@dnews>, John Herbster wrote:
>
> I recommend storing DateTime in TIniFiles as a float. --JohnH

Has also a locale problem (DecimalSeparator) and is of course not
human-readable as a date.

--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


John Herbster

unread,
Jul 11, 2002, 2:37:25 PM7/11/02
to
"Peter Below (TeamB)" wrote

> > I recommend storing DateTime in TIniFiles as a float.
> Has also a locale problem (DecimalSeparator) ...

Thanks, I did not know that the locale affected TIniFiles.
Back during the "metric" and decimal revolution why didn't
the French get the world to use the same character for
the decimal point? <g> Regards, JohnH


Jens Gruschel

unread,
Jul 11, 2002, 5:15:25 PM7/11/02
to

Just one final note: You are not the only one with this problem,
designers of the libraries have noticed it, too. Anders Hejlsberg and
his team introduced a new format string to the .net framework for
converting binary data (like DateTime or Double) to strings while making
sure it can be converted back no matter what regional settings are used.
In Delphi we still have to do this ourselves, but Delphi .NET is on it's
way.

Jens

John Herbster

unread,
Jul 11, 2002, 7:04:07 PM7/11/02
to

"Dr John Stockton" <sp...@merlyn.demon.co.uk> wrote
> If it is a date, store it as ISO-8601 format, YYYY-MM-DD.
> Everyone will recognise it as a date, and will understand
> it as a date. It can be converted to a TDateTime,
> independently of location, by something like ...

Compellingly good reasons! --JohnH


Dr John Stockton

unread,
Jul 11, 2002, 3:26:13 PM7/11/02
to
JRS: In article <3d2d484d_2@dnews>, seen in news:borland.public.delphi.
objectpascal, Rob Decker <r...@XXrdcomp.net> posted at Thu, 11 Jul 2002
01:56:48 :-

If it is a date, store it as ISO-8601 format, YYYY-MM-DD. Everyone will


recognise it as a date, and will understand it as a date. It can be
converted to a TDateTime, independently of location, by something like

DT := EncodeDate(X(S, 1, 4), X(S, 6, 2), X(S, 9, 2)) ;

with function X(const S : string ; const J, K : integer) : integer ;
var A : integer ;
begin Result := 0 ;
for A := J to J+K-1 do Result := Result*10 + Ord(S[A])-48 ;
end ;

There may be a better way to convert; but that, once debugged, should be
good enough.

You can also store it as an integer; a TDateTime should be an exact
integer for a date, but you can always Round to be sure.

If it is a Date+Time, then either YYYY-MM-DD hh:mm:ss.sss or as a
Double, which can be written either in float or fixed point format,
depending on needs. Or the 8 bytes can be converted to 16 hex
characters, to be certain of exactness.


Never multinationalise if it can be avoided; instead, internationalise.

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.txt
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Jens Gruschel

unread,
Jul 12, 2002, 3:42:50 AM7/12/02
to
> Back during the "metric" and decimal revolution why didn't
> the French get the world to use the same character for
> the decimal point? <g> Regards, JohnH

And the Germans, too. And why are there different units like 蚓 and 蚌? Why
miles and meters? Probably that we don't get bored... :-)

Jens

Safari

unread,
Jul 12, 2002, 9:48:58 AM7/12/02
to
Werner hello,

Var enYear, enMonth, enDay: Word;
EncodeDate(Date, enYear, enMonth, enDay);

if date has 12/25/2002 then result =
enYear = 2002
enMonth = 12
enDay = 25

Best Regards,
Safari.

"Werner" <wer...@nospam.de> wrote in message
news:agjj7o$gci$01$1...@news.t-online.com...

Brett Watters

unread,
Jul 12, 2002, 12:27:02 PM7/12/02
to
"John Herbster" <Jo...@petronworld.com> wrote in message
news:3d2d7e3c_2@dnews...

> "Rob Decker" <r...@XXrdcomp.net> wrote ...
> > What format should I store [in an TIniFile] the DateTime?
>
> I recommend storing DateTime in TIniFiles as a float. --JohnH

Don't try this one, Europeans like using comma as a decimal separator.
On StrToFloat it will bark.

If you are only worried about date (not time), you can just store the
integer part of it.

Personally, I'd recommend:
Day=#
Month=#
Year=#

Thanks,

Brett

Dr John Stockton

unread,
Jul 12, 2002, 11:31:25 AM7/12/02
to
JRS: In article <3D2DF56D...@pegtop.net>, seen in news:borland.pu
blic.delphi.objectpascal, Jens Gruschel <j...@pegtop.net> posted at Thu,
11 Jul 2002 23:15:25 :-


If Anders can do it for .net, presumably comparatively ordinary people
could do something similar in Delphi - in various ways.

It could be nice if a friend of Anders could produce Delphi code to do
exactly the same conversions, and if it could be properly publicised
(including here, and in FAQs) as matching the Anders standard.

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk / JR.St...@physics.org ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)

Jens Gruschel

unread,
Jul 15, 2002, 4:18:00 AM7/15/02
to
> If Anders can do it for .net, presumably comparatively ordinary people
> could do something similar in Delphi - in various ways.

Of course anyone could do this (and I guess some already have done it). I
just meant that in .NET it's part of the framework, and the standard format
string "R" (for round-trip) can be used for a lot of datatypes (int, double,
decimal, DateTime etc.). So if you wait for Delphi .NET (probably you cannot
and don't want to), there is a common way to solve your problem:

Double.ToString("R") works as well as DateTime.ToString("R"), so in Delphi
we would need an overloaded function like
ConvertToString(V: Double, FormatString: String); overload;
ConvertToString(V: TDateTime, FormatString: String); overload; etc.
and a similar function for converting the string back.

> It could be nice if a friend of Anders could produce Delphi code to do
> exactly the same conversions

Of course Borland could introduce such a function. On the other hand they
probably have much work with Delphi .NET (and other stuff), so maybe such a
function is not on top of the to-do list (and for .NET they don't need to
implement such a function since it is part of the framework).

Jens

0 new messages