Hi,@zleug. Welcome to Microsoft Q&A.
You could refer to the following code to achieve the function you want.
MainWindow.xaml
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<DatePicker x:Name="MyDatePicker" SelectedDateChanged="MyDatePicker_SelectedDateChanged"></DatePicker>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<TextBox x:Name="txbWeek" Margin="10" Width="100" Height="50"></TextBox>
<TextBox x:Name="txbMonDate" Margin="10" Width="100" Height="50"></TextBox>
<TextBox x:Name="txbFriDate" Margin="10" Width="100" Height="50"></TextBox>
</StackPanel>
</Grid>
MainWindow.xaml.vb
Class MainWindow
Private Sub MyDatePicker_SelectedDateChanged(sender As Object, e As SelectionChangedEventArgs)
Dim value As String = MyDatePicker.Text
If Not String.IsNullOrEmpty(value.ToString()) Then
'The time value obtained by DatePicker Is converted from string type to DateTime
Dim mydate = value.ToString().Split("/")
Dim dateValue As New DateTime(mydate(2), mydate(0), mydate(1))
'Get Week
txbWeek.Text = GetWeekOfMonth(dateValue)
'Get the week number Of the current day
Dim Week = dateValue.DayOfWeek
'Sunday 's enumeration is 0, set it to 7 to facilitate subsequent calculation of time difference
If Week = 0 Then
Week = 7
End If
'Get Monday
Dim addDate = DayOfWeek.Monday - Week
txbMonDate.Text = dateValue.AddDays(addDate)
'Get Friday
addDate = DayOfWeek.Friday - Week
txbFriDate.Text = dateValue.AddDays(addDate)
Else
txbWeek.Text = ""
txbMonDate.Text = ""
txbFriDate.Text = ""
End If
End Sub
Function GetWeekOfMonth(dateValue As DateTime) As Integer
' Get the first day of the month
Dim firstDayOfMonth As New DateTime(dateValue.Year, dateValue.Month, 1)
'Get the week number for this date and the first day of the month
Dim firstWeekOfYear As Integer = DatePart(DateInterval.WeekOfYear, firstDayOfMonth, FirstDayOfWeek.System)
Dim currentWeekOfYear As Integer = DatePart(DateInterval.WeekOfYear, dateValue, FirstDayOfWeek.System)
' Calculate the week number of the month that the date falls in
Return currentWeekOfYear - firstWeekOfYear + 1
End Function
End Class
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.