Question

i have a string

1368352924.281610000

presenting the DateTime.

How can i parse this?

Is tried:

string rststr = Convert.ToString(result);                       
string[] rststrarr = rststr.Split('.');
DateTime.Parse(rststrarr[0]);

EDIT Sorry for the confusing. It pointed out that it was a unix time stamp in a high resoulution. Kind regards.

Was it helpful?

Solution

It's likely that this number represents the number of seconds elapsed since Jan 1, 1970 UTC. This is the "Unix Epoch" value, and is very common in many systems and date formats.

Assuming I am correct about the type of value you have, then you can do the following:

// you said you were starting with a string
string s = "1368352924.281610000";

// but you are going to need it as a double.
// (If it's already a double, skip these two steps)
var d = double.Parse(s);

// starting at the unix epoch
DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

// simply add the number of seconds that you have
DateTime dt = epoch.AddSeconds(d);


Debug.WriteLine("{0} {1}", dt, dt.Kind);  // prints  05/12/2013 10:02:04 Utc

OTHER TIPS

You need TimeSpan.FromMilliseconds, and then to add that to the reference time your milliseconds value refers to.

e.g. (EDIT 2: Assuming your reference date is DateTime.MinValue)

double d = double.Parse("1368352924.281610000");
TimeSpan ts = TimeSpan.FromMilliseconds(d);
DateTime dt = DateTime.MinValue.Add(ts);

Edit There are many ways to skin this, as Wouter Huysentruit has pointed out. And you should choose the one that emphasises the intent for your scenario E.g.:

DateTime dt = DateTime.MinValue.Add(ts);
dt = DateTime.MinValue + ts;
dt = dateTime.MinValue.AddMilliseconds(d);
dt = DateTime.FromOADate(d);

(For the latter one, see FromOADate)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top