gpt4 book ai didi

c# - 如何将 DateTime 转换为 int?

转载 作者:IT王子 更新时间:2023-10-29 03:57:04 24 4
gpt4 key购买 nike

我有以下日期时间 4/25/2011 5:12:13 PM并尝试将其转换为 int

 int result = dateDate.Year * 10000 + dateDate.Month * 100 
+ dateDate.Day + dateDate.Hour + dateDate.Minute + dateDate.Second;

但它仍然得到 2011425 我怎样才能得到时间呢?

最佳答案

dateDate.Ticks

应该给你你要找的东西。

The value of this property representsthe number of 100-nanosecond intervalsthat have elapsed since 12:00:00midnight, January 1, 0001, whichrepresents DateTime.MinValue. It doesnot include the number of ticks thatare attributable to leap seconds.

DateTime.Ticks


如果您真的在寻找 Linux 纪元时间(自 1970 年 1 月 1 日以来的秒数),this 的可接受答案问题应该是相关的。


但是,如果您实际上试图将日期的字符串表示形式“压缩”为 int,您应该问问自己,为什么不直接将其存储为字符串。如果在那之后你还想这样做,Stecya's answer是正确的。请记住,它不适合 int,您必须使用 long。

关于c# - 如何将 DateTime 转换为 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5788883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com