Jump to content

Recommended Posts

Hi,

I have a project where I need to get the value of a year in a field so I use DecodeDate( dtDate, Year, Month, Day); where dtDate is a TDateTime variable, Year, Month, Day are Word variables.

 

When I run the code I get an error - 'Constant or type identifier expected'. I've downloaded the example Embarcadero have for using DecodeDate:

procedure TfAccounts.Button1Click(Sender: TObject);
var
  Present: TDateTime;
  Year, Month, Day: Word;
 begin
  Present:= Now;
  System.SysUtils.DecodeDate(Present, Year, Month, Day);
  Label1.Caption := 'Today is Day ' + IntToStr(Day) + ' of Month ' + IntToStr(Month) + ' of Year ' + IntToStr(Year);
end;

 

.. and get this same error. The 'Word' is red-underlines as the error. I have System.SysUtils in the Uses clause:

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, ... etc

 

I have used this same code in a different project and it works with no error. I can't think of why it isn't working in this particular project. Unless some other unit in the Uses clause is causing a problem, although SysUtils comes almost first.

 

Any thoughts would be welcomed.

Share this post


Link to post

version 10.4.1

 

Found the answer!!! Grrhhh, silly me.

 

I have a status bar on the form using it to write various things happening. I'd called it 'word' would you believe! Changed it to SayWord and all works fine.

 

  • Haha 1

Share this post


Link to post
2 hours ago, bazzer747 said:

procedure TfAccounts.Button1Click(Sender: TObject);

var
  Present: TDateTime;
  Year, Month, Day: Word;
 begin
  Present:= Now;
  System.SysUtils.DecodeDate(Present, Year, Month, Day);
  Label1.Caption := 'Today is Day ' + IntToStr(Day) + ' of Month ' + IntToStr(Month) + ' of Year ' + IntToStr(Year);
end;

Just on a side note, I probably would have used FormatDateTime() instead of DecodeDate() for for this, eg:

uses
  ..., System.SysUtils;

procedure TfAccounts.Button1Click(Sender: TObject);
begin
  Label1.Caption := FormatDateTime('"Today is Day "d" of Month "m" of Year "yyyy', Now);
end;

 

Share this post


Link to post

Hi,

 

Yes, thanks. I actually used this: cYear:= AnsiRightStr(DateToStr(dDate),4); when Decode wasn't behaving. Probably not as resilient as dependant on date format, I suppose, but works OK.

 

Nice to think there are several ways to achieve a result.

Share this post


Link to post
Guest
10 minutes ago, bazzer747 said:

I suppose, but works OK.

try use "System.DateUtils" unit and its functions for easy work with DateTime types.

 

not necessary many "workarounds" for your task.

 

for help you, combining basic functions, like "StrToDateTimeDEF()" or inverse functions will be very util.

 

hug

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×