【发布时间】:2020-02-03 10:21:57
【问题描述】:
我只想更改 F# 中 System.DateTime 对象的日期和/或时间部分,同时保留现有字段。 当我说“改变”时,我的意思是保持不变性,所以我想在操作后接收一个新的 DateTime 对象。
我已经说明了我会如何更改时间,但这似乎是不正确的。
open System
type State = {
When: DateTime
}
// How would I actually do the following?
// let setTime h m state = { state with When = { state.When with Hour = h; Minute = m } }
理想情况下,该解决方案适用于 .NET Core 和 Framework。
编辑
正如评论中提到的,有一个separate question on how to change a DateTime,但这个问题似乎集中在 C#(而不是 F#)以及添加 TimeSpan 上。 我想设置字段值(而不是添加到它们)。 有一些答案可以复制字段并调用构造函数,但我希望有一个更优雅的解决方案。
【问题讨论】:
标签: .net datetime time .net-core f#