DTStartEnd (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates the use of some routines in the DateUtils unit.

Code

var
  DateTime: TDateTime;

procedure TMainForm.btProcessClick(Sender: TObject);
var
  number: Integer;
begin
  Memo1.Lines.Clear;
  number := StrToInt(edInc.Text);
  { Increase a specific portion of the TDateTime variable }
  if RadioGroup1.ItemIndex = 0 then
    DateTime := IncMilliSecond(DateTime,number);
  if RadioGroup1.ItemIndex = 1 then
    DateTime := IncSecond(DateTime,number);
  if RadioGroup1.ItemIndex = 2 then
    DateTime := IncMinute(DateTime,number);
  if RadioGroup1.ItemIndex = 3 then
    DateTime := IncHour(DateTime,number);
  if RadioGroup1.ItemIndex = 4 then
    DateTime := IncDay(DateTime,number);
  if RadioGroup1.ItemIndex = 5 then
    DateTime := IncMonth(DateTime,number);
  if RadioGroup1.ItemIndex = 6 then
    DateTime := IncYear(DateTime,number);
  Edit1.Text := DateTimeToStr(DateTime);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  DateTime := Date + Time;
  { Verify if the time portion of the TDateTime variable occurs after noon }
  if IsPM(DateTime) then
    ShowMessage('It is after-noon')
  else
    ShowMessage('It is morning');

  { Write the start and the end of current date }
  Edit1.Text := DateTimeToStr(DateTime);
  Memo1.Lines.Add('Start of the year: ' +
    DateTimeToStr(StartOfTheYear(DateTime)));
  Memo1.Lines.Add('Start of the month: ' +
    DateTimeToStr(StartOfTheMonth(DateTime)));
  Memo1.Lines.Add('Start of the week: ' +
    DateTimeToStr(StartOfTheWeek(DateTime)));
  Memo1.Lines.Add('Start of the day: ' +
    DateTimeToStr(StartOfTheDay(DateTime)));
  Memo1.Lines.Add('Start of a year: ' +
    DateTimeToStr(StartOfAYear(YearOf(DateTime))));
  Memo1.Lines.Add('Start of a month: ' +
    DateTimeToStr(StartOfAMonth(YearOf(DateTime),MonthOf(DateTime))));
  Memo1.Lines.Add('Start of a week: ' +
    DateTimeToStr(StartOfAWeek(YearOf(DateTime),WeekOf(DateTime))));
  Memo1.Lines.Add('Start of a day: ' +
    DateTimeToStr(StartOfADay(YearOf(DateTime),DayOf(DateTime))));

  Memo1.Lines.Add('End of the year: ' +
    DateTimeToStr(EndOfTheYear(DateTime)));
  Memo1.Lines.Add('End of the month: ' +
    DateTimeToStr(EndOfTheMonth(DateTime)));
  Memo1.Lines.Add('End of the week: ' +
    DateTimeToStr(EndOfTheWeek(DateTime)));
  Memo1.Lines.Add('End of the day: ' +
    DateTimeToStr(EndOfTheDay(DateTime)));
  Memo1.Lines.Add('End of a year: ' +
    DateTimeToStr(EndOfAYear(YearOf(DateTime))));
  Memo1.Lines.Add('End of a month: ' +
    DateTimeToStr(EndOfAMonth(YearOf(DateTime),MonthOf(DateTime))));
  Memo1.Lines.Add('End of a week: ' +
    DateTimeToStr(EndOfAWeek(YearOf(DateTime),WeekOf(DateTime))));
  Memo1.Lines.Add('End of a day: ' +
    DateTimeToStr(EndOfADay(YearOf(DateTime),DayOf(DateTime))));
end;

Uses