views:

59

answers:

1

How to convert one datetime to ticks?

+4  A: 

By using the Ticks property:

var now = DateTime.Now;
var ticks = now.Ticks;

UPDATE:

For those who don't like the var keyword and consider this more readable:

DateTime now = DateTime.Now;
long ticks = now.Ticks;
Darin Dimitrov
You are overusing the `var` keyword. It's not obvious what type the `Ticks` property returns, especially for someone who has never seen it before...
Guffa
@Guffa, I agree. I believe using var in this example actually harms readability.
David Neale
OK, I replace the `var` keyword with proper types.
Darin Dimitrov
Or simply: long ticks = DateTime.Now.Ticks;
rursw1
@rursw1: Then it's just an example on how to convert the current time to ticks. By giving the example as two lines, the second line is an example of how to convert any datetime value.
Guffa