日期格式转换时出错

日期格式转换时出错

本文介绍了日期格式转换时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调试时收到错误,说明FormaException未处理。附加信息:String未被识别为有效的DateTime。



以下是我的代码:



string iString = df.cmbColumns.SelectedIndex.ToString();

DateTime oDate = DateTime.ParseExact(iString,{0:dd / MM / yyyy hh:mm:tt},null);

MessageBox.Show(oDate.ToString());

I'm getting an error while debugging stating "FormaException was unhandled". Additional information: String was not recognized as a valid DateTime.

Below is my code:

string iString = df.cmbColumns.SelectedIndex.ToString();
DateTime oDate = DateTime.ParseExact(iString, "{0:dd/MM/yyyy hh:mm:tt}", null);
MessageBox.Show(oDate.ToString());

推荐答案

DateTime oDate = DateTime.ParseExact(iString, "dd/MM/yyyy hh:mm:tt", null);



{0:dd / MM / yyyy hh:mm:tt}在String.Format中用来表示哪个参数如何格式化。


{0:dd/MM/yyyy hh:mm:tt} is used in String.Format to denote which parameter how to format.




这篇关于日期格式转换时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 12:47