Home  |  Delphi .net Home  |  System.Globalization.Calendar  |  AddHours Method
AddHours  
Method  
Add a number of Hours to a DateTime value
Calendar Class
System.Globalization NameSpace
CF1.  Function AddHours ( TheDateTime:DateTimeTheDateTime : DateTime; Hours : Integer; ) : DateTime;
CF : Methods with this mark are Compact Framework Compatible
Description
Adds Hours to TheDateTime, and returns a new DateTime with this value.
Notes
Time values are culture independant.
References
DateTime
CultureInfo
Microsoft MSDN Links
System.Globalization
System.Globalization.Calendar
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Globalization;

var
  myDate    : DateTime;

  gbCulture : System.Globalization.CultureInfo;
  gbCal     : System.Globalization.Calendar;

begin
  // Set up a Great Britain English culture & calendar
  gbCulture := System.Globalization.CultureInfo.Create('en-GB');
  gbCal     := gbCulture.Calendar;

  // Create a DateTime object : use the gbCalendar to control the
  // conversion of year, month and day parameter values into the
  // number of ticks since 1st January 0001 AD (the internal value)

  // 10:30:13.123 on 8th December 2004
  myDate := DateTime.Create(2004, 12, 8, 10, 30, 15, 123, gbCal);

  // Display the date and time
  Console.WriteLine(myDate.ToString('hh:mm:ss.fff'));

  Console.WriteLine;
  Console.WriteLine('Adding 2 hours : ');
  Console.WriteLine;

  // Add 2 hours to the date
  myDate := gbCal.AddHours(myDate, 2);

  // Display the date and time
  Console.WriteLine(myDate.ToString('hh:mm:ss.fff'));

  Console.ReadLine;
end.
Show full unit code
  10:30:15.123
  
  Adding 2 hours :
  
  12:30:15.123
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author