如何使用C#将日期从DD / MM / YYYY更改为YYYY-MM-DD [英] How to change the date from DD/MM/YYYY to YYYY-MM-DD using C#

查看:2112
本文介绍了如何使用C#将日期从DD / MM / YYYY更改为YYYY-MM-DD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 DatePicker 选择日期。所以选择日期后,我应该通过 MessageBox 显示它。我可以显示它,但它是格式 DD / MM / YYYY

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.

YYYY-MM-DD 的格式显示它。所以请任何人可以帮助我做的需要。
现在我使用代码显示日期:

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);

因此,请帮助我使用 DatePicker

推荐答案

我使用代码

<toolkit:DatePicker Name="datepicker" BorderBrush="Transparent" BorderThickness="0" Value="" Background="Transparent" TabNavigation="Cycle" Template="{StaticResource DatePickerControlTemplate1}"/>

在CS档案中,我加入了

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);

最后我得到了所需的输出
和DatePicker的方式,我们必须安装WPtoolkit包

and Lastly I got the required output and By the way for DatePicker we have to install WPtoolkit Package

这篇关于如何使用C#将日期从DD / MM / YYYY更改为YYYY-MM-DD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆