Traffy
Traffy

Reputation: 2861

Conversion from milliseconds to DateTime format

I got a string which is representend like this :

string startdatetime = "13988110600000"

What I want to do is to convert this string (which are milliseconds) to a DateTime variable. This is what I'm doing :

double ticks = double.Parse(startdatetime);
TimeSpan time = TimeSpan.FromMilliseconds(ticks);
DateTime startdate = new DateTime(time.Ticks);

The result is almost good : I've got a weird date but time is okay (30/04/0045 18:00:00).

Is there any reason to this?

Upvotes: 59

Views: 82988

Answers (6)

codejockie
codejockie

Reputation: 10844

Putting this out here because I ran into a case where I needed to get the selected date from a Material Date Picker in .NET Android. I had originally converted directly from milliseconds to C#'s DateTime using the constructor overload that accepts milliseconds, but the date value was incorrect. On searching, I found this SO question with a comment in the accepted answer. Since I am using .NET 8, I used the helper method DateTimeOffset.FromUnixTimeMilliseconds as mentioned by @Jepp.

The reason, the earlier conversion did not work was because Java uses the Unix epoch which starts at January 1, 1970, while C#'s starts at January 1, 0001. To get it correctly, you need to create a DateTime from that epoch start date, then add the number of milliseconds to it (if .NET version is less than 4.6).

(new DateTime(1970, 1, 1)).AddMilliseconds(long.Parse(startdatetime))

Upvotes: 0

Windgate
Windgate

Reputation: 430

I understand your value is measured like a Unix Timestamp. As commented in other (deprecated) answers, there is an utility you can use and avoids you to "count" from 01/01/1970 (I always forget that date...)

With your variable string startdatetime = "13988110600000"

long elapsed_ms = long.Parse(startdatetime);
DateTime date = 
   DateTimeOffset.FromUnixTimeMilliseconds(elapsed_ms).UtcDateTime;

Add try/catch and different values for the initial variable as you wish and you are done

Upvotes: 0

Javier Mendoza
Javier Mendoza

Reputation: 17

Get Now in milliseconds:

DateTimeOffset.Now.ToUnixTimeMilliseconds()

yourDateTime.ToUniversalTime().Subtract(
    new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
).TotalMilliseconds

Upvotes: 0

Soner Gönül
Soner Gönül

Reputation: 98740

Since TimeSpan.Ticks property returns long, your new DateTime(time.Ticks) code call DateTime(long) constructor and from it's documentation;

A date and time expressed in the number of 100-nanosecond intervals that have elapsed since January 1, 0001 at 00:00:00.000 in the Gregorian calendar.

That's why it's wrong to say The result is almost good. The value of result is expected as implemented and documented.

Upvotes: 5

Christoph Fink
Christoph Fink

Reputation: 23093

DateTime in .NET is initialized to 0001-01-01 00:00:00 and then you add your TimeSpan, which seems to be 45 Years.

It is common for such (milli)-second time definitions to start at 1970-01-01 00:00:00, so maybe the following gives you the expected result:

double ticks = double.Parse(startdatetime);
TimeSpan time = TimeSpan.FromMilliseconds(ticks);
DateTime startdate = new DateTime(1970, 1, 1) + time;

or simply

var date = (new DateTime(1970, 1, 1)).AddMilliseconds(double.Parse(startdatetime));

Upvotes: 96

Michael Mairegger
Michael Mairegger

Reputation: 7301

The reason is that your value is based on milliseconds elapsed since 01/01/1900 or 01/01/1970 and DateTime in C# starts with 01/01/00001.

I think it starts from 01/01/1970 because 1970 + 45 would be 2015 which I think it is the year you search.

Upvotes: 4

Related Questions