如何将DateTime转换为int? [英] How can I convert a DateTime to an int?
问题描述
我有以下DateTime 4/25/2011 5:12:13 PM
并尝试将其转换为int
I have the following DateTime 4/25/2011 5:12:13 PM and tried this to convert it to int
int result = dateDate.Year * 10000 + dateDate.Month * 100
+ dateDate.Day + dateDate.Hour + dateDate.Minute + dateDate.Second;
但是它仍然可以获得2011425我还能如何获得时间?
But it still getting 2011425 how can i get the time as well?
推荐答案
dateDate.Ticks
应该给你你要找的东西。
should give you what you're looking for.
此属性的值表示
从0001年1月1日午夜12:00以后
午餐已经过去的100纳秒间隔
,其中
表示DateTime.MinValue。
不包括
归因于闰秒的蜱数。
The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds.
如果您真正在寻找Linux Epoch时间(1970年1月1日以来的几秒钟),则接受这个问题应该是相关的。
但是,如果您真的试图将日期的字符串表示压缩到int中,那么您应该问自己为什么不将它作为一个字符串存储开始。如果您还要这样做, Stecya的答案是正确的。请记住,它不适合int,你必须使用一个长的。
But if you're actually trying to "compress" a string representation of the date into an int, you should ask yourself why aren't you just storing it as a string to begin with. If you still want to do it after that, Stecya's answer is the right one. Keep in mind it won't fit into an int, you'll have to use a long.
这篇关于如何将DateTime转换为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!