VB.NET DatetimePicker - 错误的周数 [英] VB.NET DatetimePicker - Wrong week number

查看:47
本文介绍了VB.NET DatetimePicker - 错误的周数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 vbnet 扩展日期时间选择器有问题.当元素传到新年(2016)时,左边显示的周数是错误的.

I have an issue with my vbnet extented datetime picker. When the element pass to new year (2016), the week number displayed on the left is wrong.

我有一个datetimepicker",它不是默认组件,它是在此处下载的:http://www.codeproject.com/Articles/17063/ExtendedDateTimePicker-control-with-week-numbers

I have a "datetimepicker" which is not the default component, it was downloaded here : http://www.codeproject.com/Articles/17063/ExtendedDateTimePicker-control-with-week-numbers

我不明白为什么日历从 53 到 2,而不是从 53 到 1.

I don't understand why the calendar pass from 53 to 2 and not 53 to 1.

也许你们中的一个人有同样的错误.感谢您抽出宝贵时间.

Maybe one of you has the same error. Thanks for your time.

推荐答案

我不明白为什么日历从 53 传递到 2 而不是 53 传递到 1

它几乎按预期工作.按照周数计算方式,2016 年的前 3 天算作 2016 年的第一周.

It is pretty much working as expected. The way it is counting weeks, those first 3 days of 2016 count as the first week of 2016.

请注意,该控件不执行任何与日历或显示相关的操作.它只是改变了 Windows 提供的日历窗口的显示样式.在 CP 页面上看到的代码就是全部,主要是设置一个样式标志来告诉 Windows 添加周数:

Note that the control doesnt do anything calendar or display related. It is simply changing the display style of the calendar window provided by Windows. The code seen on the CP page is all there is and mainly it just sets a style flag to tell Windows to add the week numbers:

style = style | MCS_WEEKNUMBERS;

它的 MSDN 条目表明:

The MSDN entry for it indicates:

第 1 周定义为至少包含四天的第一周.

Week 1 is defined as the first week that contains at least four days.

由于 1 月 1 日至 3 日不是 4 天,因此似乎存在错误、使用了不同的日历或 MSDN 已过期.

Since Jan 1-3 is not 4 days, it would seem that there is either an error, a different calendar being used or MSDN is out of date.

来自评论:

据我所知,日期格式"有什么问题.可能不是8601

不,不仅如此:ISO8601 是一个不同的日历,Windows 和 NET 都没有实现.维基百科注释:

No, it is more than that: ISO8601 is a different calendar which neither Windows nor NET implements. Wikipedia notes:

一年的第一周是包含该年第一个星期四的那一周(因此,始终包含 1 月 4 日).因此,在接近 1 月 1 日的某些日子里,ISO 周年编号与公历略有不同.

The first week of a year is the week that contains the first Thursday of the year (and, hence, always contains 4 January). ISO week year numbering therefore slightly deviates from the Gregorian for some days close to 1 January.

这是您在日历下拉菜单中看到的内容.

This is what you see in the calendar drop down.

替代方案

但 ISO8601 年的周数很容易计算:

But the ISO8601 Week Of Year is easy to calculate:

我对一个非常相似问题的回答中的 GetISOWeekOfYear() 代码开始.您可以使用它在标签或 DTP 旁边的其他内容中显示所选日期的 ISO8601 一年中的第几周.

Start with the code for GetISOWeekOfYear() from my answer to a very similar question. You can use that to display the ISO8601 week of year for the selected date in a label or something next to the DTP.

打印 2011 年到 2021 年的第一周和最后一周的数字:

Print the first and last week numbers for 2011 To 2021:

Dim cal As Calendar = CultureInfo.CurrentCulture.DateTimeFormat.Calendar
For n As Int32 = 2011 To 2017       '2021
    dt = New DateTime(n, 12, 21)
    Console.WriteLine(" ***** {0} *****", n)
    For j = 0 To 3
        Dim NetWk = cal.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, firstD)
        Console.WriteLine("Invariant Date: {0} ISO #:{1:00}  NET #:{2:00}",
                          dt.ToString("MM/dd/yyyy"), GetISOWeekOfYear(dt), NetWk)
        dt = dt.AddDays(7)
    Next
Next

2015/2016 部分的结果:

The result for 2015/2016 portion:

***** 2015 *****
不变日期:12/21/2015 ISO #:52 NET #:52
不变日期:12/28/2015 ISO #:53 NET #:53
不变日期:01/04/2016 ISO #:01 NET #:02
不变日期:01/11/2016 ISO #:02 NET #:03
***** 2016 *****
不变日期:12/21/2016 ISO #:51 NET #:52
不变日期:12/28/2016 ISO #:52 NET #:53
不变日期:01/04/2017 ISO #:01 NET #:01
不变日期:01/11/2017 ISO #:02 NET #:02

***** 2015 *****
Invariant Date: 12/21/2015 ISO #:52 NET #:52
Invariant Date: 12/28/2015 ISO #:53 NET #:53
Invariant Date: 01/04/2016 ISO #:01 NET #:02
Invariant Date: 01/11/2016 ISO #:02 NET #:03
***** 2016 *****
Invariant Date: 12/21/2016 ISO #:51 NET #:52
Invariant Date: 12/28/2016 ISO #:52 NET #:53
Invariant Date: 01/04/2017 ISO #:01 NET #:01
Invariant Date: 01/11/2017 ISO #:02 NET #:02

除非您愿意从头开始编写自己的控件或许可可以设置为不同日历的控件(并且有 ISO8601 的定义),否则这可能是您能做的最好的事情.

Unless you are willing to write your own control from scratch or license one which can be set to a different calendar (and has a definition for ISO8601), that may be the best you can do.

底线:周数没有错.它使用的日历与您期望/想要的不同.

The Bottomline: The Week number is not wrong. It using a different calendar than you expect/want.

参考文献:

这篇关于VB.NET DatetimePicker - 错误的周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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