How to change the date from DD/MM/YYYY to YYYY-MM-DD using C#

拈花ヽ惹草 提交于 2019-12-12 22:26:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!