Get-Date cast to string vs ToString() Get-Date cast to string vs ToString() powershell powershell

Get-Date cast to string vs ToString()


If an object implements the IFormattable interface, then PowerShell will call IFormattable.ToString instead of Object.ToString for the cast operation. A similar thing happens for static Parse method: if there is an overload with IFormatProvider parameter, then it would be called.

Add-Type -TypeDefinition @'    using System;    using System.Globalization;    public class MyClass:IFormattable {        public static MyClass Parse(string str) {            return new MyClass{String=str};        }        public static MyClass Parse(string str,IFormatProvider fp) {            return new MyClass{String=str,FormatProvider=((CultureInfo)fp).DisplayName};        }        public string String {get;private set;}        public string FormatProvider {get;private set;}        public override string ToString() {            return "Object.ToString()";        }        string IFormattable.ToString(string format,IFormatProvider fp) {            return string.Format("IFormattable.ToString({0},{1})",format,((CultureInfo)fp).DisplayName);        }    }'@[String](New-Object MyClass) #Call IFormattable.ToString(null,CultureInfo.InvariantCulture)[MyClass]'Test'              #Call MyClass.Parse("Test",CultureInfo.InvariantCulture)


Your question is not PowerShell question, but a .NET question. PowerShell scripts can use the .NET structure [datetime] as it is, i.e., PowerShell does not change its behavior.

What are the rules for casting an object to a string, if not calling ToString()?

Casting uses culture invariant definitions. The ToString() method can contain culture dependent implementations, because it is overridable.

Is the DateTime class simply a special case because of its overloading of ToString(String)?

First, DateTime is not a class; it's a structure.Second, there's no ToString() method overloading; in this case, the correct denomination is overriding (it is overriding the Object.ToString() method).

To understand better what I'm meaning, have fun with these pretty funny date and time printing in different cultures (copy, paste and run):

function f{    $x=get-date    [CultureInfo]$currentCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture    [System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('en-US')    $x.ToString()    [System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('ar-IQ')    $x.ToString()    [System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('de-DE')    $x.ToString()    [System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('ru-RU')    $x.ToString()    [System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('fr-FR')    $x.ToString()    [System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('zh-CN')    $x.ToString()    [System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('zh-HK')    $x.ToString()    [System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('zh-TW')    $x.ToString()    [System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('hu-HU')    $x.ToString()    [System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('ko-KR')    $x.ToString()    [System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('ja-JP')    $x.ToString()    [System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('ka-GE')    $x.ToString()    [System.Threading.Thread]::CurrentThread.CurrentCulture=[CultureInfo]::CreateSpecificCulture('pt-BR')    $x.ToString()    [System.Threading.Thread]::CurrentThread.CurrentCulture=$currentCulture}f

Notice that the code above will produce different printing if run either in ISE or in non-ISE versions of PowerShell.