DateTime format Time String

DateTime format Time String

To get hour or minute or second parts of time representation of DateTime instance, You need to format Time as shown in this tutorial. We will see how to format time using (C#.net) and [VB.net]. You can extract only time part from date.

C# DateTime format

[C#] programming example of Time Format string.

It's very simple in .net, you need to specify formatting string to the instance of date and time and get appropriate string representation of hour,minute or seconds

using System;
namespace TimeString.Format.Example
{
class Program
{
static void Main(string[] args)
{
DateTime Today = DateTime.Now;     
string hourString = "hh";
string minuteString = "mm";
string secondString = "ss";
Console.WriteLine("Hour of date time {0} is          {1} Hours.", Today.ToString(), 
Today.ToString(hourString)); Console.WriteLine("Minute of date time {0} is {1} Minute.", Today.ToString(),
Today.ToString(minuteString)); Console.WriteLine("Second of date time {0} is {1} Seconds.", Today.ToString(),
Today.ToString(secondString)); Console.ReadLine(); } } } Output Hour of date time 12/31/2011 4:41:36 PM is 04 Hours. Minute of date time 12/31/2011 4:41:36 PM is 41 Minute. Second of date time 12/31/2011 4:41:36 PM is 36 Seconds.
Date Time Example C#

(Vb.Net) Example of time format String.

Namespace TimeString.Format.Example
Class Program
Private Shared Sub Main(args As String())
Dim Today As DateTime = DateTime.Now
Dim hourString As String = "hh"
Dim minuteString As String = "mm"
Dim secondString As String = "ss"
Console.WriteLine("Hour of date time {0} is          {1} Hours", Today.ToString(), 
Today.ToString(hourString)) Console.WriteLine("Minute of date time {0} is {1} Minute", Today.ToString(),
Today.ToString(minuteString)) Console.WriteLine("Second of date time {0} is {1} Seconds", Today.ToString(),
Today.ToString(secondString)) Console.ReadLine() End Sub End Class End Namespace
Remember

The time format string used here is case sensitive.

Tags:

DateTime format Time StringDateTimeTryParse with IFormatProvider culture information using c#C# Convert String To DateTimeDateTime ParseExact Time 10:10:13 AM hh mm ss tt nullDateTimeTryParse using C# and VB.net exampleIFormatProvider DateTime C#.netC# DateTime DifferenceC# Parse method of DateTimeAsp.net Convert String To ImageDateTime.Today Vs DateTime.Now.net String vs string

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.