bazzer747 25 Posted February 23, 2021 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
Lars Fosdal 1792 Posted February 23, 2021 Which Delphi version? I am unable to reproduce in 10.4.1 - i.e. works as expected. Share this post Link to post
Lars Fosdal 1792 Posted February 23, 2021 Is it possible to Ctrl-click on Word ? Share this post Link to post
bazzer747 25 Posted February 23, 2021 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. 1 Share this post Link to post
Remy Lebeau 1393 Posted February 23, 2021 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
bazzer747 25 Posted February 23, 2021 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 Posted February 23, 2021 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
Lars Fosdal 1792 Posted February 23, 2021 5 hours ago, bazzer747 said: called it 'word' That was what I suspected. Share this post Link to post