Try to parse string ("05/01/2014 6:00:00AM +5:00") to DateTimeOffset in CSharp

Description

The following code shows how to try to parse string ("05/01/2014 6:00:00AM +5:00") to DateTimeOffset.

Example


using System;/* w  w w.  j ava2s  .  co  m*/
using System.Globalization;
public class MainClass{
  public static void Main(String[] argv){  
    string dateString;
    DateTimeOffset parsedDate;

    // Parse and convert to UTC 
    dateString = "05/01/2014 6:00:00AM +5:00";
    if (DateTimeOffset.TryParse(dateString, null as IFormatProvider, 
                               DateTimeStyles.AdjustToUniversal, 
                               out parsedDate))
       Console.WriteLine("'{0}' was converted to to {1}.", 
                         dateString, parsedDate.ToString());
    else
       Console.WriteLine("Unable to parse '{0}'.", dateString);    

  }
}

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