在.NET中,知道了周数我怎样才能平日日期? [英] In .net, knowing the week number how can I get the weekdays date?

查看:228
本文介绍了在.NET中,知道了周数我怎样才能平日日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建在C#中的功能是一个星期编号将返回我的日子,在那一周。

I want to create a function in C# which for a week number will return me the days, in that week.

例如,对于本周40号,我怎么能得到天: 4/10,5/10,6/10,7/10,8/10,9/10,10/10。

For instance for week number 40, how can I get the days: 4/10, 5/10, 6/10, 7/10, 8/10, 9/10, 10/10.

感谢你在前进!

推荐答案

我想这应该做你想要的:

I think this should do what you want:

    public static DateTime[] WeekDays(int Year, int WeekNumber)
    {
        DateTime start = new DateTime(Year, 1, 1).AddDays(7 * WeekNumber);
        start = start.AddDays(-((int)start.DayOfWeek));
        return Enumerable.Range(0, 7).Select(num => start.AddDays(num)).ToArray();
    }

虽然我把星期日作为一周的第一天,如果你想周一第一天的变化范围为(0,7)到(1,7)。

Although I treat Sunday as first day of week, if you want Monday as first day change range from (0,7) to (1,7).

如果要符合ISO标准,我觉得这应该工作:

If you want to conform the ISO standard, I think this should work:

    public static DateTime[] WeekDays(int Year, int WeekNumber)
    {
        DateTime start = new DateTime(Year, 1, 4);
        start = start.AddDays(-((int)start.DayOfWeek));
        start = start.AddDays(7 * (WeekNumber - 1));
        return Enumerable.Range(0, 7).Select(num => start.AddDays(num)).ToArray();
    }

这篇关于在.NET中,知道了周数我怎样才能平日日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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