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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Current Date 5

Status
Not open for further replies.

Jonah86

Programmer
Dec 11, 2003
152
US
What is the best way to get just the currnent date? I'm wanting to display just the date, and use it in an SQL query. I'll also need to be able to subtract from it if possible. I want to show data from the current year, and 2 previous years as well.

Thanks for any help!
 
I would grab the date in a variable before hand and then use the variable in the SQL query.

Code:
var
 MyDate : String;
begin
  MyDate := DateToStr(Date);
end;

It really depends what you want to do with the date though.


"I'm a quitter. I come from a long line of quitters. It's amazing I'm here at all!
 
Another way to convert a date to a string is to use the FormatDateTime function (FormatDateTime(<format string>, <date>)) This will allow you to format the date in other ways than just the Windows Short Date format that DateToStr uses.

-D
 
DOH! I'm sorry, I meant to say current YEAR. I just need the year, and need to be able to subtract from it to get previous years. I don't need, or want, the whole date. Sorry for my goof-up =/

But thanks for the replies!
 
hi

Have a look at DecodeDate in the help, that'll break the current date down in to individual elements (day, month, year).

lou

 
Oops should mention, EncodeDate will put it back to a TDateTime format.

 
You don't specify which version of Delphi are using but the function YearOf is the simplest way of extracting the year from a date. It returns a Word which can usually be used when an integer is required.
Code:
uses DateUtils;
...
var
  datum: TDateTime;
  y: word;
...
begin
  y := YearOf ( datum );
...
end;
will assign 2004 to y assuming that datum contains today's date.

YearOf is in the DateUtils unit of Delphi 7. I haven't checked if it is in earlier versions.

There is a function called CurrentYear (in SysUtils) which actually returns the current year. You will find YearOf to be more useful in your database application as it will operate on any date.

Andrew
Hampshire, UK
 
Ah, thank you. I do have Delphi 7, so it should work out. Could I just use YearOf(date), considering date contains the current date does it not? I could be way off base there:)

Thanks again, everyone!
 
Well, I see my idea there didn't really work out:) But it did work fine when I did it your way! Thanks a ton!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top