年C#日期时间的周 [英] Week of Year C# Datetime

查看:133
本文介绍了年C#日期时间的周的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码获取的日期时间定在今年weeknumber对象时

I have following code to get the weeknumber of the year given in the DateTime object Time

    public static int WeeksInYear(DateTime date)
    {
        GregorianCalendar cal = new GregorianCalendar(GregorianCalendarTypes.Localized);
        return cal.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
    }

现在我给该函数的日期1/1/2012至极应该返回周1,但HES回到52周,我似乎无法找出原因。任何人都有一个想法,为什么?

Now i give the function the date 1/1/2012 wich should return week 1 , but hes returning week 52. And i can't seem to figure out why. Anyone have an idea why ?

推荐答案

该算法在做什么,你已指示做的事。你有CalanderWeekRule设置为FirstFourDayWeek。 2012年1月1日已经不是第四天工作制的一部分,所以你已指示压延,开始从1月2日计数

The algorithm is doing exactly what you have instructed it to do. You have the CalanderWeekRule set to FirstFourDayWeek. The 1st of January 2012 was not part of the first four day week, so you have instructed the calander to start counting from January 2nd.

Calculate日期

这篇关于年C#日期时间的周的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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