Mega Code Archive

 
Categories / C# / Date Time
 

Convert To TimeSpan

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace DateTimeUtils {   public static class Conversion {     public static TimeSpan ConvertToTimeSpan( string time ) {       TimeSpan result = TimeSpan.Zero;       if ( string.IsNullOrEmpty( time ) ) throw new ArgumentNullException( "No time given." );       Regex regex = new Regex( "^([0-1][0-9]|[2][0-3]):([0-5][0-9])$" );       if ( !regex.IsMatch( time ) ) throw new FormatException( "Invalid time format" );       TimeSpan.TryParse( time, out result );             return result;     }   } }