Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Date to Milliseconds

I am working with Visual Studio 2010, MVC 3 and C#. I am creating some highcharts and need to have the x-axis be a date. I am pulling the dates from a database and adding them to and array that will then be passed to highcharts. I think highcharts requires the dates to be in millisecond format. Ho do I go about converting a DateTime of '12/20/2011 5:10:13 PM" for example to milliseconds?

like image 526
Linger Avatar asked Mar 15 '12 16:03

Linger


People also ask

How do you convert date to milliseconds?

This is the number of seconds since the 1970 epoch. To convert seconds to milliseconds, you need to multiply the number of seconds by 1000. To convert a Date to milliseconds, you could just call timeIntervalSince1970 and multiply it by 1000 every time.

How do I convert date to milliseconds in Excel?

Select a timestamp type in the Type group box. If you need to display milliseconds, instead of clicking the Date category, select Custom, and then enter dd-mmm-yyyy hh:mm:ss. 000 in the Type field.

How do you convert timestamps to milliseconds?

You can get the time in seconds using time. time function(as a floating point value). To convert it to milliseconds, you need to multiply it with 1000 and round it off.

How do you convert epoch time to milliseconds?

Convert from human-readable date to epoch long epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds.


4 Answers

Once you figure out what you want to calculate milliseconds from, you can just take one DateTime object from another to get a TimeSpan object. From TimeSpan you can get TotalMilliseconds.

In other words, if start and end are DateTime objects, you can do this:

double milliseconds = (end - start).TotalMilliseconds;
like image 55
Matt Burland Avatar answered Oct 13 '22 12:10

Matt Burland


You can use the DateTime.Ticks property and convert the value to milliseconds.

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.

A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond.

like image 31
Marek Grzenkowicz Avatar answered Oct 13 '22 11:10

Marek Grzenkowicz


The .Ticks in C# DateTime gives you the value of any time in ticks. You can thereafter convert to milliseconds as shown below:

long dateticks = DateTime.Now.Ticks;
long datemilliseconds = dateticks / TimeSpan.TicksPerMillisecond;
like image 7
Ifesinachi Bryan Avatar answered Oct 13 '22 12:10

Ifesinachi Bryan


DateTime[] dates = ;

var minDate = dates.Min();

var msDates = dates.Select(date => (date - minDate).TotalMilliseconds).ToArray();
like image 1
Serj-Tm Avatar answered Oct 13 '22 10:10

Serj-Tm