Skip to main content
 首页 » 编程设计

c#之Calendar Extender 日期选择并转换为正确的格式

2024年02月13日12shanyou

我正在从文本框 中的日历 扩展程序中选取值,并且我正在获取格式为{"MM/dd/yyyy"} 的值但我想要它的格式 {"dd/MM/yyyy"}在另一个文本框 ( txt_actualrightformat.Text ) 如下所示的代码

DateTime wrongformat = DateTime.Parse(TextBox4.Text); 
String rightformat = String.Format("{0:dd/MM/yyyy}", wrongformat.Date); 
txt_actualrightformat.Text = rightformat.ToString(); 

请您参考如下方法:

DateTime 与格式无关,格式仅用于显示目的。如果您没有在 wrongformat 中获得正确的日期,那么您可以使用 DateTime.ParseExact 格式。然后简单地

txt_actualrightformat.Text = wrongformat.ToString("dd/MM/yyyy"); 

编辑:

像这样使用 DateTime.ParseExcat:

DateTime dt = DateTime.ParseExact(TextBox4.Text, "MM/dd/yyyy", CultureInfo.InvariantCulture); 
txt_actualrightformat = dt.ToString("dd/MM/yyyy");