65.9K
CodeProject 正在变化。 阅读更多。
Home

12/24 小时制时间格式

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.83/5 (15投票s)

2011年6月5日

CPOL
viewsIcon

232774

在 C#/VB.NET 中以 12/24 小时制显示日期时间

代码

C#.NET
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("With Seconds" + "
"); Response.Write("12 Hour Date Format : " + DateTime.Now.ToString("hh:mm:ss tt") + "
"); Response.Write("24 Hour Date Format : " + DateTime.Now.ToString("HH:mm:ss tt") + "
"); Response.Write("Without Seconds" + "
"); Response.Write("12 Hour Date Format : " + DateTime.Now.ToString("hh:mm tt") + "
"); Response.Write("24 Hour Date Format : " + DateTime.Now.ToString("HH:mm tt") + "
"); }
VB.NET
Protected Sub Page_Load(sender As Object, e As EventArgs)
	Response.Write("With Seconds" & "
") Response.Write("12 Hour Date Format : " & DateTime.Now.ToString("hh:mm:ss tt") & "
") Response.Write("24 Hour Date Format : " & DateTime.Now.ToString("HH:mm:ss tt") & "
") Response.Write("Without Seconds" & "
") Response.Write("12 Hour Date Format : " & DateTime.Now.ToString("hh:mm tt") & "
") Response.Write("24 Hour Date Format : " & DateTime.Now.ToString("HH:mm tt") & "
") End Sub

输出

With Seconds
12 Hour Date Format : 03:01:31 PM
24 Hour Date Format : 15:01:31 PM
Without Seconds
12 Hour Date Format : 03:01 PM
24 Hour Date Format : 15:01 PM

延伸阅读

自定义日期和时间格式字符串[^]
© . All rights reserved.