How to print a formatted date in F #

I have this date in F #

let myDate = new DateTime(2015, 06, 02)

And I want to display it as "2015/06/02" in the console window. I tried:

Console.WriteLine(sprintf "%s" myDate.ToString("yyyy/MM/dd"))

But this does not compile (the compiler says: "Consecutive arguments must be separated by spaces or alternating, and arguments with applications of functions or methods must be enclosed in brackets")

How to display date as "2015/06/02"?

UPDATE:

As Panagiotis Kanavos commented, this will work:

Console.WriteLine("{0:yyyy/MM/dd}", myDate)
+2
source share
1 answer

You can easily call an overload ToStringthat takes a format string:

let formatted = myDate.ToString "yyyy/MM/dd"

However, it sprintfdoes not support this in short form, but you can do this:

printfn "%s" (myDate.ToString "yyyy/MM/dd")

, , :

let inline stringf format (x : ^a) = 
    (^a : (member ToString : string -> string) (x, format))

. , :

myDate |> stringf "yyyy/MM/dd" |> printfn "%s"

:

(stringf "yyyy/MM/dd" >> printfn "%s") myDate
+8

All Articles