XmlConvert.ToString Method (DateTimeOffset, String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Converts the supplied DateTimeOffset to a String in the specified format.

Namespace:   System.Xml
Assembly:  System.Xml (in System.Xml.dll)

public static string ToString(
	DateTimeOffset value,
	string format


Type: System.DateTimeOffset

The DateTimeOffset to be converted.

Type: System.String

The format to which s is converted. The format parameter can be any subset of the W3C Recommendation for the XML dateTime type. (For more information see http://www.w3.org/TR/xmlschema-2/#dateTime.)

Return Value

Type: System.String

A String representation in the specified format of the supplied DateTimeOffset.

The following example converts a DateTimeOffset representation of the current time to a String in the specified format.

using System;
using System.Xml;

class Example
    static void Main()
        // Create the DateTimeOffset object and set the time to the current time.
        DateTimeOffset dto;
        dto = DateTimeOffset.Now;

        // Convert the DateTimeObject to a string in a specified format and display the result.
        // The specified format must be a subset of the W3C Recommendation for the XML dateTime type.
        String timeAsString = XmlConvert.ToString(dto, "yyyy-MM-ddTHH:mm:sszzzzzzz");

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top