【问题标题】:VB - Difference between 2 datesVB - 两个日期之间的差异
【发布时间】:2012-03-01 04:26:25
【问题描述】:

我试图找出两个日期之间的差异。两个日期中的第一个可以更改,更改时必须保留原始日期之间的差异。这意味着我必须自动将第二个日期更改为与以前相同的天数差异,但使用新的第一个日期。

Public Sub New()

    InitializeComponent()

    currDate = txt_ProjStart.Text

End Sub

Private Sub txt_ProjStart_TextChanged.....

If txt_ProjEnd.Text.Length > 0 Then

    Dim newDate As Date
    newDate = txt_ProjStart.Text




    Dim endDate As Date
    endDate = txt_ProjEnd.Text


End If

我有旧 date1(currDate) 和新 date1 值(newDate) 的值。我需要得到这些之间的差异,然后从 endDate 开始添加或减去这些天数。

任何帮助将不胜感激!谢谢!

【问题讨论】:

  • DateTime.Parse(txt_ProjectEnd.Text) - DateTime.Parse(txt_ProjectStart.Text)
  • 您可以通过减去日期来获得 TimeSpan - 然后只需将它们添加回您的第一个(更改的)日期即可获得另一个具有相同差异的日期 - 我猜你得到了 算法 自己工作 ;)

标签: .net vb.net winforms


【解决方案1】:
Dim newDate, endDate, someOtherDate As Date

If Date.TryParse(txt_ProjStart.Text, newDate) AndAlso _
   Date.TryParse(txt_ProjEnd.Text, endDate) Then

    Dim diff As TimeSpan = endDate - newDate
    Dim result As Date = someOtherDate + diff
End If

日期和时间跨度会自动相互转换。此外,最好使用安全的日期转换。如果日期无效,TryParse 不会产生异常。而是返回False

【讨论】:

    猜你喜欢
    • 2011-10-29
    • 1970-01-01
    • 2014-02-12
    • 2015-09-14
    • 2015-06-21
    • 2020-02-06
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多