从周数计算月份

发布于 2024-11-06 20:57:13 字数 153 浏览 1 评论 0原文

我从数据库中获取周号,需要计算它是该月的哪一周。

例如:

week no = 7 month = 2

week no 11 month = 3

如何计算?

只是补充一下,我还有年份值和周号。

i am getting week no from database need to calculate which week of the month it is.

for ex:

week no = 7 month = 2

week no 11 month = 3

How to calculate it??

Just to add on i also have the year value with the week no.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

鲜血染红嫁衣 2024-11-13 20:57:13

好吧,我会尝试一下,但它不是很漂亮:

public int MonthOfWeek( int week, int year, CalendarWeekRule, weekrule, DayOfWeek firstDayOfWeek)
{
  GregorianCalendar gc = new GregorianCalendar();
  for( DateTime dt = new DateTime(year, 1, 1); dt.Year == year; dt = dt.AddDays(1))
  {
    if( gc.GetWeekOfYear( dt, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) == week ){
      return dt.Month;
    }
  }
  return -1;
}  

这考虑到了生成周的文化特定规则。如果一周分为两个月,它将返回它遇到的第一个月。

因此,在挪威,我会使用 CalendarWeekRule.FirstFourDayWeekDayOfWeek.Monday 来调用它以获得正确的结果。

Ok, I'll have a go, but it's not very pretty:

public int MonthOfWeek( int week, int year, CalendarWeekRule, weekrule, DayOfWeek firstDayOfWeek)
{
  GregorianCalendar gc = new GregorianCalendar();
  for( DateTime dt = new DateTime(year, 1, 1); dt.Year == year; dt = dt.AddDays(1))
  {
    if( gc.GetWeekOfYear( dt, System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday) == week ){
      return dt.Month;
    }
  }
  return -1;
}  

This takes into consideration the culture specific rules for generating weeks. If a week is split between two months it will return the first month it encounters.

So in Norway, I would call this with CalendarWeekRule.FirstFourDayWeek and DayOfWeek.Monday for correct results.

懷念過去 2024-11-13 20:57:13

根据周数计算日期:

根据周数计算日期

获取月份的周数:

计算 .NET 中的星期

Calculate a date from weeknumber:

Calculate date from week number

Get the WeekOfMonth:

Calculate week of month in .NET

心舞飞扬 2024-11-13 20:57:13

假设您正在查找给定年份中的月份:

var dtYearStart = new DateTime(DateTime.Now.Year, 1, 1);
CultureInfo defaultCultureInfo = CultureInfo.CurrentCulture;

// Check for the starting date of the initial week.
int diff = dtYearStart.DayOfWeek - defaultCultureInfo.DateTimeFormat.FirstDayOfWeek;
if (diff < 0)
{
 diff += 7;
}

dtYearStart = dtYearStart.AddDays(-1 * diff).Date;

int weekNo = 15;

DateTime dtWeekBased = dtYearStart.AddDays((weekNo-1) * 7);
string strMonth  = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dtWeekBased.Month);

Assuming that you are looking for the month in the given year:

var dtYearStart = new DateTime(DateTime.Now.Year, 1, 1);
CultureInfo defaultCultureInfo = CultureInfo.CurrentCulture;

// Check for the starting date of the initial week.
int diff = dtYearStart.DayOfWeek - defaultCultureInfo.DateTimeFormat.FirstDayOfWeek;
if (diff < 0)
{
 diff += 7;
}

dtYearStart = dtYearStart.AddDays(-1 * diff).Date;

int weekNo = 15;

DateTime dtWeekBased = dtYearStart.AddDays((weekNo-1) * 7);
string strMonth  = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dtWeekBased.Month);
数理化全能战士 2024-11-13 20:57:13

使用 DateTime,您可以将周数乘以 7,获取一年中过去的总天数,将这些天数添加到 31/12/yyyy-1,然后从结果 DateTime 中获取月份。

我希望这对你有用。

再见。

Using DateTime, you can multiply week number by 7, get the total days passed on the year, add those days to 31/12/yyyy-1 and the get the month from the resulting DateTime.

I hope this to be useful to you.

See you.

祁梦 2024-11-13 20:57:13

4 周 = 1 个月
所以使用

week%4

它会返回你的月份号

4 week = 1 month
so use

week%4

its return you no of month

一身软味 2024-11-13 20:57:13

这是使用 .NET 时间段库 的解决方案:

// ----------------------------------------------------------------------
public int GetMonthOfWeek( int weekOfYear )
{
  return new Week( weekOfYear ).FirstDayStart.Month;
} // GetMonthOfWeek

Here a solution using the Time Period Library for .NET:

// ----------------------------------------------------------------------
public int GetMonthOfWeek( int weekOfYear )
{
  return new Week( weekOfYear ).FirstDayStart.Month;
} // GetMonthOfWeek
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文