Ramy
Ramy

Reputation: 21261

System.DateTime formatting in F#

I have the following f# code:

let mutable argNum = 0
let cmdArgs = System.Environment.GetCommandLineArgs()

for arg in cmdArgs do
    printfn "arg %d : %s" argNum arg
    match argNum with
    | 1 -> pmID      <- System.Int32.Parse arg 
    | 2 -> startDate <- System.DateTime.Parse arg
    | 3 -> endDate   <- System.DateTime.Parse arg
    | _ -> ()
    argNum <- argNum + 1

for the date parameters, the argument comes in the form: "1-1-2011", so "M-D-YYYY" when i write the dates into the xml serializer, I get the following format:

1/1/2011 12:00:00 AM

I'd like to remove the Time piece completely. What's the best way to do this?

Upvotes: 3

Views: 4910

Answers (2)

BrokenGlass
BrokenGlass

Reputation: 160942

By default DateTime instances are serialized using the "dateTime" datatype for serialization. You can change it by annotating your type to use "date" instead, which will serialize it in the format YYYY-MM-DD - if this doesn't work for you just serialize a string instead that holds the format of your choice.

You can set the custom serialization attribute like this:

full datetime: [<XmlAttribute("start-date", DataType = "dateTime")>]

just the date part: [<XmlAttribute("start-date", DataType = "date")>]

Example:

[<Serializable>]
type DateTest() = 
  let mutable startDate = DateTime.Now
  [<XmlAttribute("start-date", DataType = "date")>]
  member x.StartDate with get() = startDate and set v = startDate <- v

Upvotes: 3

Nyi Nyi
Nyi Nyi

Reputation: 788

What about this one?

(DateTime.Parse arg).ToString("MM-dd-yyyy")

Upvotes: 4

Related Questions