How can I convert ticks to a date format?

user175084 picture user175084 · Sep 28, 2009 · Viewed 125.1k times · Source

I am converting a ticks value to a date like this:

Convert(datetime, (MachineGroups.TimeAdded - 599266080000000000)/864000000000);

Using this i get:

9/27/2009 10:50:27 PM

But I want just the date in this format:

October 1, 2009

My sample ticks value is

633896886277130000

What is the best way to do this?

Answer

Jason Berkan picture Jason Berkan · Sep 28, 2009

A DateTime object can be constructed with a specific value of ticks. Once you have determined the ticks value, you can do the following:

DateTime myDate = new DateTime(numberOfTicks);
String test = myDate.ToString("MMMM dd, yyyy");