How to print formatted date in F#

mrturtle

I have this date in F#

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

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

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

But this does not compile (compiler says, "Successive arguments should be separated by spaces or tupled, and arguments involving function or method applications should be parenthesized")

How would I output the date as "2015/06/02"?

UPDATE:

As commented by Panagiotis Kanavos, this will work:

Console.WriteLine("{0:yyyy/MM/dd}", myDate)
Mark Seemann

You easily can call the ToString overload that takes a format string:

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

However, sprintf doesn't support that in short form, but you could do this:

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

You can also define a function for this purpose, if you feel that calling a method on an object isn't sufficiently functional:

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

which would enable you to compose functions in many interesting ways. You could for example write to the console like this:

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

or like this:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to print a formatted SOAPMessage?

From Dev

How to print terminal formatted output to a variable

From Dev

How to print well-formatted tables to the console?

From Dev

How to print terminal formatted output to a variable

From Dev

How can I print well formatted code

From Dev

How to print formatted double value to string in java?

From Dev

How to print formatted Decimal value in Swift?

From Dev

How to change a date formatted number into number in SQL

From Dev

How to produce a formatted date string in Q/KDB?

From Dev

How to get formatted Date from string android?

From Dev

How to get textbox date formatted as a string?

From Dev

How to convert Object as timestamp to formatted date

From Dev

How to sort by formatted date with Elastic Search?

From Dev

How is this int formatted to represent date and time?

From Dev

Show formatted date in f.text_field if present, else placeholder

From Dev

How to retrieve formatted version of date from date picker backend

From Dev

how to echo/print a nicely formatted array to the modx log?

From Dev

How to do formatted print of hex number with bigint/Math::BigInt?

From Dev

How to print an array, or a formatted string in EDUMIPS64 simulator?

From Dev

How to cat and print formatted more than one file?

From Dev

How to print Current Time and Date

From Java

How to print a date in a regular format?

From Dev

How to print date and time with jquery

From Dev

How to print Current Time and Date

From Dev

How to print a given date in C

From Dev

Spring MVC: how to display formatted date values in JSP EL

From Dev

In Pentaho Kettle, how to generate formatted date for parameter for Google Analytics API?

From Dev

How to transfer the formatted date string from my DatePickerFragment?

From Dev

Java 8 epoch-millis time stamp to formatted date, how?

Related Related

  1. 1

    How to print a formatted SOAPMessage?

  2. 2

    How to print terminal formatted output to a variable

  3. 3

    How to print well-formatted tables to the console?

  4. 4

    How to print terminal formatted output to a variable

  5. 5

    How can I print well formatted code

  6. 6

    How to print formatted double value to string in java?

  7. 7

    How to print formatted Decimal value in Swift?

  8. 8

    How to change a date formatted number into number in SQL

  9. 9

    How to produce a formatted date string in Q/KDB?

  10. 10

    How to get formatted Date from string android?

  11. 11

    How to get textbox date formatted as a string?

  12. 12

    How to convert Object as timestamp to formatted date

  13. 13

    How to sort by formatted date with Elastic Search?

  14. 14

    How is this int formatted to represent date and time?

  15. 15

    Show formatted date in f.text_field if present, else placeholder

  16. 16

    How to retrieve formatted version of date from date picker backend

  17. 17

    how to echo/print a nicely formatted array to the modx log?

  18. 18

    How to do formatted print of hex number with bigint/Math::BigInt?

  19. 19

    How to print an array, or a formatted string in EDUMIPS64 simulator?

  20. 20

    How to cat and print formatted more than one file?

  21. 21

    How to print Current Time and Date

  22. 22

    How to print a date in a regular format?

  23. 23

    How to print date and time with jquery

  24. 24

    How to print Current Time and Date

  25. 25

    How to print a given date in C

  26. 26

    Spring MVC: how to display formatted date values in JSP EL

  27. 27

    In Pentaho Kettle, how to generate formatted date for parameter for Google Analytics API?

  28. 28

    How to transfer the formatted date string from my DatePickerFragment?

  29. 29

    Java 8 epoch-millis time stamp to formatted date, how?

HotTag

Archive