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.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5