[英]how to convert DateTime into Unix TimeStamp in F#?
is there a function / library / easy snippet doing that ? 有没有这样的函数/库/简单片段?
I've found answers for Python for example, but not F# thanks 我找到了Python的答案,但不是F#感谢
I've found the other way round here http://tryfs.net/snippets/snippet-r6 我在这里找到了相反的方法http://tryfs.net/snippets/snippet-r6
open System
let toDateTime (timestamp:int) =
let start = DateTime(1970,1,1,0,0,0,DateTimeKind.Utc)
start.AddSeconds(float timestamp).ToLocalTime()
You should be careful because timestamp
might be something else than int
. 你应该小心,因为
timestamp
可能不是int
。 However in .NET 4.6.x you should be able to use the functions referred to in the comments. 但是,在.NET 4.6.x中,您应该能够使用注释中引用的功能。
Here's an example: 这是一个例子:
open System
let unix = DateTimeOffset(DateTime.Now).ToUnixTimeSeconds() //val unix : int64 = 1488254491L
let dt = DateTimeOffset.FromUnixTimeSeconds(unix) //val dt : DateTimeOffset = 2017/02/28 4:01:31 +00:00
dt.LocalDateTime //val it : DateTime = 2017/02/28 13:01:31
As ToUnixTimeSeconds
and FromUnixTimeSeconds
operates on DateTimeOffset
, either work with that or convert back/forth to DateTime
if necessary. 由于
ToUnixTimeSeconds
和FromUnixTimeSeconds
在DateTimeOffset
运行,因此可以使用它或者在必要时来回转换为DateTime
。
By the way I find it that in the .fsx file the intellisense is a bit flaky for these methods (but works). 顺便说一下,我发现在.fsx文件中,intellisense对于这些方法来说有点不稳定(但有效)。 In .fs files it's OK.
在.fs文件中没关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.