开发者

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?


What about this one?

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜