Home  |  Delphi .net Home  |  System.Globalization.Calendar  |  GetYear Method
GetYear  
Method  
Return the specified DateTime Year according to the current Calendar
Calendar Class
System.Globalization NameSpace
CF1.  Function GetYear ( TheDateTime : DateTime; ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
Returns the Year value from TheDateTime value according to the current Calendar culture.
 
The GetYear method converts the ticks value in TheDateTime (1 tick = 0.0000001 seconds since 1st Jan 0001 AD) into an internal representation of years, months, days according to the Calendar culture. It then returns the Year value according to this culture.
 
For example, in the Hebrew Calendar, there are between 353 and 355 days per common year, and 383 to 385 days in a leap year.
References
DateTime
CultureInfo
Microsoft MSDN Links
System.Globalization
System.Globalization.Calendar
 
 
Getting the Year for British and Arabic cultures
// Full Unit code.
// -------------------------------------------------------------
// Create a new WinForm application, double click the form to
// create an OnLoad event, and then replace the WinForm unit
// with this text.
 
unit WinForm;
 
interface
 
uses
  System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data;
 
type
  TWinForm = class(System.Windows.Forms.Form)
  \{REGION 'Designer Managed Code'\} // Note that REGION and ENREGION should be prefixed by a dollar sign
  strict private
    ///
    /// Required designer variable.
    ///

    Components: System.ComponentModel.Container;
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///

    procedure InitializeComponent;
    procedure TWinForm_Load(sender: System.Object; e: System.EventArgs);
  {ENDREGION}
  strict protected
    ///
    /// Clean up any resources being used.
    ///

    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;
 
  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]
 
implementation
 
\{REGION 'Windows Form Designer generated code'\}
///
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
///

 
 
procedure TWinForm.InitializeComponent;
begin
  //
  // TWinForm
  //
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.ClientSize := System.Drawing.Size.Create(292, 266);
  Self.Name := 'TWinForm';
  Self.Text := 'WinForm';
  Include(Self.Load, Self.TWinForm_Load);
end;
{ENDREGION}
 
procedure TWinForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;
 
constructor TWinForm.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;
 
procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs);
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Globalization;

var
  myDate    : DateTime;

  gbCulture : System.Globalization.CultureInfo;
  saCulture : System.Globalization.CultureInfo;

  gbCal     : System.Globalization.Calendar;
  saCal     : System.Globalization.Calendar;

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

  // Set up a Saudi Arabia Arabic culture & calendar
  saCulture := System.Globalization.CultureInfo.Create('ar-SA');
  saCal     := saCulture.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)
  // Note : Gregorian Calendar is used by default anyway
  myDate := DateTime.Create(2004, 12, 8, gbCal);  // 8th December 2004

  // Now use the British calendar object to display this date
  Console.WriteLine('British calendar :');
  Console.WriteLine;
  Console.WriteLine('Era   = {0}', gbCal.GetEra(myDate).ToString);
  Console.WriteLine('Year  = {0}', gbCal.GetYear(myDate).ToString);
  Console.WriteLine('Month = {0}', gbCal.GetMonth(myDate).ToString);
  Console.WriteLine('Day   = {0}', gbCal.GetDayOfMonth(myDate).ToString);

  // Now use the Arabic calendar object to display this date
  Console.WriteLine;
  Console.WriteLine('Arabic calendar :');
  Console.WriteLine;
  Console.WriteLine('Era   = {0}', saCal.GetEra(myDate).ToString);
  Console.WriteLine('Year  = {0}', saCal.GetYear(myDate).ToString);
  Console.WriteLine('Month = {0}', saCal.GetMonth(myDate).ToString);
  Console.WriteLine('Day   = {0}', saCal.GetDayOfMonth(myDate).ToString);

  Console.ReadLine;
end.
 
end.
Hide full unit code
  British calendar :
  
  Era   = 1
  Year  = 2004
  Month = 12
  Day   = 8
  
  Arabic calendar :
  
  Era   = 1
  Year  = 1425
  Month = 10
  Day   = 26
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author