0

I have a MySql DateTime 1442915520000 and i need to parse it using C#.

I have tried the following:

DateTime dt = Convert.ToDateTime(dataFim);

DateTime dt = DateTime.Parse(i[0]["dataFim"].ToString());

Any advice? It must be something really easy but i can't seem to figure it out...

EDIT:

I forgot to mention that this value is coming from a java web app and the value is getting parsed to java Date

Kunal
  • 327
  • 1
  • 4
  • 19
  • Why are you not storing the value in a DateTime field in MySQL to start with? – Jon Skeet Sep 22 '15 at 08:41
  • @DavidG i have tried the accepted answer from your link and it return an error saying out of range value – Kunal Sep 22 '15 at 08:44
  • Your value is in milliseconds so use `AddMilliseconds` instead of `AddSeconds`. – DavidG Sep 22 '15 at 08:46
  • @JonSkeet It is stored in datetime format. this value is getting passed trhough a java application that parses the mysql value to java Date – Kunal Sep 22 '15 at 08:46
  • @Kunal: Well how is the Java application passing the value, and why isn't that being done in a more appropriate manner? – Jon Skeet Sep 22 '15 at 08:48
  • @JonSkeet I have a Sping MVC project and it uses hibernate framework to connect to mysql db's and in my model i parse that value to Java Date format. I am using Rest to get the data i want in my C# project – Kunal Sep 22 '15 at 08:51
  • I would strongly suggest that your REST service should be returning values in an ISO-8601 format then, instead of milliseconds-since-the-unix-epoch. That's a much more platform-neutral format, as well as being easier to read in responses. But if you *have* to stick with this format, just use the answer to the question DavidG referred to, adding milliseconds instead of seconds. – Jon Skeet Sep 22 '15 at 08:54
  • @JonSkeet I have researched about epoch time and found a online time converter and it actually works with my value... i'm going to research some more about this and post the answer when i find it. You have been a great help. Thanks :) – Kunal Sep 22 '15 at 08:56
  • 1
    Finding "a way which works" isn't the same as looking at your overall solution and determining whether or not it's the *best* approach... – Jon Skeet Sep 22 '15 at 09:02
  • You are right. It was the best approach on the java app but when i pass it to c# it might not be... but since the solution given is easy and it doesn't require much processing it won't be a problem – Kunal Sep 22 '15 at 09:32

1 Answers1

2

Just add the milliseconds to Unix epoch start:

DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(1442915520000);
Matteo Umili
  • 3,412
  • 1
  • 19
  • 31