C# DateTimeOffset ToString(String)

Description

DateTimeOffset ToString(String) converts the value of the current DateTimeOffset object to its equivalent string representation using the specified format.

Syntax

DateTimeOffset.ToString(String) has the following syntax.


public string ToString(
  string format
)

Parameters

DateTimeOffset.ToString(String) has the following parameters.

  • format - A format string.

Returns

DateTimeOffset.ToString(String) method returns A string representation of the value of the current DateTimeOffset object, as specified by format.

Example

The following example displays a DateTimeOffset object to the console using each of the standard date and time format specifiers.


/*w w  w  .j av a2 s . c o  m*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    DateTimeOffset outputDate = new DateTimeOffset(2014, 10, 31, 21, 0, 0, 
                                         new TimeSpan(-8, 0, 0));
    string specifier; 
    
    specifier = "d";
    Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 
    
    specifier = "D";
    Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 
    
    specifier = "t";
    Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 
    
    specifier = "T";
    Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 
    
    specifier = "f";
    Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 
    
    specifier = "F";
    Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 
    
    specifier = "g";
    Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 
    
    specifier = "G";
    Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 
    
    specifier = "M";           // 'm' is identical 
    Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 
    
    specifier = "R";           // 'r' is identical 
    Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 
    
    specifier = "s";
    Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 
    
    specifier = "u";
    Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 
    
    specifier = "Y";         // 'y' is identical 
    Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));

  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version