67

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?

razlebe
  • 7,134
  • 6
  • 42
  • 57
someguy
  • 995
  • 3
  • 11
  • 22

6 Answers6

101
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.

Community
  • 1
  • 1
Alex J
  • 9,905
  • 6
  • 36
  • 46
  • your right i don't have to change it int if i'm not using it.Currently working with Dictionary and i thought that using int is the easier – someguy Apr 26 '11 at 16:01
  • 2
    Why not just use the DateTime itself as the dictionary key? – Alex J Apr 26 '11 at 16:27
68
long n = long.Parse(date.ToString("yyyyMMddHHmmss"));

see Custom Date and Time Format Strings

Martin
  • 10,738
  • 14
  • 59
  • 67
Stecya
  • 22,896
  • 10
  • 72
  • 102
  • 6
    wow! sometimes c# makes me wonder if i have to do anything on my own! – atoMerz Apr 26 '11 at 10:23
  • 2
    Good to know about this is that mm stands for minute and MM for month so it should be: Int64 n = Int64.Parse(date.ToString("yyyyMMddhhmmss")); – Henrik Fransas Oct 30 '14 at 09:58
  • 3
    Actually, you want "yyyyMMddHHmmss". (e.g. http://stackoverflow.com/a/3025374/530545 ) Case matters; using a 12-hour clock is going to get you duplicates. – Granger Oct 30 '14 at 15:09
  • 1
    Note that you won't get sequential integers from this method, or a consistent difference in value for a given timespans. For example, the new year second will switch from 20171231235959 to 20180101000000, a difference of 8869764041 for one second... while other elapsed seconds are just a difference of 1. Operations such as < == > will all be correct though. – Stephen Holt Nov 03 '17 at 15:29
6

I think you want (this won't fit in a int though, you'll need to store it as a long):

long result = dateDate.Year * 10000000000 + dateDate.Month * 100000000 + dateDate.Day * 1000000 + dateDate.Hour * 10000 + dateDate.Minute * 100 + dateDate.Second;

Alternatively, storing the ticks is a better idea.

Jackson Pope
  • 14,520
  • 6
  • 56
  • 80
  • +1 I think this is the most correct answer because Ticks does not give the exact format the OP tried to make and I think converting to string as intermediate stage gives worse performance because of parsing. – M.Sameer Apr 26 '11 at 11:54
3

Do you want an 'int' that looks like 20110425171213? In which case you'd be better off ToString with the appropriate format (something like 'yyyyMMddHHmmss') and then casting the string to an integer (or a long, unsigned int as it will be way more than 32 bits).

If you want an actual numeric value (the number of seconds since the year 0) then that's a very different calculation, e.g.

result = second
result += minute * 60
result += hour * 60 * 60
result += day * 60 * 60 * 24 

etc.

But you'd be better off using Ticks.

Unsliced
  • 10,404
  • 8
  • 51
  • 81
0
string date = DateTime.Now.ToString();

date = date.Replace("/", "");
date = date.Replace(":", "");
date = date.Replace(" ", "");
date = date.Replace("AM", "");
date = date.Replace("PM", "");            
return date;
  • 4
    The output is culture specific, so replace might not work as intended. Better use the `ToString` overload to provide formatting. – Fred Aug 22 '18 at 12:30
  • I think what Fred was saying is that if someone has a different default date format (for example, - instead of /, you will have issues. ToString() without formatting is not even guaranteed to have the month in the front. My system for example defaults dd-mmm-yyyy because it's the format i prefer, being ex military. – John Lord Dec 31 '20 at 16:14
0

Use Date.parse for converting datetime to an integer.

var yourdate = new Date(); //in your case '2011-04-25T17:12:13'
var d = Date.parse(yourdate);
alert(d); //this is in interger
Malik Zahid
  • 593
  • 5
  • 13