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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With