Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a DateTime to an int?

Tags:

c#

datetime

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?

like image 897
someguy Avatar asked Apr 26 '11 10:04

someguy


People also ask

How do you convert time to integer in Python?

Example 1: Integer timestamp of the current date and timeConvert the DateTime object into timestamp using DateTime. timestamp() method. We will get the timestamp in seconds. And then round off the timestamp and explicitly typecast the floating-point number into an integer to get the integer timestamp in seconds.

How do I convert a date to an integer in Java?

Or, if you really want to convert the 'date' into integer type 06/03/2017 to 06032017 .. you can do something like this. SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy"); System. out. println(Integer.


1 Answers

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.

like image 101
Alex J Avatar answered Oct 09 '22 23:10

Alex J