如何在某些文化中获取一年中的第几周?
我想获取一年中的第几周(在当前文化中),所以我这样做:
var week = CultureInfo.CurrentCulture.Calendar
.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay,
DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
当当前文化为 fa-IR
(波斯语)时,日历为 GregorianCalendar
和 week
值是 24(在波斯语中我们是在第 13 周),
但如果我用 PersianCalendar 测试它,如下所示:
var calendar = new PersianCalendar();
var week = calendar
.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay,
DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
它给出了正确的答案 (13)。
我检查了文化的日历是波斯语:
var persianCultures = CultureInfo.GetCultures(CultureTypes.AllCultures)
.Where(x => x.Calendar is PersianCalendar).ToList();
但没有文化。
但我需要这种文化,因为我认为检查日历是否为公历然后使用波斯语是不好的,如果您有更好的想法或对此有所了解,请告诉我。
(据我所知,公历是旧的,如果我没有错的话,上面发生的事情(使用 Gregorian )是错误的。它应该与波斯历类似。)
I want to get the week of a year (in current culture), so I do:
var week = CultureInfo.CurrentCulture.Calendar
.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay,
DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
When current culture is fa-IR
(Persian) the calendar is GregorianCalendar
and week
value is 24 (in Persian we are in the 13th week)
but if I test it with PersianCalendar as below:
var calendar = new PersianCalendar();
var week = calendar
.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay,
DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
it gives a true answer (13).
I checked that culture's calendar is Persian:
var persianCultures = CultureInfo.GetCultures(CultureTypes.AllCultures)
.Where(x => x.Calendar is PersianCalendar).ToList();
but there is no culture.
but I need this culture because I think is not good to check if calendar is Gregorian then use Persian, if you have better idea or know anything about this please let me know it.
(As I know Gregorian calendar is old, and if I'm not wrong what happened above (with Gregorian ) is wrong. it should be similar to Persian calendar.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://www.codeproject.com/KB/dotnet/PersianCulture.aspx
总之,这是 .NET 框架的限制,但可以通过反射来解决此问题。
http://www.codeproject.com/KB/dotnet/PersianCulture.aspx
In summary, it's a limitation of the .NET framework, but a workaround is possible with reflection.