Home  |  Delphi .net Home  |  System.Globalization.Calendar  |  GetDayOfYear Method
GetDayOfYear  
Method  
Gets the specified DateTime Day of Year according to the current culture
Calendar Class
System.Globalization NameSpace
CF1.  Function GetDayOfYear ( TheDateTime : DateTime; ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
Returns the day of the yearfor the given TheDateTime value.
 
The GetDayOfYear 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 Day of 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
 
 
A simple example
// 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('Year           = {0}', gbCal.GetYear(myDate).ToString);
  Console.WriteLine('Month          = {0}', gbCal.GetMonth(myDate).ToString);
  Console.WriteLine('Day of    year = {0}', gbCal.GetDayOfYear(myDate).ToString);
  Console.WriteLine('Day of   month = {0}', gbCal.GetDayOfMonth(myDate).ToString);
  Console.WriteLine('Day of    week = {0}', gbCal.GetDayOfWeek(myDate));

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

  Console.ReadLine;
end.
 
end.
Hide full unit code
  British calendar :
  
  Year           = 2004
  Month          = 12
  Day of    year = 343
  Day of   month = 8
  Day of    week = Wednesday
  
  Arabic calendar :
  
  Year           = 1425
  Month          = 10
  Day of    year = 292
  Day of   month = 26
  Day of    week = Wednesday
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author