Try to Parse hour:minute value with "g" specifier current culture in CSharp

Description

The following code shows how to try to Parse hour:minute value with "g" specifier current culture.

Example


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

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

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

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