C# DateTimeOffset ToString(String, IFormatProvider)

Description

DateTimeOffset ToString(String, IFormatProvider) converts the value of the current DateTimeOffset object to its equivalent string representation using the specified format and culture-specific format information.

Syntax

DateTimeOffset.ToString(String, IFormatProvider) has the following syntax.


public string ToString(
  string format,
  IFormatProvider formatProvider
)

Parameters

DateTimeOffset.ToString(String, IFormatProvider) has the following parameters.

  • format - A format string.
  • formatProvider - An object that supplies culture-specific formatting information.

Returns

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

Example

The following example uses the ToString(String, IFormatProvider) method to display a DateTimeOffset object using a custom format string for several different cultures.


//from ww w .  j  av a2 s .c  o m
using System;
using System.Globalization;
public class MainClass{
  public static void Main(String[] argv){  
    
    DateTimeOffset outputDate = new DateTimeOffset(2014, 11, 1, 9, 0, 0, 
                                         new TimeSpan(-7, 0, 0)); 
    string format = "dddd, MMM dd yyyy HH:mm:ss zzz";
    
    Console.WriteLine(outputDate.ToString(format, null as DateTimeFormatInfo));
    Console.WriteLine(outputDate.ToString(format, CultureInfo.InvariantCulture));
    Console.WriteLine(outputDate.ToString(format, 
                                          new CultureInfo("fr-FR")));
    Console.WriteLine(outputDate.ToString(format, 
                                          new CultureInfo("es-ES")));
  }
}

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