我一直试图找到如何在其中声明具有时间格式的变量的方法。我发现的所有结果只讲授如何仅在Date变量中存储日期。
实际目标: 今天上午6:00:00 今天,并在今天下午1:59:59将其范围扩展到
感谢。
答案 0 :(得分:2)
您的问题不是100%明确,而是尝试用可能对您有所帮助的一些示例来回答:
'get todays date with 6AM time
Dim sixAmToday As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 6, 0, 0)
'get todays date with 1:59 PM
Dim oneFiftyNineToday As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 13, 59, 59)
'get the timespan between the two dates
Dim ts As TimeSpan = oneFiftyNineToday - sixAmToday
Debug.WriteLine(ts.ToString) 'outputs 07:59:59
答案 1 :(得分:0)
另一种方法可以做到这一点。在相同的24小时内使用军事时间可以更容易地减去同一天的时间。如果您需要从下一个或上一个时段中减去时间,请使用timespan或datediff,并包括日期和时间。选择“h”或小时作为返回值。
Dim timeString As New StringBuilder
Dim beginOfShift As DateTime = CDate("06:00")
Dim endOfShit As DateTime = CDate("13:59:59")
Dim beginOfShiftToShow As String = beginOfShift.ToShortTimeString
Dim endOfShiftToShow As String = endOfShit.ToShortTimeString
Dim resultOfShiftDiff As TimeSpan = endOfShit.Subtract(beginOfShift)
timeString.AppendFormat("{0} {1}", "Number of Hours in this shift:", resultOfShiftDiff.ToString)
MsgBox(timeString.ToString)
http://msdn.microsoft.com/en-us/library/b5xbyt6f(v=vs.90).aspx