vmb
vmb

Reputation: 3028

How to convert HEX data to Datetime

I have a hex string like this 529CD17C.This is corresponding to One date time 12/2/2013 06:29:16 PM .(ie in MM/dd/yyyy hh:mm:ss AM/PM).How can i do this in c# coding

Upvotes: 1

Views: 7513

Answers (2)

mkamioner
mkamioner

Reputation: 2461

You are working with a UNIX time stamp. First you need to convert it to an integer value, then go ahead and add that amount of seconds to the epoch (January 1, 1970).

Here is an example:

string hexValue = "529CD17C";
int secondsAfterEpoch = Int32.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
DateTime epoch = new DateTime(1970, 1, 1);
DateTime myDateTime = epoch.AddSeconds(secondsAfterEpoch);
Console.WriteLine(myDateTime);

Hope that was helpful!

Upvotes: 2

Bas van Dijk
Bas van Dijk

Reputation: 10713

Here is someone with the same question http://social.msdn.microsoft.com/Forums/en-US/c9624a19-1ef6-4f60-b063-527beb36de1d/hex-string-to-datetime-conversion It looks like the function provided could work.

They first convert the HEX value to an integer and then convert the integer to a date type.

Upvotes: 0

Related Questions