本帖最后由 百川 于 2014-3-13 12:28 编辑
- Console.WriteLine("请输入时间,格式为xxxxaabbccddee");
- string str=Console.ReadLine();
- try
- {
- string year = str.Substring(0, 4);
- string month = str.Substring(4, 2);
- string day = str.Substring(6, 2);
- string hour = str.Substring(8, 2);
- string minute = str.Substring(10, 2);
- string second = str.Substring(12, 2);
- string time = (year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
- DateTime datetime = DateTime.Parse(time);
- Console.WriteLine(datetime);
- }
- catch
- {
- Console.WriteLine("输入日期格式有误。");
- }
- Console.ReadKey();
复制代码 其实没必要这么麻烦,你让用户直接输入“XXXX-aa-bb cc:dd:ee”的格式是可以直接通过DateTime datetime = DateTime.Parse(time);转换的 |