如何从C#中的年份和周数获取月份号? [英] How do I get the month number from the year and week number in c#?

查看:128
本文介绍了如何从C#中的年份和周数获取月份号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所示,给定年份和周数,如何获取月份号?

As the title says, given the year and the week number, how do I get the month number?

编辑:如果一个星期过了两个月,我想这个星期的第一天是。

edit: if a week crosses two months, I want the month the first day of the week is in.

编辑(2):这是我如何得到周数:

edit(2): This is how I get the week number:

CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday);

我只是想反过来​​。

推荐答案

这是我最终做的:

static int GetMonth(int Year, int Week)
{
    DateTime tDt = new DateTime(Year, 1, 1);

    tDt.AddDays((Week - 1) * 7);

    for (int i = 0; i <= 365; ++i)
    {
        int tWeek = CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
            tDt, 
            CalendarWeekRule.FirstDay, 
            DayOfWeek.Monday);
        if (tWeek == Week)
            return tDt.Month;

        tDt = tDt.AddDays(1);
    }
    return 0;
}

我会喜欢更简单的东西,但它的作品:)

I would have preferred something simpler, but it works :)

这篇关于如何从C#中的年份和周数获取月份号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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