Home  |  Delphi .net Home  |  System.Globalization.Calendar  |  GetDayOfWeek Method
GetDayOfWeek  
Method  
Gets the specified DateTime Day of Week according to the current culture
Calendar Class
System.Globalization NameSpace
CF1.  Function GetDayOfWeek ( TheDateTime : DateTime; ) : DayOfWeek;
CF : Methods with this mark are Compact Framework Compatible
Description
Returns the day of the week for the given TheDateTime value.
 
The GetDayOfWeek 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 week 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
CultureInfo
DayOfWeek
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;
  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.
Show 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