2

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.

zirbel
  • 402
  • 1
  • 5
  • 23

2 Answers2

3

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)

Andy Brown
  • 18,200
  • 3
  • 46
  • 59
  • @WouterHuysentruit. It does indeed, and so would `DateTime.MinValue.AddMilliseconds(d);` - so many options to choose from! – Andy Brown May 13 '13 at 12:10
  • I agree, intent is all - answer updated to emphasise the options – Andy Brown May 13 '13 at 12:14
  • So that date is `16/01/0001 20:05:52`? Hmmm. – Matthew Watson May 13 '13 at 12:15
  • `FromOADate` takes number of days, not ms. Upper limit is 2958465.99999999, and the OP has a number 1368352924.281610000. Also, OP didn't specify the offset. – huysentruitw May 13 '13 at 12:15
  • @WouterHuysentruit My point entirely. :) It's impossible to answer the question without that information. – Matthew Watson May 13 '13 at 12:18
  • @WouterHuysentruit. Understood, I had assumed the OP had just hit the number pad a few times to generate a random value, so showed OADate as an option as well. I'm sure between us we can find a few more without trying too hard! – Andy Brown May 13 '13 at 12:20
  • @WouterHuysentruit - it's a unix timestamp, in seconds since 1/1/1970 utc. See my answer. Good try though! :) – Matt Johnson-Pint May 13 '13 at 21:02
  • @MattJohnson I didn't try anything :) Did you really have to figure that out yourself? Why is the 'ms' in the title of this question? Where is zirbel? – huysentruitw May 14 '13 at 06:56
3

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
Matt Johnson-Pint
  • 197,368
  • 66
  • 382
  • 508
  • 2
    +1 It's Unix Epoch and the decimals are fraction of a second in a very high resolution – jgauffin May 13 '13 at 21:13
  • 2
    I wuld have a preference for `double.Parse` or `double.TryParse` instead of `Convert.ToDouble` (see [Double.TryParse or Convert.ToDouble - which is faster and safer?](http://stackoverflow.com/questions/586436/double-tryparse-or-convert-todouble-which-is-faster-and-safer)) – Andy Brown May 14 '13 at 13:12
  • @MattJohnson. This is no reflection on the your answer: the question was specifically how to parse milliseconds. While the specific example is probably seconds off the unix epoch - it is worth pointing out that even that is a dangerous assumption without knowing for sure (it would suck to be a year out, for instance). Not enough info in the question to assume that categorically. – Andy Brown May 14 '13 at 13:15
  • @AndyBrown - Agreed, updated to use `double.Parse`. I don't know enough about the OP's stability of input to know if `TryParse` is required, but he could of course use that instead. (From the original question, it almost seems that maybe `result` is already a double.) – Matt Johnson-Pint May 14 '13 at 15:00
  • @AndyBrown - It seemed to me that the OP was confused about what input he had. I tried milliseconds first, but the result wasn't meaningful. Since the value in seconds was just a day before the post, I'm pretty sure that's what he's got. (I did qualify my answer with "it's likely" and "assuming I am correct"...) – Matt Johnson-Pint May 14 '13 at 15:04
  • @MattJohnson. I missed the qualifications - my fault. – Andy Brown May 14 '13 at 17:21
  • @AndyBrown - NP. :) Thanks for the feedback! – Matt Johnson-Pint May 14 '13 at 17:32