views:

73

answers:

2

i realize that ShortDateFormat variable represents the user's preferences.

i also realize that if settings change, Delphi will automatically refresh the ShortDateFormat variable with the user's settings.

mm/dd/yy

i have a customer who wants all "short dates" to be displayed in a particular, but they don't want to their Windows preferences.

mm/dd/yyyy

What is a good way to globally, in my application, change the ShortDateFormat variable, given that it can be reset at any time?

Note: example date format masks in code format included to make the post more visually appealing

+8  A: 

Have your own FormatSettings and use it explictly with all the format routines instead of using the implicit version.

It has also the advantage of being Thread safe.

function DateTimeToStr(const DateTime: TDateTime): string;

function DateTimeToStr(const DateTime: TDateTime; const FormatSettings: TFormatSettings): string;

Update: to avoid your application to react to locale changes, you can change Application.UpdateFormatSettings to False.
It is defaulted to True in TApplication.Create.
But beware this is not 100% bulletproof for all settings as some routines go get the ThreadLocale directly.

François
There's just too many places in the software to change *every* last one. They really want it global. Plus, some of the code is shared, i don't want everyone getting it.
Ian Boyd
+1 for using your own TFormatSettings record.
Jeroen Pluimers
+6  A: 

I agree with François, but I want to mention another option: set

Application.UpdateFormatSettings := False;

to prevent automatic updating of format settings when they are changed globally in Windows.

TOndrej
hehe. I was just about to add that!
François
I think that ShortDateFormat etc. are changed everytime you log in to Windows, or even just unlock the workstation. So this is very important.
Andreas Rejbrand
That little gem (`Application.UpdateFormatSettings`) is the secret i was hoping for. Thanks to that i don't have to put the code in `Application.OnIdle` >.>
Ian Boyd