文化日历TwoDigitYearMax被.Net Date Parse忽略 [英] Culture Calendar TwoDigitYearMax ignored by .Net Date Parse

查看:157
本文介绍了文化日历TwoDigitYearMax被.Net Date Parse忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望以下内容将2030日期写入targetDate。它没有。它给出了1930年的日期。



  Dim  ci  As  CultureInfo = CurrentThread.CurrentCulture.Clone 
ci.Calendar.TwoDigitYearMax = 2099
DateTime.TryParse ( 12/8/30,ci,DateTimeStyles.AssumeLocal,targetDate)



但是,如果我将当前文化设置为克隆,那么我将得到预期的(并且必需的)2030日期。但前提是代码在控制台应用程序(Win7 Professional上的Framework 2或Framework 4.5)中运行。



  Dim  ci  As  CultureInfo = CurrentThread.CurrentCulture.Clone 
ci.Calendar.TwoDigitYearMax = 2099

CurrentThread.CurrentCulture = ci
DateTime.TryParse( 12/8/30,ci,DateTimeStyles.AssumeLocal,targetDate)



如果第二个片段是运行服务器在Web应用程序(Framework 2,Win7 Professional)中,targetDate是_always_ 20世纪(19xx)日期。这不是所希望的行为,似乎与文件相矛盾。



两个控制台应用程序。和Web应用程序。在相同的(开发)机器上运行。运行之间不会改变文化设置。



其他人看过这个吗?

你有没有解释发生了什么?

解决方案

您可能还想在CurrentThread的CurrentUICulture属性上设置TwoDigitYearMax。由于某种原因,有两个文化对象,一个用于后台线程,另一个用于UI线程。你似乎只是在后台线程1而不是UI上设置它。



它在控制台中工作,因为没有UI,所以它使用CurrentThread。 CurrentCulture。



编辑---



如果你想添加评论,请使用有问题或评论?解决方案下面的链接:)



[烟花笔记]

引用:
感谢。这很有效,曾尝试过单独设置UICulture,但修补似乎表明你需要将cultureinfo实例中的文化属性_and_ hand设置为TryParse调用。


I would expect the following to write a 2030 date into targetDate. It doesn't. It gives a 1930 date.

Dim ci As CultureInfo = CurrentThread.CurrentCulture.Clone
ci.Calendar.TwoDigitYearMax = 2099
DateTime.TryParse("12/8/30", ci, DateTimeStyles.AssumeLocal, targetDate)


However if I set current culture to the clone then I get the expected (and required) 2030 date. But only if the code is running in a console app (Framework 2 or Framework 4.5 on Win7 Professional).

Dim ci As CultureInfo = CurrentThread.CurrentCulture.Clone
ci.Calendar.TwoDigitYearMax = 2099

CurrentThread.CurrentCulture = ci
DateTime.TryParse("12/8/30", ci, DateTimeStyles.AssumeLocal, targetDate)


If the second fragment is run server side in a web app (Framework 2, Win7 Professional) then targetDate is _always_ a 20th century (19xx) date. This not desired behaviour and seems to
contradict the documentation.

Both the console apps. and the Web app. are run on the same (development) machine. Culture settings are not altered between runs.

Anyone else seen this?
Have you an explanation for what's going on?

解决方案

You probably also want to set the TwoDigitYearMax on the CurrentUICulture property of the CurrentThread. For some reason there are two culture objects, one for background threads, and one for UI threads. You only seem to be setting it on the background thread one and not the UI one.

It works in the console because there is no UI, so it uses CurrentThread.CurrentCulture.

EDIT---

If you want to add comments, please use the "Have a question or comment?" link below the solution :)

[cigwork note]

Quote:

Thanks. That works, had tried setting UICulture on its own, but tinkering seems to show that you need to set both culture properties _and_ hand in a cultureinfo instance to the TryParse call.


这篇关于文化日历TwoDigitYearMax被.Net Date Parse忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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