Click here to Skip to main content
15,881,803 members
Articles / Web Development / ASP.NET
Tip/Trick

12/24 Hour Time format

Rate me:
Please Sign up or sign in to vote.
4.83/5 (16 votes)
5 Jun 2011CPOL 230.1K   8   6
Display Date Time in 12/24 hour time format in C#/VB.NET

Code


C#.NET
C#
protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("With Seconds" + "<br />");
    Response.Write("12 Hour Date Format : " + DateTime.Now.ToString("hh:mm:ss tt") + "<br />");
    Response.Write("24 Hour Date Format : " + DateTime.Now.ToString("HH:mm:ss tt") + "<br />");
    Response.Write("Without Seconds" + "<br />");
    Response.Write("12 Hour Date Format : " + DateTime.Now.ToString("hh:mm tt") + "<br />");
    Response.Write("24 Hour Date Format : " + DateTime.Now.ToString("HH:mm tt") + "<br />");
}

VB.NET
VB
Protected Sub Page_Load(sender As Object, e As EventArgs)
	Response.Write("With Seconds" & "<br />")
	Response.Write("12 Hour Date Format : " & DateTime.Now.ToString("hh:mm:ss tt") & "<br />")
	Response.Write("24 Hour Date Format : " & DateTime.Now.ToString("HH:mm:ss tt") & "<br />")
	Response.Write("Without Seconds" & "<br />")
	Response.Write("12 Hour Date Format : " & DateTime.Now.ToString("hh:mm tt") & "<br />")
	Response.Write("24 Hour Date Format : " & DateTime.Now.ToString("HH:mm tt") & "<br />")
End Sub

Output


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

Further Reading


Custom Date and Time Format Strings[^]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice Pin
Sibeesh Venu29-Jul-14 19:36
professionalSibeesh Venu29-Jul-14 19:36 
SuggestionAll type of formates Pin
Farhan Ghumra8-Aug-12 23:07
professionalFarhan Ghumra8-Aug-12 23:07 
General24H PM? Pin
Morten Gorm Madsen12-Jul-12 21:41
Morten Gorm Madsen12-Jul-12 21:41 
GeneralRe: 24H PM? Pin
ravithejag2-Nov-15 21:45
ravithejag2-Nov-15 21:45 
GeneralReason for my vote of 3 Useful Pin
ramakrishnankt1-Jul-11 4:44
ramakrishnankt1-Jul-11 4:44 
GeneralRe: *sigh* I think you clicked Vote <b>3</b> instead of Vote <b>... Pin
thatraja1-Jul-11 5:09
professionalthatraja1-Jul-11 5:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.