什麼是使用VB.NET來確定當前時間是AM還是PM的最佳方法?確定當前時間是AM還是PM的最簡單方法?
目前我使用如果Date.Today.ToString.Contains(「AM」),但我敢肯定有一個更好的方法。
Good <%If Date.Today.ToString.Contains("AM") Then Response.Write("Morning") Else Response.Write("Afternoon")%>
什麼是使用VB.NET來確定當前時間是AM還是PM的最佳方法?確定當前時間是AM還是PM的最簡單方法?
目前我使用如果Date.Today.ToString.Contains(「AM」),但我敢肯定有一個更好的方法。
Good <%If Date.Today.ToString.Contains("AM") Then Response.Write("Morning") Else Response.Write("Afternoon")%>
If Date.Now.Hour < 12 Then
...也許?
如何:
Now.Hour > 17 ' Night
Now.Hour < 5 ' Day
HTH
Now.ToString("t") == "A" //or //"P"
如何:
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
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)
下午檢查必須< =否則在小時17個STR訥韋爾獲取設置
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
不Date.Now.Hour總是返回0-23,或者它可以再回到1-12? – 2008-12-23 19:08:33
它將始終返回0-23。顯示它,從而將其轉換爲字符串,可能會顯示0-11/12等,但這是一個本地化/顯示問題。 Time屬性將始終爲00:00:00 - 23:59:59(+毫秒等) – 2008-12-23 19:15:45
請參閱:http://msdn.microsoft.com/en-us/library/system.datetime.hour.aspx在評論中它說:Hour屬性的值總是用24小時製表示。 – 2008-12-23 19:16:35