PDA

View Full Version : سوال: مشکل در تبدیل رشته به تاریخ توسط DateTime.ParseExact



mamiz
یک شنبه 11 تیر 1391, 22:21 عصر
با سلام خدمت همه دوستان
کسی میتونه بگه مشکل این کد چیه؟ در همه حالتها درست کار میکنه غیر از تاریخهای 31/2 و 31/4 و 31/6



PersianCalendar pc = new PersianCalendar();
CultureInfo persianCulture = new CultureInfo("fa-IR");
DateTime dt = DateTime.ParseExact(txtPDate.Text, "yyyy/MM/dd", persianCulture);
txtGDate.Text = pc.ToDateTime(dt.Year, dt.Month, dt.Day, 12, 0, 0, 0).ToShortDateString();


تاریخ شمسی در تکست باکس txtPDate با فرمت 11/04/1391 وارد میشود.
همه رشته های تاریخ درست به تاریخ تبدیل میشن غیر از مواردی که عرض کردم.

با سپاس

mamiz
دوشنبه 12 تیر 1391, 10:20 صبح
جون من یکی جواب بده.
این خطا حسابی رو مخمه.
شاید بگید که چرا از روشهای دیگه مثل جداسازی بخشهای تاریخ با استفاده از توابع رشته ای استفاده نمیکنم. باید بگم که کاملا درست میگید اما بعضی موضوعات هستند که چون خیلی عجیب میشن آدمو تحریک میکنن که ببینه چرا اینطوریه؟ اینم یکی از هموناست که (3)362 روز سالو درست کار میکنه اما سه روزو باهاش مشکل داره.
لطفا اگر کسی تجربه ای داره بگه.

xsornax
شنبه 28 اردیبهشت 1392, 11:22 صبح
style را باید در این حالت قرار بدی DateTimeStyles.RoundtripKind
مثلا
CultureInfo persianCulture = new CultureInfo("fa-IR");
DateTime.ParseExact(ShamsiDateFormat, "yyyy/mm/dd", persianCulture, DateTimeStyles.RoundtripKind)


مقدار های style به شرح ذیل می باشد



Member name Description
None Default formatting options must be used. This value represents the default style for the DateTime.Parse, DateTime.ParseExact, and DateTime.TryParse methods.
AllowLeadingWhite Leading white-space characters must be ignored during parsing, except if they occur in the DateTimeFormatInfo format patterns.
AllowTrailingWhite Trailing white-space characters must be ignored during parsing, except if they occur in the DateTimeFormatInfo format patterns.
AllowInnerWhite Extra white-space characters in the middle of the string must be ignored during parsing, except if they occur in the DateTimeFormatInfo format patterns.
AllowWhiteSpaces Extra white-space characters anywhere in the string must be ignored during parsing, except if they occur in the DateTimeFormatInfo format patterns. This value is a combination of the AllowLeadingWhite, AllowTrailingWhite, and AllowInnerWhite values.
NoCurrentDateDefault If the parsed string contains only the time and not the date, the parsing methods assume the Gregorian date with year = 1, month = 1, and day = 1. If this value is not used, the current date is assumed.
AdjustToUniversal Date and time are returned as a Coordinated Universal Time (UTC). If the input string denotes a local time, through a time zone specifier or AssumeLocal, the date and time are converted from the local time to UTC. If the input string denotes a UTC time, through a time zone specifier or AssumeUniversal, no conversion occurs. If the input string does not denote a local or UTC time, no conversion occurs and the resulting Kind property is Unspecified.
This value cannot be used with RoundtripKind.
AssumeLocal If no time zone is specified in the parsed string, the string is assumed to denote a local time.
This value cannot be used with AssumeUniversal or RoundtripKind.
AssumeUniversal If no time zone is specified in the parsed string, the string is assumed to denote a UTC.
This value cannot be used with AssumeLocal or RoundtripKind.

مشکل شما در این گزینه می باشد


RoundtripKind The DateTimeKind field of a date is preserved when a DateTime object is converted to a string using the "o" or "r" standard format specifier, and the string is then converted back to a DateTime object.