How to get time only from DateTime in wpf?

Maria 71 Reputation points
2022-12-19T23:41:09.78+00:00

I have this method which is meant to parse and get the time that the user types in the textBox in WPF.
It should then add a string like '12:05' as a value to one of my selected items.

   private void addInterviewTime(object sender, RoutedEventArgs e)  
           {  
               ShortlistedClient sc = dgr.SelectedItem as ShortlistedClient;  
     
     
               if (sc != null && DateTime.TryParseExact(textBox.Text, "HH: mm", out TimeOnly result))  
               {  
                   sc.DT = result;  
               }  
     
           }  

It gives the following errors:

   // for this line, if (sc != null && DateTime.TryParseExact(textBox.Text, "HH: mm", out TimeOnly result))  
   Error	CS1501	No overload for method 'TryParseExact' takes 3 arguments  
     
   // and for	 sc.DT = result;  
     
   Error	CS0029	Cannot implicitly convert type 'System.TimeOnly' to 'System.DateTime?'  

I have also tried

   if (sc != null && TimeOnly.TryParseExact(textBox.Text, "HH: mm", out TimeOnly result))  
               {  
                   sc.DT = result;  
               }  

and it still gives

   Error	CS0029	Cannot implicitly convert type 'System.TimeOnly' to 'System.DateTime?'  

Previously, I have set my DT property like this in my ShortlistedClient class:

   public class ShortlistedClient : Client, INotifyPropertyChanged  
       {  
           private DateTime? _dt;  
     
           public DateTime?  DT  
           {  
               get { return _dt; }  
               set { _dt = value; NotifyPropertyChanged(); }  
           }  
     
           public bool InterestedinVac { get; private set; }  
           
     
           public List<ShortlistedClient> clients { get; set; } = new List<ShortlistedClient>();  
           public ShortlistedClient(string n, string p, string e, string l, string wt, string q, string we, string dl, string cc) : base(n, p, e, l, wt, q, we, dl, cc)  
           {  
               DT = new DateTime();   
               InterestedinVac = true;   
           }  

how can I have time only and fix these errors?

Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,711 Reputation points Microsoft External Staff
    2022-12-20T06:36:31.567+00:00

    Hi, @Maria . Welcome Microsoft Q&A.
    According to the documentation DateTime.TryParseExact, there is no overload for DateTime.TryParseExact that takes 3 parameters.
    You could modify the code as follows.

     if( DateTime.TryParseExact(textBox.Text, "hh:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime result))  
    

    I made a sample code according to your description, you can try to refer to it.

    MainWindow.xaml:

       <StackPanel >  
            <TextBox x:Name="textBox" Height=" 50" Width="400" Background="AliceBlue" Text="{Binding Txt ,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged}"/>  
            <DataGrid  x:Name="dgr" ItemsSource="{Binding Datas}" SelectedItem="{Binding Selected}" AutoGenerateColumns="False">  
                <DataGrid.Columns >  
                    <DataGridTextColumn Header="InterestedinVac" Binding="{Binding Path=InterestedinVac}"/>  
                    <DataGridTextColumn Header="Date" Binding="{Binding Path=DT}"/>  
                </DataGrid.Columns>  
            </DataGrid>  
            <Button x:Name="btn" Click="addInterviewTime"  Height="50" Width="400" Content="click"/>  
        </StackPanel>  
    

    Codebedhind:
    272403-new-text-document-9.txt

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    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.

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.