Tag: 日期算术

WeekNumber计算.NET中的错误?

我有一个相当奇怪的问题。 我住在丹麦,这里2013年的第一周(第1周)开始于2012年12月31日,持续7天 – 通常几周:) 据.NET称,12月30日是第52周,第31周是第53周,1月1日是第1周。 第53周仅持续一天,第1周持续6天。 显然,这一定是错误的(一周不到7天),在丹麦语中肯定是错误的。 12月31日是第1周,而不是第53周。 以下代码说明了问题(CurrentCulture是“da-DK”) static void Main(string[] args) { //Here I get Monday DayOfWeek firstDayOfWeek = DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek; //Here I get FirstFourDayWeek CalendarWeekRule weekRule = DateTimeFormatInfo.CurrentInfo.CalendarWeekRule; DateTime date = new DateTime(2012,12,30); for (int i = 0; i <= 10; i++) { DateTime currentDate = date.AddDays(i); Console.WriteLine("Date: {0} WeekNumber: {1}", currentDate.ToShortDateString(), CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(currentDate, […]

如何根据生日计算年龄?

可能重复: 如何计算C#中某人的年龄? 我想编写一个ASP.NET辅助方法,它返回给定生日的人的年龄。 我试过这样的代码: public static string Age(this HtmlHelper helper, DateTime birthday) { return (DateTime.Now – birthday); //?? } 但它不起作用。 根据生日计算人的年龄的正确方法是什么?