Try to Parse hour:minute value with custom format specifier in CSharp

Description

The following code shows how to try to Parse hour:minute value with custom format specifier.

Example


using System;//from  w  w  w. j a  v a2 s.c o m
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string intervalString, format;
      TimeSpan interval;
      CultureInfo culture = null;

      intervalString = "17:14";
      format = "h\\:mm";
      culture = CultureInfo.CurrentCulture;
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 culture, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);

   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var