我怎么能一个DateTime转换为int? [英] How can I convert a DateTime to an int?

查看:514
本文介绍了我怎么能一个DateTime转换为int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的日期时间2011年4月25日下午五时12分13秒
并试用了此将其转换为int

  INT结果= dateDate.Year * 10000 + dateDate.Month * 100
             + dateDate.Day + dateDate.Hour + dateDate.Minute + dateDate.Second;

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


解决方案

  dateDate.Ticks

应该给你你在找什么。


  

此属性的重新presents价值
  100纳秒的间隔的数量
  12:00:00以来已经过去了
  午夜,0001年1月1,这
  再presents DateTime.MinValue。它确实
  不包括蜱的数量
  是由于闰秒。


  
  

DateTime.Ticks



如果你真的找Linux的纪元时间(秒自1970年1月1日),为的这个问题应该是相关的。


但如果你实际上是试图COM preSS的字符串重新日期为一个int presentation,你应该问自己,为什么你不只是把它作为一个字符串开始。如果你仍然想后做, Stecya的回答是正确的。请记住,它不适合进入一个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;

But it still getting 2011425 how can i get the time as well?

解决方案

dateDate.Ticks

should give you what you're looking for.

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.

DateTime.Ticks


If you're really looking for the Linux Epoch time (seconds since Jan 1, 1970), the accepted answer for this question should be relevant.


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆