简体   繁体   English

获取日期的星期数,其中52作为最高星期数

[英]Get the weeknumber for a date with 52 as the highest weeknumber

I need to get the weeknumber of a date. 我需要获取日期的星期数。

I know this is asked many times before but all the answers here are not usefull for me since they return the dotnet weeknumber or the iso 8601 weeknumber. 我知道这已经被问过很多次了,但是这里的所有答案对我来说都没有用,因为它们返回dotnet weeknumber或iso 8601 weeknumber。

What I need is a bit more challenging. 我需要的是更具挑战性的。
Let me explain with sample data that will be the easiest : 让我用最简单的示例数据进行说明:

input date    desired weeknumber
01-jan-2012          1
02-jan-2012          2
31-dec-2012         52
01-jan-2013          1
06-jan-2013          1
07-jan-2013          2
31-dec-2013         52
01-jan-2014          1
05-jan-2014          1
06-jan-2014          2

I hope this makes clear what I need. 我希望这可以弄清楚我的需要。
Its like if you would write weeknumbers on a printed calendar but with the first/Last week splitted to 1/52 in stead of 53 就像您要在打印的日历上写星期数,但第一周/最后一周被拆分为1/52而不是53

Searching in google returns lots of answers like this one example of answer , but none of them I found returns these result so I am hoping someone can help me with this or point me in the right direction. 在google中搜索会返回很多答案,例如这一答案示例 ,但我发现没有一个返回这些结果,因此我希望有人可以帮助我解决这个问题或为我指明正确的方向。

EDIT : another way to explain it is this : 编辑:另一种解释方式是:

look at it as you would say to another person, the last day of the year a person would say it is week 52 and the first day of the year a person would say week 1. A person would never say it is week 53. 就像您对另一个人说的那样看,一个人在一年的最后一天说是第52周,一个人在一年的第一天说是第1周。一个人永远不会说是第53周。
For 02-jan-2012 a person would say we are in week 2 of this year, not in week 1 对于2012年1月2日,有人会说我们在今年的第二周,而不是在第一周

EDIT : I finally convinced my employer that this is not practical and even impossible. 编辑:我终于说服我的雇主,这是不切实际的,甚至是不可能的。 The answers and comments in this thread is what I used for the convincing so you did all help me well afterall 这是我在说服力中使用的答案和评论,因此您毕竟对我有帮助

I'm not sure if I understand your post correctly but maybe this could be a solution: 我不确定我是否正确理解您的帖子,但这也许可以解决:

  var dates = new[]
  {
    new DateTime(2012, 1, 1),
    new DateTime(2012, 1, 2),
    new DateTime(2012, 12, 31),
    new DateTime(2013, 1, 1),
    new DateTime(2013, 1, 6),
    new DateTime(2013, 1, 7),
    new DateTime(2013, 12, 31),
    new DateTime(2014, 1, 1),
    new DateTime(2014, 1, 5),
    new DateTime(2014, 1, 6),
  };

  var calendar = CultureInfo.CurrentCulture.Calendar;

  foreach (var date in dates)
  {
    var day = calendar.GetDayOfYear(date);
    var week = calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
    week = week > 52 ? 52 : week;
    Console.WriteLine($"{date.Date}: {day,3}: {week,3}");
  }

have you tried any codes? 您是否尝试过任何密码? and what you say is not making sense , although weeks start on monday , a january 2nd on tuesday does not mean it's the years second week, week number in a year means that you want to know how many weeks have passed, meaning you have to devide the day number by 7. what i mean is that the week number is not dependant on the start of the weekdays , it depends on how many 7-days have passed. 而且您说的是没有道理的,尽管几周从星期一开始,1月2日在星期二并不意味着它是第二个星期,一年中的周数意味着您想知道已经过去了几周,这意味着您必须将天数除以7。我的意思是,周数不取决于工作日的开始,而是取决于过去了7天。 for this you have two ways to implement: 1st - first extract the day number from the date and then devide it to 7 to get the week number. 为此,您有两种实现方法:第一种-首先从日期中提取天数,然后将其指定为7以获取周数。 2nd - to create a switch case , and deviding the weeks by dates. 2-创建一个切换案例,并按日期指定星期。 although in single cases the 2nd way might prove faster, the 1st ways proves to be easier to make it into code, needless to say both are a little too long, if you don't understand what to do or how to do it comment so i put first few lines of each one's codes. 尽管在少数情况下,第二种方法可能会证明速度更快,但是第一种方法却证明更容易将其编写为代码,不用说,如果您不了解该怎么做或如何做,那么两者都可能会太长,因此请注释一下我把每个人的代码的前几行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM