vb.net 获取从 VB 中的给定日期开始计算的一年中的周数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12712300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the week number of the year counting from a given date in VB
提问by user1717576
In vb we can get the week number counting from January. ex Jan 1st is Week 1 and Feb 1st is Week 5 etc by .DatePart(DateInterval.WeekOfYear, Now)
在 vb 中,我们可以得到从一月开始计算的周数。前 1 月 1 日是第 1 周,2 月 1 日是第 5 周等,通过 .DatePart(DateInterval.WeekOfYear, Now)
I need to count the week number from a given date. ex: if set the base to July 1st then July 1st is the week 1 and based on that what would be the week number for Oct 3rd? How can I do this in VB?
我需要计算给定日期的周数。例如:如果将基数设置为 7 月 1 日,那么 7 月 1 日是第 1 周,基于此,10 月 3 日的周数是多少?我怎样才能在VB中做到这一点?
回答by Jodrell
You can use Calendar.GetWeekOfYear
.
您可以使用Calendar.GetWeekOfYear
.
Here's an example
这是一个例子
Dim dateNow = DateTime.Now
Dim dfi = DateTimeFormatInfo.CurrentInfo
Dim calendar = dfi.Calendar
' using Thursday because I can.
Dim weekOfyear = calendar.GetWeekOfYear(
dateNow, _
dfi.CalendarWeekRule, _
DayOfWeek.Thursday)
回答by Steven Doggart
Something like this should do the trick:
像这样的事情应该可以解决问题:
Private Function GetWeekNumber(ByVal startMonth As Integer, ByVal startDay As Integer, ByVal endDate As Date) As Integer
Dim span As TimeSpan = endDate - New Date(endDate.Year, startMonth, startDay)
Return (CInt(span.TotalDays) \ 7) + 1
End Function