- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的类(class)中有以下 Sub。我没有错误,但结果不符合 ISO-8601 标准
Private Sub calculateAllProperties(ByVal dt As Date)
Select Case dt.DayOfWeek
Case DayOfWeek.Monday
m_CurrentWeekStartDate = dt
Case DayOfWeek.Tuesday
m_CurrentWeekStartDate = DateAdd(DateInterval.Day, -1, dt)
Case DayOfWeek.Wednesday
m_CurrentWeekStartDate = DateAdd(DateInterval.Day, -2, dt)
Case DayOfWeek.Thursday
m_CurrentWeekStartDate = DateAdd(DateInterval.Day, -3, dt)
Case DayOfWeek.Friday
m_CurrentWeekStartDate = DateAdd(DateInterval.Day, -4, dt)
Case DayOfWeek.Saturday
m_CurrentWeekStartDate = DateAdd(DateInterval.Day, -5, dt)
Case DayOfWeek.Sunday
m_CurrentWeekStartDate = DateAdd(DateInterval.Day, -6, dt)
End Select
'Now we have our start point of m_CurrentWeekStartDate we can calculate all other properties.
m_CurrentWeekStartYear = DatePart(DateInterval.Year, m_CurrentWeekStartDate)
m_CurrentWeekNo = DatePart(DateInterval.WeekOfYear, m_CurrentWeekStartDate, Microsoft.VisualBasic.FirstDayOfWeek.Monday, FirstWeekOfYear.FirstFourDays)
m_CurrentWeekNoYear = CurrentWeekNo.ToString("D2") & "-" & CurrentWeekStartYear.ToString
m_CurrentYearWeekNo = CurrentWeekStartYear.ToString & "-" & CurrentWeekNo.ToString("D2")
m_PreviousWeekStartDate = DateAdd(DateInterval.Day, -7, m_CurrentWeekStartDate)
m_PreviousWeekStartYear = DatePart(DateInterval.Year, m_PreviousWeekStartDate)
m_PreviousWeekNo = DatePart(DateInterval.WeekOfYear, m_PreviousWeekStartDate, Microsoft.VisualBasic.FirstDayOfWeek.Monday, FirstWeekOfYear.FirstFourDays)
m_PreviousWeekNoYear = PreviousWeekNo.ToString("D2") & "-" & PreviousWeekStartYear.ToString
m_PreviousYearWeekNo = PreviousWeekStartYear.ToString & "-" & PreviousWeekNo.ToString("D2")
End Sub
给定日期 dt 从 m_CurrentWeekNoYear 返回的值的一些示例
然后,当我达到实际有 53 周的一年时,它就起作用了。
我哪里出了问题,有什么想法吗?
最佳答案
众所周知,使用 DateAdd 和 DatePart 在处理这类事情时很糟糕,但问题是 .NET 计算周数的方式存在(错误)不一致。
请参阅此页面了解更多信息 ISO 8601 Week of Year format in Microsoft .Net
“具体来说,ISO 8601 总是有 7 天的周。如果一年的第一个部分周不包含星期四,则将其算作上一年的最后一周。同样,如果一年的最后一周上一年不包含星期四,则将其[原文如此]视为下一年的第一周。GetWeekOfYear() 有第一个行为,但没有第二个行为”
这是我编写的一些代码,用于根据定义获取 ISO 8601 周数:
“根据 ISO-8601 标准的周数,从星期一开始的周。一年中的第一周是包含该年第一个星期四的周(=“第一个 4 天周”)。最高一年中的周数为 52 或 53。”
''' <summary>
''' Finds the ISO 8601 Week Number based on a given date
''' </summary>
''' <param name="dateValue"></param>
''' <returns></returns>
''' <remarks>ISO 8601 Specifies Monday as the First Day of the week and week one defined as the First Four Day Week</remarks>
Public Shared Function GetIso8601WeekOfYear(ByVal dateValue As DateTime) As Integer
Return GetWeekOfYear(dateValue, DayOfWeek.Sunday, CalendarWeekRule.FirstFourDayWeek)
End Function
它使用以下更通用的方法来根据 ISO 标准的具体情况查找一年中的第几周:
'Need a calendar - Culture's irrelevent since we specify start day of week
Private Shared cal As Calendar = CultureInfo.InvariantCulture.Calendar
''' <summary>
''' Returns the week number of the year based on the Last Day of the Week and the Week One Rule
''' </summary>
''' <param name="dateValue">The date to find the week number for</param>
''' <param name="lastDayOfWeek">The last day of the week</param>
''' <param name="rule">The Definition of Week One</param>
''' <returns>An integer specifying the week number in the year</returns>
''' <remarks></remarks>
Public Shared Function GetWeekOfYear(ByVal dateValue As DateTime, ByVal lastDayOfWeek As DayOfWeek, ByVal rule As CalendarWeekRule) As Integer
'There is a bug in the .NET framework where some dates at the end of the year return the incorrect week number so to find the correct value we need to cheat.
'Find the DayOfWeek that represents the first day based on the last day
Dim firstDayOfWeek As DayOfWeek = lastDayOfWeek.Increment
'Find the date of the last day of the week, this ensures that we get the correct week number value
dateValue = GetWeekendingDate(dateValue, lastDayOfWeek)
'Return the value of the week for the last day of the week
Return cal.GetWeekOfYear(dateValue, rule, firstDayOfWeek)
End Function
''' <summary>
''' Finds the week ending date for a specified date value
''' </summary>
''' <param name="dateValue">The date to find the week ending date for</param>
''' <param name="lastDayOfWeek">The last day of the week</param>
''' <returns>A date value that is the last day of the week that contains the specified dateValue</returns>
''' <remarks></remarks>
Public Shared Function GetWeekendingDate(ByVal dateValue As DateTime, ByVal lastDayOfWeek As DayOfWeek) As DateTime
'Find out how many days difference from the date we are testing to the end of the week
Dim dayOffset As Integer = lastDayOfWeek - cal.GetDayOfWeek(dateValue)
If dayOffset < 0 Then dayOffset += 7
'Add days to the test date so that it is the last day of the week
Return dateValue.AddDays(dayOffset)
End Function
此代码使用此扩展方法,可以快速查找一周中的第二天:
<Extension()>
Public Function Increment(ByVal aDay As DayOfWeek) As DayOfWeek
Select Case aDay
Case DayOfWeek.Sunday : Return DayOfWeek.Monday
Case DayOfWeek.Monday : Return DayOfWeek.Tuesday
Case DayOfWeek.Tuesday : Return DayOfWeek.Wednesday
Case DayOfWeek.Wednesday : Return DayOfWeek.Thursday
Case DayOfWeek.Thursday : Return DayOfWeek.Friday
Case DayOfWeek.Friday : Return DayOfWeek.Saturday
Case DayOfWeek.Saturday : Return DayOfWeek.Sunday
Case Else : Return Nothing
End Select
End Function
我用这个尝试了你的测试用例并得到了以下结果:
Debug.WriteLine(GetIso8601WeekOfYear(Date.Parse("20/07/2015"))) '30
Debug.WriteLine(GetIso8601WeekOfYear(Date.Parse("09/03/2015"))) '11
Debug.WriteLine(GetIso8601WeekOfYear(Date.Parse("29/12/2014"))) '1
Debug.WriteLine(GetIso8601WeekOfYear(Date.Parse("05/01/2015"))) '2
Debug.WriteLine(GetIso8601WeekOfYear(Date.Parse("30/12/2013"))) '1
Debug.WriteLine(GetIso8601WeekOfYear(Date.Parse("06/01/2014"))) '2
关于.net - 使用 DatePart() 的 ISO8601 周数不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31560905/
我想知道是否可以通过 ISO 8583 做一个完整的声明(在一个日期范围之间),我看到 ATM 可以做完整的声明并且想知道他们使用什么方法。我知道余额查询和迷你报表可以在超过 8583 的 POS 设
我一直在尝试通过 7zip 和 WinRar 提取 ISO 镜像。 这是我使用的命令行: 7z x -y "%Isocontents%\ISO.iso" -o%Newfolder% WinRar.ex
我正在将 ICU 集成到一些内部软件中。我希望能够获取诸如“en_US”之类的字符串并为其获取脚本名称“Latin”。 (虽然最终我实际上想要一个 ICU ScriptCode。) 我尝试使用 ICU
我的数据位于一个数据框中,其结构如下: df2 <- data.frame(Year = c("2007"), Week = c(1:12), Measurement = c(rnorm(12, me
我需要计算一周分配到的年份。例如,2003 年 12 月 29 日被指定为 2004 年的第一周(我认为这仅适用于欧洲)。您可以使用以下代码来查看: SELECT DATEPART(isowk, '2
我想做什么? 我想提取 ISO 文件并编辑其中的文件,然后将其重新打包回 ISO 文件。 (正如你已经读过的) 我为什么要这样做? 我想开始修改 PSP ISO,为此我必须使用游戏资源、 Assets
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 2年前关闭。 Improve this questi
我有一个文件。在 Python 中,我想占用它的创建时间,并将其转换为 ISO time (ISO 8601) string 同时保留它是在 Eastern Time Zone 中创建的事实。 (美国
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
我在二维网格上有两个标量场。我想为第一个标量场绘制一条等值线,并根据第二个标量场设置其不透明度。 我从 this SO answer 开始尝试使用 mayavi但我无法从 3D 切换到 2D。 下面是
假设您声明两个同构 showing :: (Read a, Show a) => Iso' String a showing = iso read show reading :: (Read a, S
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines准则。它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,
假设有一个两个字符的String,它应该代表ISO 639国家或语言名称。 你知道,Locale类有两个函数 getISOLanguages和 getISOCountries分别返回包含所有 ISO
实体中的值是否可以自由嵌入到 ISO 10303-21 格式的文件中? 在我看到的所有由 CAD 程序生成的示例中,length_measure 都嵌入在uncertainty_measure_wit
是否可以从原始 ISO 自动创建用于操作系统安装的 Vagrant .box 文件? 对我来说,Vagrant 提供的虚拟机上操作系统安装和配置的端到端自动化存在巨大差距。 当然,VagrantBox
有没有办法实现这样的格式: 2003-09-25T14:00:00.000+1000 or 2003-09-25T14:00:00.000+1100 要这样转换 2003-09-25T14:00:00
我正在使用: @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @JsonFormat( pattern = "MM-dd-yyyy" ) private
我需要将 iso-3166-1/iso-3166-2 代码转换为经度/纬度 例子: 输入:“美国”,输出:(37.09024, -95.71289100000001)。 输入“VE-O”,输出:(10
我想转换 POSIX 语言环境名称,例如 en_US、de_DEISO-639-1 和 ISO-3166 代码到 Windows English_United States、German_German
我在 MS Access 上有一个数据库,我通过调用 PDO 和 odbc 驱动程序将其与 PHP 一起使用。我的数据库中有法语、丹麦语和波兰语单词。法语和丹麦语没问题,但波兰语字符没办法,我只得到“
我是一名优秀的程序员,十分优秀!