Click here to Skip to main content
11,484,752 members (64,386 online)
Click here to Skip to main content

12/24 Hour Time format

, 5 Jun 2011 CPOL 100.6K 5
Rate this:
Please Sign up or sign in to vote.
Display Date Time in 12/24 hour time format in C#/VB.NET

Code

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

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)

Share

About the Author

thatraja
Web Developer
India India
Programmer | Dude | ||||||

About him & his blog? check out here. Currently he's updating the site, he'll return soon there with more things*.

In Codeproject, he posted a blogpost & more than couple of Tip/Tricks, hope he'll write more soon.

Favorite techs are MVC, WCF, Mobile, HTML5, CSS3, jQuery, etc.,

His hobbies are watching movies, CP, being alone, etc., But now he's working on something*.

* - SCAMP???
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionNice Pin
Sibeesh Venu29-Jul-14 20:36
professionalSibeesh Venu29-Jul-14 20:36 
SuggestionAll type of formates Pin
Farhan Ghumra9-Aug-12 0:07
memberFarhan Ghumra9-Aug-12 0:07 
General24H PM? Pin
Morten Gorm Madsen12-Jul-12 22:41
memberMorten Gorm Madsen12-Jul-12 22:41 
GeneralReason for my vote of 3 Useful Pin
ramakrishnankt1-Jul-11 5:44
memberramakrishnankt1-Jul-11 5:44 
GeneralRe: *sigh* I think you clicked Vote <b>3</b> instead of Vote <b>... Pin
thatraja1-Jul-11 6:09
mvpthatraja1-Jul-11 6:09 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 5 Jun 2011
Article Copyright 2011 by thatraja
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid