Error: System.ArgumentOutOfRangeException: 'Year, Month, and Day parameters describe an un-representable DateTime.'

huangapple go评论46阅读模式
英文:

Error: System.ArgumentOutOfRangeException: 'Year, Month, and Day parameters describe an un-representable DateTime.'

问题

以下是您要翻译的内容:

"Can someone help me?

My algorithm should be this:
(Note: I used DateEdit from DevExpress not DateTimePicker)

This is my output
OBTAForTheMonthDateEdit - DateEdit1
OBTADeadlineDateEdit - DateEdit2 (disable for display use only)
LiquidationDeadlineDateEdit - DateEdit3 (disable for display use only)

Example: OBTAForTheMonthDateEdit the selected date is = 12/1/2022 (you can choose any day it doesn't matter)
Display: OBTADeadlineDateEdit = 11/18/2022 (should be the 18th day of the month and before the month you chose.)
LiquidationDeadlineDateEdit = 1/5/2023 (should be the 5th day of the month and after the month you chose.)

I used this code but error: System.ArgumentOutOfRangeException: 'Year, Month, and Day parameters describe an un-representable DateTime.'

    Private Sub OBTAForTheMonthDateEdit_TextChanged(sender As Object, e As EventArgs) Handles OBTAForTheMonthDateEdit.TextChanged
       Dim dt As Date = OBTAForTheMonthDateEdit.EditValue

       OBTADeadlineDateEdit.Text = New DateTime(dt.Year, dt.Month - 1, 18)
       LiquidationDeadlineDateEdit.Text = New DateTime(dt.Year, dt.Month + 1, 5)
    End Sub

Please help me regarding for this matter. thank you so much!"

英文:

Can someone help me?

My algorithm should be this:
(Note: I used DateEdit from DevExpress not DateTimePicker)

This is my output
OBTAForTheMonthDateEdit - DateEdit1
OBTADeadlineDateEdit - DateEdit2 (disable for display use only)
LiquidationDeadlineDateEdit - DateEdit3 (disable for display use only)

Example: OBTAForTheMonthDateEdit the selected date is = 12/1/2022 (you can choose any day it doesn't matter)
Display: OBTADeadlineDateEdit = 11/18/2022 (should be the 18th day of the month and before the month you chose.)
LiquidationDeadlineDateEdit = 1/5/2023 (should be the 5th day of the month and after the month you chose.)

I used this code but error: System.ArgumentOutOfRangeException: 'Year, Month, and Day parameters describe an un-representable DateTime.'

    Private Sub OBTAForTheMonthDateEdit_TextChanged(sender As Object, e As EventArgs) Handles OBTAForTheMonthDateEdit.TextChanged
       Dim dt As Date = OBTAForTheMonthDateEdit.EditValue

       OBTADeadlineDateEdit.Text = New DateTime(dt.Year, dt.Month - 1, 18)
       LiquidationDeadlineDateEdit.Text = New DateTime(dt.Year, dt.Month + 1, 5)
    End Sub

Please help me regarding for this matter. thank you so much!

答案1

得分: 1

例子:OBTAForTheMonthDateEdit所选择的日期是=2022年12月1日

如果是这样,你认为dt.Month - 1的值会是多少?它将为零。零是一个有效的月份吗?不是的,所以这就是你的问题。与其从月份值中添加或减去,然后构造日期,你应该首先构造日期,然后再添加或减去一个月:

OBTADeadlineDateEdit.Text = New DateTime(dt.Year, dt.Month, 18).AddMonths(-1)
LiquidationDeadlineDateEdit.Text = New DateTime(dt.Year, dt.Month, 5).AddMonths(1)

编辑:

我意识到指定的输入是美国格式,在这种情况下,它表示的是12月1日,而不是1月12日。但问题基本上是一样的。这只是意味着问题是加法产生13,而不是减法产生零。解决方案仍然是相同的,即首先构造Date值,然后添加或减去一个月。我像这样测试了我的解决方案:

Dim dt = #12/1/2022#
Dim dt1 = New Date(dt.Year, dt.Month, 18).AddMonths(-1)
Dim dt2 = New Date(dt.Year, dt.Month, 5).AddMonths(1)

Console.WriteLine(dt1.ToLongDateString())
Console.WriteLine(dt2.ToLongDateString())

输出是:

星期五,2022年11月18日
星期四,2023年1月5日

正如你所期望的那样。

英文:

> Example: OBTAForTheMonthDateEdit the selected date is = 12/1/2022

If that's true then what do you think the value of dt.Month - 1 will be? It will be zero. Is zero a valid month? No it is not, so that's your problem. Rather than adding or subtracting from the month value and then constructing a date, you should be constructing the date first, then adding or subtracting a month:

OBTADeadlineDateEdit.Text = New DateTime(dt.Year, dt.Month, 18).AddMonths(-1)
LiquidationDeadlineDateEdit.Text = New DateTime(dt.Year, dt.Month, 5).AddMonths(1)

EDIT:

I realised that the specified input was in US format, in which case it represents December 1st rather than January 12th. The issue is still basically the same though. It just means that the specific issue is the addition producing 13 rather than the subtraction producing zero. The solution is still the same, i.e. construct the Date value first, then add or subtract a month. I tested my solution like this:

Dim dt = #12/1/2022#
Dim dt1 = New Date(dt.Year, dt.Month, 18).AddMonths(-1)
Dim dt2 = New Date(dt.Year, dt.Month, 5).AddMonths(1)

Console.WriteLine(dt1.ToLongDateString())
Console.WriteLine(dt2.ToLongDateString())

and the output was:
<pre>
Friday, 18 November 2022
Thursday, 5 January 2023
</pre>
exactly as you'd expect.

huangapple
  • 本文由 发表于 2023年3月7日 10:09:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定