简体   繁体   English

对于某些确切的日期,发生了“字符串未被识别为有效的DateTime。”错误

[英]"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: 引发异常的确切日期是:

  1. 22/07/(any year) 22/07 /(任何一年)
  2. 22/09/(any year) 22/09 /(任何一年)
  3. 19/05/(any year) 19/05 /(任何一年)
  4. 20/05/(any year) 20/05 /(任何一年)
  5. 21/05/(any year) 21/05 /(任何一年)

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.

相关问题 错误消息为“字符串未被识别为有效的DateTime。” - Error Message as “String was not recognized as a valid DateTime.” 错误:字符串未被识别为有效的DateTime。 - Error: String was not recognized as a valid DateTime. 获取错误“字符串未被识别为有效的DateTime”。 - Getting error “String was not recognized as a valid DateTime.” 字符串未被识别为有效的日期时间。 即使使用 parse Exact - string was not recognized as a valid DateTime. even if using parse Exact 如何在没有得到“字符串未被识别为有效的日期时间”的情况下转换日期。 - 错误? - How to convert dates without getting 'String was not recognized as a valid DateTime.'-error? 迄今为止的字符串解析错误“字符串未被识别为有效的DateTime。” - String to date parsing error “String was not recognized as a valid DateTime.” “字符串未被识别为有效的DateTime。”Window 7计算机环境中出现错误 - “String was not recognized as a valid DateTime.” Error occur in Window 7 computer environment 无法将字符串识别为有效的DateTime。 :仅服务器错误 - String was not recognized as a valid DateTime. : Error only in Server 错误“字符串未被识别为有效的日期时间。” 在 c# - error “String was not recognized as a valid DateTime.” in c# 字符串日期未被识别为有效的日期时间。 - String date was not recognized as a valid DateTime.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM