views:

807

answers:

4

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?

+1  A: 

It's much simpler to do this:

DateTime dt = new DateTime(633896886277130000);

Which gives

dt.ToString() = "9/27/2009 10:50:27 PM"

You can format this any way you want by using dt.ToString(MyFormat). Refer to this reference for format strings. "MMMM dd, yyyy" works for what you specified in the question.

Not sure where you get October 1.

Eric J.
october 1 is just a format sample..I need the convert statement so i can use it in the SQL select statement
+9  A: 

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");
Jason Berkan
+3  A: 
    private void button1_Click(object sender, EventArgs e)
    {
        long myTicks = 633896886277130000;
        DateTime dtime = new DateTime(myTicks);
        MessageBox.Show(dtime.ToString("MMMM d, yyyy"));
    }

Gives

September 27, 2009

Is that what you need?

I don't see how that format is necessarily easy to work with in SQL queries, though.

JosephStyons
That's what he asked for, if it isn't what he needed. :) You beat me to it.
John Kraft
+2  A: 
  datename(month,(MachineGroups.TimeAdded-599266080000000000)/864000000000) +
  space(1) +
  datename(d,(MachineGroups.TimeAdded-599266080000000000)/864000000000) +
  ', ' +
  datename(year,(MachineGroups.TimeAdded-599266080000000000)/864000000000);
Steve Kass
this is perfect.... thanks
+1, an accepted answer with no up votes really stinks!
KM
"Perfect" is not the word I would use for this...
Deniz Dogan