To get the current week count in Delphi, you can use the ISOWeek
unit that provides functions for handling ISO 8601 week dates. Here’s an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
unit MainForm; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, ISOWeek; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private function GetCurrentWeekNumber: Integer; public { Public declarations } end; var Form1: TForm1; implementation {$R *.fmx} procedure TForm1.FormCreate(Sender: TObject); begin ShowMessage(‘Current Week: ‘ + IntToStr(GetCurrentWeekNumber)); end; function TForm1.GetCurrentWeekNumber: Integer; var CurrentDate: TDateTime; begin // Get the current date CurrentDate := Now; // Use ISOWeekOfYear function from ISOWeek unit to get the week number Result := ISOWeekOfYear(CurrentDate); end; end. |
In this example:
- The
ISOWeek
unit is included in theuses
clause to access theISOWeekOfYear
function. - The
GetCurrentWeekNumber
function gets the current date usingNow
and then calculates the ISO week number usingISOWeekOfYear
. - The
FormCreate
event handler is triggered when the form is created, and it displays the current week number using a message box.
Make sure to include the ISOWeek
unit in your uses clause. The ISO week system defines a week as starting on Monday and ending on Sunday, with the first week of the year containing the first Thursday of the year.
Leave a Reply