如何解决这行代码。 [英] How to solve this line of code.

查看:70
本文介绍了如何解决这行代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

date dateFromformat =dd / MM / yyyy;

DateTime dateFrom = DateTime.ParseExact(tb_DateFrom.Text,dateFromformat,CultureInfo.InvariantCulture);



//错误是这个 -

日历System.Globalization.GregorianCalendar中不支持字符串表示的DateTime。

解决方案

我尝试使用您描述的日期字符串代码:

  string  dateFromformat = < span class =code-string>  dd / MM / yyyy; 
DateTime dateFrom1 = DateTime.ParseExact( 21/12/2014,dateFromformat,CultureInfo .InvariantCulture);
DateTime dateFrom2 = DateTime.ParseExact( 2014年12月21日,dateFromformat,CultureInfo .InvariantCulture);

我在第一次转换时没有错误,但是当第二次执行时我得到格里高利历例外。



您是否错误输入字符串?



(顺便说一句:我使用TryParseExact而不是ParseExact,并向用户报告错误而不是依赖于异常,或者首选使用DateTimePicker控件而不是TextBox - 这样用户就无法输入错误的值而直接获得DateTime而不进行任何转换)


tring dateFromformat = "dd/MM/yyyy";
DateTime dateFrom = DateTime.ParseExact(tb_DateFrom.Text, dateFromformat, CultureInfo.InvariantCulture);

//error is this -
The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.

解决方案

I tried that code, with the date string you describe:

string dateFromformat = "dd/MM/yyyy";
DateTime dateFrom1 = DateTime.ParseExact("21/12/2014", dateFromformat, CultureInfo.InvariantCulture);
DateTime dateFrom2 = DateTime.ParseExact("12/21/2014", dateFromformat, CultureInfo.InvariantCulture);

I get no error with the first conversion, but I do get a "Gregorian Calendar" exception when the second executes.

Did you perhaps miskey the input string?

(BTW: I'd use TryParseExact, instead of ParseExact, and report an error to the user instead of relying on the exception, or by preference use a DateTimePicker control instead of a TextBox - that way the user can't enter a wrong value and you get the DateTime directly without any conversions)


这篇关于如何解决这行代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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