'Declaration Public Overloads Function GetWeekNumberForDate( _ ByVal date As Date, _ ByRef yearContainingWeek As Integer _ ) As Integer
public int GetWeekNumberForDate( DateTime date, out int yearContainingWeek )
The System.Globalization.Calendar's GetWeekOfYear method simply returns the number of weeks into the year that the date falls on. It does not seem to use the weekrule specified in the arguments. e.g. If the first day of week is sunday and the weekrule is Jan 1, then 12/31/2000 (Sunday) should return 1. Instead it returns 54 and 1/1/2001 (Monday) returns 1.
This routine returns the correct week number for the specified date based on the WeekRule and FirstDayOfWeekResolved.
Note, this may be a week for a year other than the year of the date. e.g. 12/31/2000, week rule of Jan 1, and week start of sunday will return 1 because this date falls into the first week of the year 2001.
Imports Infragistics.Win Imports Infragistics.Win.UltraWinSchedule Imports System.IO Imports System.Globalization Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click Dim yearNumber As Integer Dim weekNumber As Integer weekNumber = Me.ultraCalendarInfo1.GetWeekNumberForDate(DateTime.Today, yearNumber) Dim info As String = "The number of the current week is " + weekNumber.ToString() + "." + vbCrLf info += "The week falls in the year " + yearNumber.ToString() + "." + vbCrLf ' Display the information MessageBox.Show(info, "GetWeek", MessageBoxButtons.OK) End Sub
using System.Diagnostics; using Infragistics.Win; using Infragistics.Win.UltraWinSchedule; using System.IO; using System.Globalization; private void button1_Click(object sender, System.EventArgs e) { int yearNumber; int weekNumber; weekNumber = this.ultraCalendarInfo1.GetWeekNumberForDate( DateTime.Today, out yearNumber ); string info = "The number of the current week is " + weekNumber.ToString() + "." + "\n"; info += "The week falls in the year " + yearNumber.ToString() + "." + "\n"; // Display the information MessageBox.Show( info, "GetWeek", MessageBoxButtons.OK ); }
Target Platforms: Windows 10, Windows 8.1, Windows 8, Windows 7, Windows Server 2012, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2