简体   繁体   中英

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 "MD-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?

这个如何?

(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM