Description |
Returns the Week of Year value from TheDateTime value according to the current Calendar culture.
The GetWeekOfYear 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 Week 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.]
It uses the CalendarRule and FirstDayOfWeek values to calculate the week of the year.
|
| References | DateTime
CultureInfo
CalendarWeekRule
DayOfWeek
|
|
Microsoft MSDN Links |
System.Globalization
System.Globalization.Calendar
|
|
|
Getting the Week of Year for British and Arabic cultures |
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;
gbWeek : Integer; // British week in year
saWeek : Integer; // Arabic week in year
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
// Get the British week in the year
gbWeek := gbCal.GetWeekOfYear(myDate,
gbCulture.DateTimeFormat.CalendarWeekRule,
gbCulture.DateTimeFormat.FirstDayOfWeek);
// Get the Arabic week in the year
saWeek := saCal.GetWeekOfYear(myDate,
saCulture.DateTimeFormat.CalendarWeekRule,
saCulture.DateTimeFormat.FirstDayOfWeek);
// 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 month = {0}', gbCal.GetDayOfMonth(myDate).ToString);
Console.WriteLine('Week of year = {0}', gbWeek.ToString);
// 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 month = {0}', saCal.GetDayOfMonth(myDate).ToString);
Console.WriteLine('Week of year = {0}', saWeek.ToString);
Console.ReadLine;
end.
| Show full unit code | British calendar :
Year = 2004
Month = 12
Day of month = 8
Week of year = 50
Arabic calendar :
Year = 1425
Month = 10
Day of month = 26
Week of year = 42
| | Using different criteria for the first week of the year | program Project1;
{$APPTYPE CONSOLE}
uses
System.Globalization;
var
gbCulture : System.Globalization.CultureInfo;
gbCal : System.Globalization.Calendar;
date : DateTime;
day : Integer;
week : Integer;
begin
// Set up a Great Britain English culture & calendar
gbCulture := System.Globalization.CultureInfo.Create('en-GB');
gbCal := gbCulture.Calendar;
Console.WriteLine('Using CalendarWeekRule.FirstDay :');
Console.WriteLine;
for day := 1 to 7 do
begin
date := DateTime.Create(2004, 1, day, gbCal);
week := gbCal.GetWeekOfYear(date,
CalendarWeekRule.FirstDay,
DayOfWeek.Monday);
Console.WriteLine('{0} is in week {1}',
date.ToString('ddd dd MMM yyyy'),
week.ToString);
end;
Console.WriteLine;
Console.WriteLine('Using CalendarWeekRule.FirstFullWeek :');
Console.WriteLine;
for day := 1 to 7 do
begin
date := DateTime.Create(2004, 1, day, gbCal);
week := gbCal.GetWeekOfYear(date,
CalendarWeekRule.FirstFullWeek,
DayOfWeek.Monday);
Console.WriteLine('{0} is in week {1}',
date.ToString('ddd dd MMM yyyy'),
week.ToString);
end;
Console.WriteLine;
Console.WriteLine('Using CalendarWeekRule.FirstFourDayWeek :');
Console.WriteLine;
for day := 1 to 7 do
begin
date := DateTime.Create(2004, 1, day, gbCal);
week := gbCal.GetWeekOfYear(date,
CalendarWeekRule.FirstFourDayWeek,
DayOfWeek.Monday);
Console.WriteLine('{0} is in week {1}',
date.ToString('ddd dd MMM yyyy'),
week.ToString);
end;
Console.ReadLine;
end.
| Show full unit code | Using CalendarWeekRule.FirstDay :
Thu 01 Jan 2004 is in week 1
Fri 02 Jan 2004 is in week 1
Sat 03 Jan 2004 is in week 1
Sun 04 Jan 2004 is in week 1
Mon 05 Jan 2004 is in week 2
Tue 06 Jan 2004 is in week 2
Wed 07 Jan 2004 is in week 2
Using CalendarWeekRule.FirstFullWeek :
Thu 01 Jan 2004 is in week 52
Fri 02 Jan 2004 is in week 52
Sat 03 Jan 2004 is in week 52
Sun 04 Jan 2004 is in week 52
Mon 05 Jan 2004 is in week 1
Tue 06 Jan 2004 is in week 1
Wed 07 Jan 2004 is in week 1
Using CalendarWeekRule.FirstFourDayWeek :
Thu 01 Jan 2004 is in week 1
Fri 02 Jan 2004 is in week 1
Sat 03 Jan 2004 is in week 1
Sun 04 Jan 2004 is in week 1
Mon 05 Jan 2004 is in week 2
Tue 06 Jan 2004 is in week 2
Wed 07 Jan 2004 is in week 2
|
|
|
|