1、新建一个C#窗体应用程序项目,用来做测试用。
2、双击控件form1,输入代码:string d1 = "2018-1-1";//需要转换的年月日DateTime d2 = DateTime.Parse(d1);//转换后的年月日MessageBox.Show(d2.ToString());运行测试,效果如图。
3、使用不同的格式转换都可以:// string d1 = "2018-1-1"挢旗扦渌;//需要转换的年月日string d1 = "2018年1月1日";DateTime d2 = DateTime.Parse(d1);//转换后的年月日MessageBox.Show(d2.ToString());效果一样。