[英]"String was not recognized as a valid DateTime.” error occurs for some exact dates
I have a problem converting Gregorian calendar dates to Persian(Hijri calendar) using default system "en-GB" culture in my ASP.NET MVC Application. 我在ASP.NET MVC应用程序中使用默认系统“ en-GB”文化将公历日期转换为波斯(回历)时遇到问题。 I used the globalization tag in my web.config so the default culture would be "en-GB":
我在web.config中使用了全球化标记,因此默认区域性为“ en-GB”:
<system.web>
...
<globalization uiCulture="en-GB" culture="en-GB" />
...
</system.web>
The conversion will happen for any date except for these exact dates that I listed them below and will throw "String was not recognized as a valid DateTime.” error. 转换将在任何日期发生,但我在下面列出的确切日期除外,并且将引发“字符串未被识别为有效的DateTime。”错误。
The exact dates that will throw exception are: 引发异常的确切日期是:
A part of my sample code: 我的示例代码的一部分:
using System.Globalization;
...
...
public static PersianCalendar PC = new PersianCalendar();
...
//I'm using this method for conversion
public static DateTime GregorianToPersian(DateTime date)
{
...
string stringDate = string.Format("{0}/{1}/{2} {3}:{4}:{5}"
, PC.GetDayOfMonth(date), PC.GetMonth(date), PC.GetYear(date)
, PC.GetHour(date), PC.GetMinute(date), PC.GetSecond(date));
return DateTime.Parse(stringDate); //error!
}
Hope you guys can help me out with. 希望你们能帮助我。 Thanks.
谢谢。
Leaving the parsing error aside, this isn't going to achieve what you want to achieve. 抛开解析错误,这将无法实现您想要实现的目标。 A
DateTime
value is always in the Gregorian calendar - trying to parse 31/04/1396 as a DateTime
is never going to work because it's not a valid date in the Gregorian calendar. DateTime
值始终位于公历中-尝试解析1396年1月31日,因为DateTime
永远不会起作用,因为它不是公历中的有效日期。
I would advise using my Noda Time library for this instead, where appropriate types "know" which calendar system they're part of, and you can explicitly convert between them. 我建议改用我的Noda Time库,在适当的类型处“知道”它们属于哪个日历系统,并且您可以在它们之间进行显式转换。 For example:
例如:
// The Gregorian calendar is used implicitly
LocalDate gregorian = new LocalDate(2017, 7, 22);
LocalDate persian = gregorian.WithCalendar(CalendarSystem.PersianArithmetic);
Console.WriteLine($"{persian:yyyy-MM-dd}");
Output: 输出:
1396-04-31
You can then do sensible arithmetic with persian
such as adding a month etc, which would give the wrong answer if you tried doing it with DateTime
naively. 然后,您可以使用
persian
进行明智的算术,例如添加一个月等,如果您天真地尝试使用DateTime
这样做会给出错误的答案。 (You could use Calendar.AddMonths
etc, but you'd need to remember to do so everywhere.) (您可以使用
Calendar.AddMonths
等,但您需要记住在各处使用它。)
DateTime
object is in Gregorian calendar (from MSDN DateTime specification ) : DateTime
对象在公历中(根据MSDN DateTime规范 ):
The DateTime value type represents dates and times with values ranging from 00:00:00 (midnight), January 1, 0001 Anno Domini (Common Era) through 11:59:59 PM, December 31, 9999 AD (CE) in the Gregorian calendar.
DateTime值类型表示日期和时间,其值从公历9999年12月31日(公元)0001 Anno Domini(公共时代)00:00:00(午夜)到公元9999年12月31日晚上11:59:59。日历。
Your solution is valid until target days are in range of target month in Gregorian Calendar . 您的解决方案有效,直到目标日期在公历中的目标月份范围内。 (Un)fortunely
DateTime
struct is sealed which mean, you can't override it. (Un)幸运的是
DateTime
结构是密封的,这意味着您无法覆盖它。
Because of this limits, I suggest make some kind of PersianDateTimeUtility
class, where would be logic of parsing object as DateTime
to specified type (for example String
), comparing dates etc. There are some frameworks for this purpose, but i haven't tested them so I will not describe them. 由于这个限制,我建议制作某种
PersianDateTimeUtility
类,其中将对象作为DateTime
解析为指定类型(例如String
),比较日期等的逻辑。为此目的有一些框架,但是我尚未测试他们,所以我不会描述它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.