Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: How to override ShortDateFormat?

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

like image 472
Ian Boyd Avatar asked Dec 03 '22 12:12

Ian Boyd


1 Answers

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.

like image 71
Francesca Avatar answered Dec 20 '22 04:12

Francesca