troubleshooting Question

How to format DateTime Field in C# WPF application as yyyy-MM-dd?

Avatar of Jim Riddles
Jim RiddlesFlag for United States of America asked on
10 Comments2 Solutions69 ViewsLast Modified:
I am building a C# app to generate codes.  To that end I have created a XAML page to collect the information about the codes to be created.  This page contains two DatePicker fields to indicate when the begin and end dates as to when the code is valid.  However, I need to write this data into a MySQL database, so I need the date to be formatted as yyyy-MM-dd (i.e. 2020-03-10).  After much searching online, I am unable to figure this out.

Here is the code that I have thus far:
<Page x:Class="WSP_USA_Admin_Dashboard.Pages.GiftCodeGenerator"
       Height="Auto" Width="600"
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <ColumnDefinition Width="20*"/>
            <ColumnDefinition Width="25*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="20*"/>
            <ColumnDefinition Width="25*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
        <TextBlock Grid.Row="0" Grid.ColumnSpan="5" TextWrapping="Wrap" Padding="10">
            Use the below form to generate new gift codes for use on the WSP USA Gift Store.  The Begin and End dates are optional and indicate the dates that the code is valid.  If you leave the begin date empty, it will be valid immediately.  If you leave the end date empty, it will never expire.
        <Label Grid.Column="0" Grid.Row="1" Content="Number of codes*" VerticalAlignment="Center" Margin="5" />
        <TextBox x:Name="CodeQty" Grid.Column="1" Grid.Row="1" Height="25" Margin="5" Width="100" VerticalContentAlignment="Center" />
        <Label Grid.Column="0" Grid.Row="2" Content="PO Number*" VerticalAlignment="Center" Margin="5" />
        <TextBox x:Name="PONumber" Grid.Column="1" Grid.Row="2" Height="25" Margin="5" Width="100" VerticalContentAlignment="Center" />
        <Rectangle Grid.Column="2" Grid.Row="1" Grid.RowSpan="2" VerticalAlignment="Stretch" Fill="LightSlateGray" Width="1" />
        <Label Grid.Column="3" Grid.Row="1" Content="Begin Date" VerticalAlignment="Center" Margin="5" />
        <DatePicker x:Name="BeginDate" Grid.Column="4" Grid.Row="1" Margin="5" Width="100" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" />
        <Label Grid.Column="3" Grid.Row="2" Content="End Date" VerticalAlignment="Center" Margin="5" />
        <DatePicker x:Name="EndDate" Grid.Column="4" Grid.Row="2" Margin="5" Width="100" Height="25" HorizontalAlignment="Left" VerticalAlignment="Center" />
        <Button x:Name="GenerateCodes" Content="Generate Gift Codes" Grid.ColumnSpan="5" Grid.Row="3" Height="Auto" HorizontalAlignment="Center" Padding="5" Background="DarkRed" Foreground="White" Width="Auto" VerticalAlignment="Center" VerticalContentAlignment="Center" Click="GenerateCodes_Click" />

    private void GenerateCodes_Click(object sender, RoutedEventArgs e)
      if (CodeQty.Text == "" || PONumber.Text == "")
        MessageBox.Show("The quantity and PO Number fields are required!");
        int numCodes = Convert.ToInt32(CodeQty.Text);
        string poNumber = PONumber.Text;
        string ValidFrom = BeginDate.Text;
        string ValidTo = EndDate.Text;
        MessageBox.Show("The values entered include:\n# codes: " + numCodes.ToString() + "\nPO #: " + poNumber + "\nValid From: " + ValidFrom + "\nValid To: " + ValidTo);

Currently, the date looks like "03/10/2020".  I have tried the following example I found online, but got an error about no method of "Value".
var datestring = datePicker.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
Systems architect
Join our community to see this answer!
Unlock 2 Answers and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros