[英]System.DateTime formatting in F#
我有以下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
對於日期參數,參數的格式為:“ 1-1-2011”,因此當我將日期寫入xml序列化程序時,“ MD-YYYY”會得到以下格式:
1/1/2011 12:00:00 AM
我想完全刪除計時器。 最好的方法是什么?
這個如何?
(DateTime.Parse arg).ToString("MM-dd-yyyy")
默認情況下,使用“ dateTime” 數據類型對DateTime
實例進行序列化以進行序列化。 您可以通過注釋您的類型以改為使用“日期”來進行更改,這將以YYYY-MM-DD
格式對其進行序列化-如果這不適用於您,則只需序列化一個字符串即可,該字符串將保留您選擇的格式。
您可以像這樣設置自定義序列化屬性:
完整的日期時間: [<XmlAttribute("start-date", DataType = "dateTime")>]
只是日期部分: [<XmlAttribute("start-date", DataType = "date")>]
例:
[<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.