【问题标题】:C# Date format error in run time VS2005 Application运行时VS2005应用程序中的C#日期格式错误
【发布时间】:2016-07-17 23:56:49
【问题描述】:

我有一个日期时间选择器 dtOtMonth,我只需要显示月份和年份。所以我在表单加载时将其格式化如下。

dtOtMonth.Format = DateTimePickerFormat.Custom;
dtOtMonth.CustomFormat = "MM/yyyy";

但是当我尝试在运行时使用向上/向下箭头编辑日期时,会出现以下错误。

Year、Month 和 Day 参数描述了无法表示的 DateTime。

System.ArgumentOutOfRangeException 未处理 Message="Year、Month 和 Day 参数描述了无法表示的 DateTime。" 来源="mscorlib" 堆栈跟踪: 在 System.DateTime.DateToTicks(Int32 年,Int32 月,Int32 日) 在 System.Windows.Forms.DateTimePicker.SysTimeToDateTime(SYSTEMTIME 秒) 在 System.Windows.Forms.DateTimePicker.WmDateTimeChange(消息& m) 在 System.Windows.Forms.DateTimePicker.WmReflectCommand(消息和 m) 在 System.Windows.Forms.DateTimePicker.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) 在 System.Windows.Forms.Control.SendMessage(Int32 消息,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, 留言&m) 在 System.Windows.Forms.Control.WmNotify(消息& m) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.GroupBox.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) 在 System.Windows.Forms.NativeWindow.DefWndProc(消息和 m) 在 System.Windows.Forms.Control.DefWndProc(消息和 m) 在 System.Windows.Forms.Control.WmKeyChar(消息和 m) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.DateTimePicker.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 原因, Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.Run(窗体 mainForm) 在 E:\HR System\HRProject\Attendence_Module\Attendence_Module\Program.cs:line 中的 Attendence_Module.Program.Main() 17 在 System.AppDomain._nExecuteAssembly(程序集程序集,字符串 [] 参数) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,String [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart()

【问题讨论】:

  • 我打开了一个 winforms 项目 - 拖了一个 datetimepicker - 在 Form_Load 事件中复制了您的代码,一切正常。它可以是你的代码中的其他东西吗?
  • 看下面的stackoverflow.com/a/1608804/1071091可能对你有帮助
  • 哇发现了一些东西.. VS-2005???赶紧转2015!!!

标签: c# winforms


【解决方案1】:

正如 OP 发现的那样,当只有月份发生变化时会发生错误,可以更改为不存在的日期(例如“2 月 31 日”)。

有几种方法可以防止该错误。一种方法是确保DateTimePicker 始终使用该月的第一天。下面是一个快速示例,可确保 DateTimePicker 的值的 Day 属性始终设置为 1。在此示例中,我使用只读文本框来显示输出:

public MyTestForm()
{
    InitializeComponent();

    SetDateTimePickerFirstOfMonth();
    UpdateTextbox();
}

private void SetDateTimePickerFirstOfMonth()
{
    DateTime current = dtOtMonth.Value;
    if (current.Day != 1)
    {
        DateTime newValue = new DateTime(current.Year, current.Month, 1);
        dtOtMonth.Value = newValue;
    }
}

private void UpdateTextbox()
{
    int year = dtOtMonth.Value.Year;
    int month = dtOtMonth.Value.Month;

    txtResult.Text = string.Format("Year is {0} and month is {1}", year, month);
}

private void dtOtMonth_ValueChanged(object sender, EventArgs e)
{
    SetDateTimePickerFirstOfMonth();
    UpdateTextbox();
}

使用此代码,我仍然可以使用箭头键更改DateTimePicker 中的年份或月份,并且它将确保 DateTimePicker 值始终设置为所选月份的第一个月,即使是特定日期被选中。

这是演示应用程序的屏幕截图。 DateTimePicker 的值一更改,文本框的值就会更改。

【讨论】:

  • @code_Finder 太好了! :) 如果它帮助您解决问题,请随时将答案标记为已接受。
【解决方案2】:

增加一个值需要一天的时间。似乎它正在解析一个日期,并且由于没有日期而无法做到这一点。

【讨论】:

  • 正是我发现的
【解决方案3】:

我终于找到了错误。

在日期时间选择器中,如果不是,我们会初始化默认日期,即使我们只采用月份和年份,在它内部采用默认日期(在我的例子中是今天的日期)。

因此,当我更改月份时,每个月的选定日期保持不变。因此,今天是 2016 年 3 月 30 日,当我尝试将月份减一时,它是 2 月,日期时间选择器尝试将 30 日初始化为二月,它会产生错误。

就我而言,就我使用月份和年份而言,我将日期分开。

我通过在表单加载事件中初始化当月第一天的日期来解决这个问题,其中每个月都有第一天(一个)而不是 29、30、31

dtOtMonth.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
dtOtMonth.Format = DateTimePickerFormat.Custom;
dtAdvanceStartDate.CustomFormat = "MM/yyyy";

这里有一些非常有用的链接,我发现这是一个 .NET 错误。

https://connect.microsoft.com/VisualStudio/feedback/details/553375/system-argumentoutofrangeexception-year-month-and-day-parameters-describe-an-un

https://social.msdn.microsoft.com/Forums/windows/en-US/69b10b5b-a034-426e-a045-2bed8a1637a3/bug-in-datetimepicker-control?forum=winforms

【讨论】:

  • 很高兴您解决了这个问题,但它听起来不是很可靠。如果您只需要年和月,也许您可​​以将这些值存储为两个单独的整数值,而不是使用日期时间。下面是如何从 DateTimePicker 中提取这些值的示例:stackoverflow.com/a/29454651/717088
  • 我添加了一个答案,并附有如何在 Winforms 项目中执行此操作的示例。
猜你喜欢
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-24
  • 1970-01-01
  • 2013-03-02
  • 2020-06-28
  • 1970-01-01
相关资源
最近更新 更多