Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert ticks to a date format?

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?

like image 305
user175084 Avatar asked Sep 28 '09 20:09

user175084


People also ask

How do I convert a tick to a date in Excel?

You can do this by formatting cell to date. Right Click on cell->Format Cell->Number tab->Date->Ok.

How do you convert ticks to time?

A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond (see TicksPerMillisecond) and 10 million ticks in a second.

How do you convert a date field?

Convert text dates by using the DATEVALUE function Select a blank cell and verify that its number format is General. Click the cell that contains the text-formatted date that you want to convert. Press ENTER, and the DATEVALUE function returns the serial number of the date that is represented by the text date.

How do I convert datetime to date format?

To convert a datetime to a date, you can use the CONVERT() , TRY_CONVERT() , or CAST() function.


2 Answers

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"); 
like image 99
Jason Berkan Avatar answered Sep 28 '22 02:09

Jason Berkan


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.

like image 21
Eric J. Avatar answered Sep 28 '22 02:09

Eric J.