简体   繁体   English

F#中的System.DateTime格式

[英]System.DateTime formatting in F#

I have the following f# code: 我有以下f#代码:

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”,因此当我将日期写入xml序列化程序时,“ MD-YYYY”会得到以下格式:

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. 默认情况下,使用“ dateTime” 数据类型DateTime实例进行序列化以进行序列化。 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. 您可以通过注释您的类型以改为使用“日期”来进行更改,这将以YYYY-MM-DD格式对其进行序列化-如果这不适用于您,则只需序列化一个字符串即可,该字符串将保留您选择的格式。

You can set the custom serialization attribute like this: 您可以像这样设置自定义序列化属性:

full datetime: [<XmlAttribute("start-date", DataType = "dateTime")>] 完整的日期时间: [<XmlAttribute("start-date", DataType = "dateTime")>]

just the date part: [<XmlAttribute("start-date", DataType = "date")>] 只是日期部分: [<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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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