vb.net 确定当前时间是上午还是下午的最简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/389761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simplest method to determine if the current time is AM or PM?
提问by Brian Boatright
What is the best method to determine if the current time is AM or PM using VB.NET?
使用 VB.NET 确定当前时间是 AM 还是 PM 的最佳方法是什么?
Currently I'm using If Date.Today.ToString.Contains("AM") but I'm sure there is a better method.
目前我正在使用 If Date.Today.ToString.Contains("AM") 但我确定有更好的方法。
Good <%If Date.Today.ToString.Contains("AM") Then Response.Write("Morning") Else Response.Write("Afternoon")%>
回答by Lasse V. Karlsen
If Date.Now.Hour < 12 Then
... perhaps?
If Date.Now.Hour < 12 Then
... 也许?
回答by Jacob Adams
Now.ToString("t") == "A" //or //"P"
回答by yet another programmer
The afternoon check needs to be <= otherwise at hour 17 str nevers gets set
下午检查需要 <= 否则在 17 小时 str nevers 被设置
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
回答by Colby Africa
How about this:
这个怎么样:
Now.Hour > 17 ' Night
Now.Hour < 5 ' Day
HTH
HTH
回答by Eppz
How about this:
这个怎么样:
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
回答by Greg
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
Usage:
用法:
Dim isAM as Boolean = (IsMidnight(current) Or current.Hour < 12)