Use "o" format specifier to create a string, then restore the original date and time value by calling a date and time Parse method in CSharp

Description

The following code shows how to use "o" format specifier to create a string, then restore the original date and time value by calling a date and time Parse method.

Example


/* w w  w  . j a  va  2  s . co m*/

using System;
using System.Globalization;
public class MainClass {     
    public static void Main()
    {
        // Round-trip DateTime values.
        DateTime originalDate, newDate;
        string dateString;
        // Round-trip a local time.
        originalDate =
           DateTime.SpecifyKind(new DateTime(2008, 4, 10, 6, 30, 0), DateTimeKind.Local);
        dateString = originalDate.ToString("o");
        newDate = DateTime.Parse(dateString, null, DateTimeStyles.RoundtripKind);
        Console.WriteLine("Round-tripped {0} {1} to {2} {3}.",
                      originalDate, originalDate.Kind, newDate, newDate.Kind);

    }

}

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