Parse strings to TimeSpan : Time Span Parse « Date Time « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Date Time » Time Span ParseScreenshots 
Parse strings to TimeSpan
  

using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      string[] values = "6""6:12""6:12:14""6:12:14:45"
                          "6.12:14:45""6:12:14:45.3448"
                          "6:12:14:45,3448""6:34:14:45" };
      string[] cultureNames = "hr-HR""en-US"};

      foreach (string cultureName in cultureNames)
      {
         Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
         foreach (string value in values)
         {
            try {
               TimeSpan ts = TimeSpan.Parse(value);
               Console.WriteLine("{0} --> {1}", value, ts.ToString("c"));
            }
            catch (FormatException) {
               Console.WriteLine("{0}: Bad Format", value);
            }   
            catch (OverflowException) {
               Console.WriteLine("{0}: Overflow", value);
            }
         }                               
      }
   }
}

   
    
  
Related examples in the same category
1.Parse string to TimeSpan
2.Parse string to TimeSpan: "18:31.05"
3.Parse string to TimeSpan: "12.035"
4.TimeSpan.Parse("0:0:3.669")
5.Parse hour:minute value with "g" specifier current culture.
6.Parse hour:minute:second value with "G" specifier.
7.Parse hours:minute.second value with "G" specifier and current (en-US) culture.
8.Parse days:hours:minute.second value with "G" specifier and current (en-US) culture.
9.Parse days:hours:minute.second value with "G" specifier and fr-FR culture.
10.Parse a single number using the "c" standard format string.
11.Parse a single number using the "%h" custom format string.
12.Is Date valid
13.Parse Rfc 2822 Date
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.