DateTime d = new DateTime(年,12,31); //返回一年中最后一天的周数 返回d.Year * 100 + GregorianCalendar.GetWeekOfYear(d, DateTimeFormatInfo.Current.CalendarWeekRule,DayOfWeek.Monday); } 小修正 - GetWeekOfYear并非静态,并且DateTimeFormatInfo中没有当前 属性: private static int GetLastWeek(int year) { //获取一年中的最后一天 DateTime d = new DateTime(year,12, 31); //返回一年中最后一天的周数 GregorianCalendar calendar = new GregorianCalendar(); return d.Year * 100 + calendar.GetWeekOfYear(d, DateTimeFormatInfo.CurrentInfo.Calen darWeekRule, DayOfWeek.Monday); } Jon 教我这么晚才发帖。谢谢。 :) Hi! I''m converting some methods of VB-class into C#-class for anotherproject. It''s quite easy, but when converting method which returns lastweek number of the entered year I got problems. The VB code is... Private Function GetLastWeek(ByVal year As Integer) As Integer''// Get last day of the yearDim dte As DateTime = New DateTime(year, 12, 31)''// Return last day weeknumber of the yearReturn dte.Year * 100 + DatePart(DateInterval.WeekOfYear, dte,FirstDayOfWeek.Monday, FirstWeekOfYear.System)End Function How to do this using C# because the following code below is not workingbecause "DatePart" is not available in C#? private int GetLastWeek(int year){// Get last day of the yearDateTime d = new DateTime(year, 12, 31);// Return last day weeknumber of the yearreturn d.Year * 100 + DatePart(DateInterval.WeekOfYear, d,FirstDayOfWeek.Monday, FirstWeekOfYear.System);} --Thanks in advance! Mika 解决方案 I think you are looking for the Calendar class. For example: private int GetLastWeek(int year){// Get last day of the yearDateTime d = new DateTime(year, 12, 31);// Return last day weeknumber of the yearreturn d.Year * 100 +GregorianCalendar.GetWeekOfYear(d, DateTimeFormatInfo.Current.CalendarWeekRule, DayOfWeek.Monday);} The above explicitly uses the Gregorian calendar, but you could also useCurrentCulture.Calendar if you wanted for some reason to handle alternate calendars. Pete I think you are looking for the Calendar class. For example: private int GetLastWeek(int year) { // Get last day of the year DateTime d = new DateTime(year, 12, 31); // Return last day weeknumber of the year return d.Year * 100 + GregorianCalendar.GetWeekOfYear(d,DateTimeFormatInfo.Current.CalendarWeekRule, DayOfWeek.Monday); }Small corrections - GetWeekOfYear isn''t static, and there''s no Currentproperty in DateTimeFormatInfo: private static int GetLastWeek(int year){// Get last day of the yearDateTime d = new DateTime(year, 12, 31);// Return last day weeknumber of the yearGregorianCalendar calendar = new GregorianCalendar();return d.Year * 100 +calendar.GetWeekOfYear(d,DateTimeFormatInfo.CurrentInfo.CalendarWeekRule,DayOfWeek.Monday);} Jon Teach me to post so late. Thanks. :) 这篇关于C#DatePart()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-29 17:51