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

Description

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

Example


//w w w . j ava 2  s. c o m
using System;
using System.Globalization;
public class MainClass{
  public static void Main(String[] argv){  
    string dateString;
    DateTimeOffset parsedDate;
    
    dateString = "05/01/2014 6:00:00";
    // Assume time is local  
    if (DateTimeOffset.TryParse(dateString, null as IFormatProvider, 
                                DateTimeStyles.AssumeLocal, 
                                out parsedDate))
       Console.WriteLine("'{0}' was converted to to {1}.", 
                         dateString, parsedDate.ToString());
    else
       Console.WriteLine("Unable to parse '{0}'.", dateString);    
    
    // Assume time is UTC 
    if (DateTimeOffset.TryParse(dateString, null as IFormatProvider, 
                                DateTimeStyles.AssumeUniversal, 
                                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