问题
I am using DatePicker to select the Date. So after selecting the date, I should display it through a MessageBox. I can able to display it but it is in the format DD/MM/YYYY.
And I want to display it in the format YYYY-MM-DD. So please can anyone help me to do the needful.
Now I am displaying the date by using the code:
string date1 = datepicker.ValueString;
MessageBox.Show(date1);
But I can display the current date format and display it as required by using code:
string format = "yyyy-MM-dd";
string f = DateTime.Now.ToString(format);
So please help me to do the same by using DatePicker.
回答1:
Declare this in XAML page (before declaring add the reference file)
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
Add this inside ContentPanel
<toolkit:DatePicker Name="datePickerOne"/>
Add this in C# page
DateTime dte = new DateTime();
dte = (DateTime)datePickerOne.Value;
MessageBox.Show(dte.ToString("yyyy/MM/dd"));
Now check it.
回答2:
I did it by using code
<toolkit:DatePicker Name="datepicker" BorderBrush="Transparent" BorderThickness="0" Value="" Background="Transparent" TabNavigation="Cycle" Template="{StaticResource DatePickerControlTemplate1}"/>
and in CS file, I added
string format = "yyyy-MM-dd";
string date1 = datepicker.ValueString;
DateTime datevalue = DateTime.Parse(date1);
string d = datevalue.ToString(format);
MessageBox.Show(d);
and Lastly I got the required output and By the way for DatePicker we have to install WPtoolkit Package
来源:https://stackoverflow.com/questions/17922312/how-to-change-the-date-from-dd-mm-yyyy-to-yyyy-mm-dd-using-c-sharp