Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formating Date

Status
Not open for further replies.

Delphard

Programmer
Jul 22, 2004
144
RS
How to format Date like this:
31.07.2009.
no matter what is system Date format on concrete machine?
 
Have a look at the FormatDate(...) function.

HTH
 
FormatDate(...) does NOT exist!
There is only FormatDateTime function, but when try this:
FormatDateTime('dd.mm.yyyy.', Date())
error occurs with message:
'27.01.2009.' is not a valid date.
 
Which version of Delphi are you using? This works okay on Delphi 2009 and produces today's date:
Code:
  Form1.Caption := FormatDateTime('dd.mm.yyyy.', Date());
I am surprised that you got a date of 27.01.2009. That's nearly a year ago!

Andrew
Hampshire, UK
 
'27.01.2009.' is just as example, to clearly see what is Day and what is Month in Date format I need. ((if I put other Date, like 03.01.2010. it will not be clear))

What is more important:
Error message "'27.01.2009.' is not a valid date."
occurs not in FormatDateTime function, but when I call StrToDate, like this:
...
var myDate : Date;
...
myDate := StrToDate(FormatDateTime('dd.mm.yyyy.', StartOfTheDay(Date())));
 
First thing, seperate out that nested call, it will make debugging much easier.

But (without checking) is that the correct parameter order for FormatDateTime() ?
Also you have a period[.] after the yyyy.


Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
The input parameter to StrToDate must be in the date format specified in your computer's Regional Settings.

For example, in the UK, this is usually dd/mm/yyyy.

If your computer's Regional settings is not set up as dd.mm.yyyy then you will get an EConvertError specifying that the date is not a valid date if you try to do something like:
Code:
StrToDate( '03.01.2010' );

Andrew
Hampshire, UK
 
My computer's Regional settings uses this format for Date:
dd.MM.yyyy.
(for both, Short and Lond Date format).

So, it IS same like in my code (I try even to change dd.mm.yyyy. to dd.MM.yyyy. but same error occurs).

OS is Windows 7, maybe this make problem?!
 
But your code has an additional period after the dd.mm.yyyy which makes the date format invalid. So you need to change the 'dd.mm.yyyy.' to 'dd.mm.yyyy' so that it looks like this
Code:
var myDate : Date;
...
myDate := StrToDate(FormatDateTime('dd.mm.yyyy', StartOfTheDay(Date())));

Andrew
Hampshire, UK
 
No, additional period after the dd.mm.yyyy not makes the date format invalid! I try without period and same happens!
 
Can you tell us what gets displayed if you execute this in a Delphi program (on the computer that shows the problem):
Code:
  ShowMessage( DateToStr( Now ) );

Andrew
Hampshire, UK
 
1/4/2010

But can't understand how?!?! My computer's Regional settings uses this format for Date:
dd.MM.yyyy.
?!?!
 
It is possible to override the computer's Regional Setting by setting the ShortDateFormat and/or LongDateFormat variables. For example:
Code:
ShortDateFormat := 'dd.mm.yyyy';
This has effect for the life of the program.

Perhaps this has been done in your code somewhere?

Are you aware that there are two versions of the StrToDate function? The second one allows you to specify the
Format Settings:
Code:
StrToDate( '03.01.2009', FormatSettings );

Consult your Delphi Help for information about setting using this version and how to set up FormatSettings.

Andrew
Hampshire, UK
 
towerbase,
thanks for your persistency!

I put:

ShortDateFormat := 'dd.mm.yyyy.';
LongDateFormat := 'dd.mm.yyyy.';

ShowMessage( DateToStr( Now ) );

and now output is really: '04.01.2010.'.


But, when try something like this:

myDate := StrToDate(FormatDateTime('dd.mm.yyyy.', Now));
or
MyDate := StrToDate(FormatDateTime('dd.mm.yyyy.', Now , FormatSettings));

error occurs ('not valid date format')!?!?
 
My apologies. I've misled you. Try this:
Code:
  DateSeparator := '.';
  StrToDate( FormatDateTime( 'dd.mm.yyyy', Now ) );
  StrToDate( FormatDateTime( 'dd.mm.yyyy.', Now ) ) ;
ShortDateFormat is used to specify the order of day, month and year.

All this is in the Delphi Help which I (and perhaps you) should have consulted first before posting.

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top