计算特定日期的天数

时间:2014-09-27 19:14:21

标签: vb.net datetime

Dim intYear, intMonth, intDay As Integer, strResult As String, tspResult As TimeSpan


        intYear = Integer.Parse(txtYear.Text)
        intMonth = Integer.Parse(txtMonth.Text)
        intDay = Integer.Parse(txtDay.Text)

Dim dteDatum As New System.DateTime(intYear, intMonth, intDay)

            tspResult = Now.Subtract(dteDate)
        strResult = Math.Abs(tspResult.Days).ToString
            MessageBox.Show(strResult)

例如: 今天是10月1日,我想知道到10月4日多少天。 该计划将说2天,但这是错的,它必须是3天。 我怎样才能解决这个问题? (过去的计算很好) 如果我的英语很糟糕,请提前致谢并抱歉。

1 个答案:

答案 0 :(得分:2)

Try use datediff

' The following statements set datTim1 to a Thursday
' and datTim2 to the following Tuesday.
Dim datTim1 As Date = #1/4/2001#
Dim datTim2 As Date = #1/9/2001#
' Assume Sunday is specified as first day of the week.
Dim wD As Long = DateDiff(DateInterval.Weekday, datTim1, datTim2)
Dim wY As Long = DateDiff(DateInterval.WeekOfYear, datTim1, datTim2)