Delphi Tutorials »

How to format 4 digit years in Delphi?

If you want database components like DBGrid and DBEdit to show years with 4 digits instead of 2, changing the FOURDIGITYEAR variable to True in the BDE admin program seemingly has no effect. If you have Paradox or some database management system on your computer, and you change its date format to mm/dd/yyyy, that works fine in Paradox or whatever... but in your Delphi programs, the date fields of the tables still are displayed with 2 digit years!

If you use Delphi's DatePicker component, you'll see that it displays the year just like your DBGrids, so the problem has nothing to do with the database engine. But when you set Windows' Regional Settings to display dates as mm/dd/yyyy and next start a Delphi application, date fields are displayed as mm/dd/yyyy. Problem solved? No, the user surely won't appreciate it if you enforce him to change his Windows settings, resulting in problems in other applications...


The formats for dates and numbers in Delphi programs are defined by a set of global formatting variables. These variables are automatically set according to Windows' settings. As one of the first actions in your program, you can change these global variables to the desired formats, thus overriding Windows' settings (don't worry, the modified settings are only used in your application, they do not affect Windows' Regional Settings).

If your application shows dates and/or floating point numbers, set the global formatting variables ShortDateFormat, DecimalSeparator, LongDateFormat, and so on... to the desired values. Do this right after the start of your application, for example in the OnCreate event handler of the main form.

A project with examples of this technique is contained in the zip-file DFORMAT.ZIP in the Download section, under the heading Mini Tutorial Projects.


TOP    FAQ  Crash Course Delphi  Tips  Source Code  Downloads  Links

© Copyright 1999-2019 
Studiebureau Festraets