使用VB.NET确定当前时间是AM还是PM的最佳方法是什么?
目前我正在使用If Date.Today.ToString.Contains(“AM”),但我确信有更好的方法。
Good <%If Date.Today.ToString.Contains("AM") Then Response.Write("Morning") Else Response.Write("Afternoon")%>
答案 0 :(得分:15)
If Date.Now.Hour < 12 Then
...也许?
答案 1 :(得分:4)
Now.ToString("t") == "A" //or //"P"
答案 2 :(得分:2)
这个怎么样:
Now.Hour > 17 ' Night
Now.Hour < 5 ' Day
HTH
答案 3 :(得分:2)
下午检查需要&lt; =否则在17小时设置
dim hours as int = DateTime.Now.Hour
dim str as string
if hours < 12 Then
str = "Morning"
else if hours <= 17 then
str = "Afternoon"
else if hours > 17 then
str = "Evening"
End If
答案 4 :(得分:1)
这个怎么样:
dim hours as int = DateTime.Now.Hour
dim str as string
if hours < 12 Then
str = "Morning"
else if hours < 17 then
str = "Afternoon"
else if hours > 17 then
str = "Evening"
End If
答案 5 :(得分:0)
Private Function IsMidnight(ByVal value As DateTime) As Boolean
Return value.Hour = 0 And _
value.Minute = 0 And _
value.Second = 0 And _
value.Millisecond = 0
End Function
用法:
Dim isAM as Boolean = (IsMidnight(current) Or current.Hour < 12)